I am trying to create a mutually exclusive group between different groups: I have the arguments -a, -b, -c, and I want to have a conflict with -a and -b together, or -a and -c together. Help should show something like [-a | ([-b] [-c])].
The following code does not seem to have mutually exclusive options:
import argparse parser = argparse.ArgumentParser(description='My desc') main_group = parser.add_mutually_exclusive_group() mysub_group = main_group.add_argument_group() main_group.add_argument("-a", dest='a', action='store_true', default=False, help='a help') mysub_group.add_argument("-b", dest='b', action='store_true',default=False,help='b help') mysub_group.add_argument("-c", dest='c', action='store_true',default=False,help='c help') parser.parse_args()
Analysis of various combinations - all pass:
> python myscript.py -h usage: myscript.py [-h] [-a] [-b] [-c] My desc optional arguments: -h, --help show this help message and exit -aa help > python myscript.py -a -c > python myscript.py -a -b > python myscript.py -b -c
I tried changing mysub_group to add_mutually_exclusive_group , turning everything into mutually exclusive:
> python myscript.py -h usage: myscript.py [-h] [-a | -b | -c] My desc optional arguments: -h, --help show this help message and exit -aa help -bb help -cc help
How to add arguments to [-a | ([-b] [-c])]?
itzhaki
source share