Message395959
I have found that the only thing argparse is actually missing is to forward the actions to the generated mutually exclusive group in _add_container_actions method.
class SubArgumentParserFixedMutExtGroups(argparse.ArgumentParser):
def _add_container_actions(self, container):
groups = container._mutually_exclusive_groups
container._mutually_exclusive_groups = [] # temporary override just so it is not processed
super(SubArgumentParserFixedMutexGroups, self)._add_container_actions(container)
# same as original loop, but with extra append of actions in created mutex
for group in groups:
mutex_group = self.add_mutually_exclusive_group(required=group.required)
for action in group._group_actions:
mutex_group._group_actions.append(action)
When printing help, this resolves correctly.
The actions can be found because they are already added when parsing the group of the parent parser that contains the mutex.
Snippet below.
# same as other comment examples
parser = argparse.ArgumentParser()
parser_group = parser.add_argument_group("INPUT OPTIONS")
parser_group_mutually_exclusive = parser_group.add_mutually_exclusive_group(required=False)
parser_group_mutually_exclusive.add_argument("--from-args")
parser_group_mutually_exclusive.add_argument("--from-files")
parser_group_mutually_exclusive.add_argument("--from-stdin")
parser_group.add_argument("-0", help="null delimited pathnames")
parser.print_help()
usage: pydevconsole.py [-h]
[--from-args FROM_ARGS | --from-files FROM_FILES | --from-stdin FROM_STDIN]
[-0 0]
optional arguments:
-h, --help show this help message and exit
INPUT OPTIONS:
--from-args FROM_ARGS
--from-files FROM_FILES
--from-stdin FROM_STDIN
-0 0 null delimited pathnames
# now add the subparser with tweaked ArgumentParser
parent = SubArgumentParserFixedMutexGroups()
subpar = parent.add_subparsers()
subpar.add_parser("test", add_help=False, parents=[parser])
parent.parse_args(["test", "--help"])
usage: pydevconsole.py test [-h]
[--from-args FROM_ARGS | --from-files FROM_FILES | --from-stdin FROM_STDIN]
[-0 0]
optional arguments:
-h, --help show this help message and exit
INPUT OPTIONS:
--from-args FROM_ARGS
--from-files FROM_FILES
--from-stdin FROM_STDIN
-0 0 null delimited pathnames |
|
Date |
User |
Action |
Args |
2021-06-16 20:49:44 | fmigneault | set | recipients:
+ fmigneault, rhettinger, paul.j3, calestyo |
2021-06-16 20:49:44 | fmigneault | set | messageid: <1623876584.49.0.650656767392.issue43259@roundup.psfhosted.org> |
2021-06-16 20:49:44 | fmigneault | link | issue43259 messages |
2021-06-16 20:49:44 | fmigneault | create | |
|