[issue43259] argparse: allow add_mutually_exclusive_group on add_argument_group

Francis Charette Migneault report at bugs.python.org
Wed Jun 16 16:49:44 EDT 2021


Francis Charette Migneault <francis.charette.migneault at gmail.com> added the comment:

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

----------
nosy: +fmigneault

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43259>
_______________________________________


More information about the Python-bugs-list mailing list