Message355370
argparse allows adding argument_groups inside of mutually_exclusive_groups, but the behavior is unintuitive and a bit buggy.
Demo:
import argparse
parser = argparse.ArgumentParser()
single_group = parser.add_argument_group(title='single_group')
single_group.add_argument('--a', action='store_true')
mutex_group = parser.add_mutually_exclusive_group()
mutex_group.add_argument('--b', action='store_true')
nested_group = mutex_group.add_argument_group(title='nested_group')
nested_group.add_argument('--c', action='store_true')
nested_group.add_argument('--d', action='store_true')
parser.print_help()
print(parser.parse_args())
Example output:
$ ~/test_args.py --a --b --c --d
usage: test_args.py [-h] [--a] [--b] [--c] [--d]
optional arguments:
-h, --help show this help message and exit
--b
single_group:
--a
Namespace(a=True, b=True, c=True, d=True)
Two issues I've noticed with this:
- Arguments in the nested group show up in the usage string, but not in the help text. The nested arguments are still parsed and available in the result, as normal.
- Arguments in the nested group are not mutually exclusive with any arguments in the containing mutually_exclusive_group.
- I would expect:
--b ok
--c ok
--d ok
--b --c error
--b --d error
--c --d error*
--b --c --d error
*From a design perspective, it seems like argument_groups are meant to control display, and mutually_exclusive_groups are meant to control logic, so I think "error" makes sense here. I personally would like the ability to allow "--c --d", but I think that's a separate discussion and probably a new type of group. |
|
Date |
User |
Action |
Args |
2019-10-25 18:13:04 | Tim Sanders | set | recipients:
+ Tim Sanders |
2019-10-25 18:13:04 | Tim Sanders | set | messageid: <1572027184.79.0.23480523249.issue38590@roundup.psfhosted.org> |
2019-10-25 18:13:04 | Tim Sanders | link | issue38590 messages |
2019-10-25 18:13:04 | Tim Sanders | create | |
|