I need to have two mutually exclusive groups of arguments in python using ArgumentParser. I use approach suggested by Jonathan here:
subparsers = parser.add_subparsers(help = "You should explicitly specify either group_1 or gropup_2")
parser_g1 = subparsers.add_parser("group_1")
parser_g1.add_argument("group_1_arg1")
parser_g2 = subparsers.add_parser("group_2")
parser_g2.add_argument("group_2_arg1")
parser_g2.add_argument("group_2_arg2")
It looks as correct approach, but the problem is to determinate which group was choosen in runtime.
If first argument was group_1
I get exception assigning args.group_2_arg1
and args.group_2_arg2
.
If first argument was group_2
I get exception assigning args.group_1_arg1
The exception is of kind 'Namespace' object has no attribute 'group_1_arg1'
Is there any way to check which paser group was used, other, then inspecting Namespace? s