This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author paul.j3
Recipients bethard, chris.jerdonek, paul.j3, r.david.murray, tati_alchueyr, tshepang
Date 2014-04-16.17:24:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1397669094.27.0.069294280446.issue17218@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2014-04-16 17:24:54paul.j3setrecipients: + paul.j3, bethard, r.david.murray, chris.jerdonek, tshepang, tati_alchueyr
2014-04-16 17:24:54paul.j3setmessageid: <1397669094.27.0.069294280446.issue17218@psf.upfronthosting.co.za>
2014-04-16 17:24:54paul.j3linkissue17218 messages
2014-04-16 17:24:53paul.j3create