New issue 58 by chphilli: Allow title and description arguments for
add_mutually_exclusive_group
http://code.google.com/p/argparse/issues/detail?id=58
I've been using argparse for a while now, and find that the mutually
exclusive groups are a great idea, but for one lack: I can't take advantage
of argparse's nice group formatting for these groups!
Out of curiosity, I decided to try to make the change myself, and found
that it *seemed* to be relatively simple:
1. Accept "title" and "description" as keyword arguments to
add_mutually_exclusive_group
2. Pass the "title" and "description" as keyword arguments to the super()
call in _MutuallyExclusiveGroup.__init__
3. Add the new group to the parser's action groups in
add_mutually_exclusive_group (e.g., self._action_groups.append(group))
Since this only took about a minute for me to figure out, I feel like there
must be something that I'm missing. If not, could this functionality
be added? If so, what needs to be changed.
(Another thing that makes me curious is that I can't find any requests for
this feature via Google searches... It was the first thing I wanted
after I tried using a mutually exclusive group, so I'm confused as to why
it doesn't appear to be discussed anywhere.)
--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings
I think that's basically right - the only thing that really needs to be
done is to
add the arguments to the _MutuallyExclusiveGroup __init__ and then pass
them on the
super() call.
As to why no one else has asked for this, I don't have a good answer. ;-)
I'll get this incorporated into the next version of argparse. If you have
the time to
write a test or two for this behavior and then send me a patch with the
change and
the tests, that would be great.
Comment #2 on issue 58 by steven.bethard: Allow title and description
arguments for add_mutually_exclusive_group
http://code.google.com/p/argparse/issues/detail?id=58
So, I worked on this for a while today, and while I still believe it's
possible, the
suggested change above doesn't work because it breaks the formatting of
regular
groups. If you have a patch that adds this functionality and doesn't break
the
current tests, I'd be happy to look at it.
Comment #3 on issue 58 by steven.bethard: Allow title and description
arguments for add_mutually_exclusive_group
http://code.google.com/p/argparse/issues/detail?id=58
I'm going to close this as WontFix. There's a pretty straightforward
alternative which is to create a group with a title and description, and
add the mutually exclusive group to that. If you'd like to implement a
patch for the simplified version, please open a new ticket with a patch at
bugs.python.org.
Workaround described in comment #3 doesn't seem to support "required=True"
parameter. For example:
=======================================================================
$ cat t.py
#!/build/toolchain/lin32/python-2.6.1/bin/python
import argparse
parser = argparse.ArgumentParser('test')
group = parser.add_argument_group('Verbosities', 'Controls level of output')
mutex_group = group.add_mutually_exclusive_group(required=True)
mutex_group.add_argument('-q', action='store_true', default=False,
help='Quiet mode')
mutex_group.add_argument('-v', action='store_true', default=False,
help='Verbose mode')
args = parser.parse_args()
print 'q=%s v=%s' % (args.q, args.v)
$ ./t.py
q=False v=False
=======================================================================
I would have expected either -q or -v to have been required, i.e. this
behavior:
=======================================================================
$ ./t.py
usage: test [-h] (-q | -v)
test: error: one of the arguments -q -v is required
=======================================================================
Well that looks like a bug. Could you please file a bug on bugs.python.org?
This bug tracker is no longer in use. Thanks!