Is there a way to specify to Python's ArgumentParser that two optional flags are conflicting?
arg_parser.add_argument('-c', '--clean', action='store_true')
arg_parser.add_argument('-d', '--dirty', action='store_true')
I want the user to be able to specify either none of those, or only one.
Is it achievable without further conditions?
How about adding a mutually exclusive group:
group = arg_parser.add_mutually_exclusive_group()
group.add_argument('-c', '--clean', action='store_true')
group.add_argument('-d', '--dirty', action='store_true')
with this I get the following behaviour:
>>> arg_parser.parse_args(['--clean'])
Namespace(clean=True, dirty=False)
>>> arg_parser.parse_args(['--dirty'])
Namespace(clean=False, dirty=True)
>>> arg_parser.parse_args(['--dirty','--clean'])
usage: PROG [-h] [-c | -d] PROG: error: argument -c/--clean: not allowed with argument -d/--dirty
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