[issue17218] support title and description in argparse add_mutually_exclusive_group

paul j3 report at bugs.python.org
Wed Apr 16 19:24:54 CEST 2014


paul j3 added the comment:

While mutually exclusive groups are a subclass of argument groups, they have very different uses.

Argument groups are used solely to organize the help section. Groups are not used at all during parsing.  'parse_args' doesn't even pay attention to those 2 default groups (positionals and optionals).  'parse_args' just uses the master list of actions ('parser._actions'). 

Mutually exclusive groups are not used at all while formatting the help lines.  They are used to format the usage line.  They also implement argument tests during parsing.

Groups are not set up for nesting.  However it is possible to define a mutually exclusive group within an argument group, and effectively give it a title and description.

    p=argparse.ArgumentParser()
    g=p.add_argument_group('title')
    g1=g.add_mutually_exclusive_group()
    g1.add_argument('--foo')
    p.print_help()

producing:

    usage: ipython [-h] [--foo FOO]
    optional arguments:
      -h, --help  show this help message and exit
    title:
      --foo FOO

Both kinds of groups are a superficial addition to argparse.  I argue in http://bugs.python.org/issue11588 that they aren't adequate for handling more complicated groupings (inclusive, nesting, etc).

While I'm not convinced the change proposed in this issue is necessary, if we are going implement it, I'd suggest a simple addition to 'add_mutually_exclusive_group()'.  If there's a title argument, add this group to '_action_groups' as well as '_mutually_exclusive_groups'.

    def add_mutually_exclusive_group(self, **kwargs):
        group = _MutuallyExclusiveGroup(self, **kwargs)
        self._mutually_exclusive_groups.append(group)
        try:
            kwargs.title
            self._action_groups.append(group)
        except AttributeError:
            pass
        return group

This should do the job without adding much complexity.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17218>
_______________________________________


More information about the Python-bugs-list mailing list