How do I use a different number of parameters for each option?
ex) a.py
parser.add_argument('--opt', type=str,choices=['a', 'b', 'c'],help='blah~~~')
- choice : a / parameter : 1
ex)
$ python a.py --opt a param
- choice : c / parameter :2
ex)
$ python a.py --opt b param1 param2
You need to add sub-commands,
ArgumentParser.add_subparsers()
method will help youCheck this example
You may add more parameters, one for each a, b, and c and also optional arguments for your params. By using the named parameter nargs='?' you can specify that they are optional and with the default="some value" you ensure it rises no errors. Finally, based on the selected option, a,b or c you will be able to capture the ones you need.
Here's a short usage example:
here, if no values are selected, the default ones are used. You can play with this to get what you want.
Cheers