Subparsers support all the methods a normal parser supports, including add_mutually_exclusive_group()
:
>>> megroup = t1sub.add_mutually_exclusive_group()
>>> megroup.add_argument('--in', action='store_true')
_StoreTrueAction(option_strings=['--in'], dest='in', nargs=0, const=True, default=False, type=None, choices=None, help=None, metavar=None)
>>> megroup.add_argument('--out', action='store_true')
_StoreTrueAction(option_strings=['--out'], dest='out', nargs=0, const=True, default=False, type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args(['task1', '--in'])
Namespace(func=<function task1 at 0x10a0d9050>, in=True, out=False)
>>> t1sub.print_help()
usage: task1 [-h] [--in | --out]
optional arguments:
-h, --help show this help message and exit
--in
--out
>>> parser.print_help()
usage: [-h] {task1,task2} ...
How can I have mutually exclusive groups in subparsers?
positional arguments:
{task1,task2}
optional arguments:
-h, --help show this help message and exit