To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.
Number of Arguments If you want your parameters to accept a list of items you can specify nargs=n for how many arguments to accept. Note, if you set nargs=1 , it will return as a list not a single value.
Metavar: It provides a different name for optional argument in help messages.
An argparse argument can be limited to specific values with the choices
parameter:
...
parser.add_argument('--val',
choices=['a', 'b', 'c'],
help='Special testing value')
args = parser.parse_args(sys.argv[1:])
See the docs for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With