I have the following scenario for command line argument. If there is a particular option then there should be some other required options. For example if there is -- create then there should be --name. Also if there is --remove then there should be --id. Is it possible to implement this scenario with argparse? or someother thing?
Optional Arguments To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.
Accepting optional command-line arguments When running this program, this optional argument can be specified with either --expected=SOMETHING or -e SOMETHING . If this expected argument isn't provided then it will default to the value None .
required is a parameter of the ArugmentParser object's function add_argument() . By default, the arguments of type -f or --foo are optional and can be omitted. If a user is required to make an argument, they can set the keyword argument required to True .
This can be done with subcommands as long as you don't mind create
and remove
not being preceded with hyphens. This may make sense anyway, since those verbs are often used as actions rather than options.
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