zenlotus / argparse

Automatically exported from code.google.com/p/argparse
Other
0 stars 0 forks source link

Allow title and description arguments for add_mutually_exclusive_group #58

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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.)

Original issue reported on code.google.com by chphilli on 31 Jan 2010 at 10:22

GoogleCodeExporter commented 9 years ago
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.

Original comment by steven.b...@gmail.com on 4 Feb 2010 at 5:48

GoogleCodeExporter commented 9 years ago
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.

Original comment by steven.b...@gmail.com on 1 Mar 2010 at 7:11

GoogleCodeExporter commented 9 years ago
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.

Original comment by steven.b...@gmail.com on 23 Jul 2010 at 12:50

GoogleCodeExporter commented 9 years ago
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
=======================================================================

Original comment by kew...@gmail.com on 23 Jul 2010 at 2:20

GoogleCodeExporter commented 9 years ago
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!

Original comment by steven.b...@gmail.com on 23 Jul 2010 at 2:38

GoogleCodeExporter commented 9 years ago
Hi Steven,

I am sorry to comment on something closed for so long.
http://bugs.python.org/issue10680 is resolved in python 2.7
Can we please port the fix here so it is available for python <= 2.6?

Thank you.

Original comment by damien.n...@gmail.com on 3 Apr 2013 at 5:18