mutually exclusive group inside argument group

994 views
Skip to first unread message

Michael Elsdörfer

unread,
Dec 10, 2009, 3:55:22 AM12/10/09
to argparse-users
It doesn't seem possible. I am using version 1.0.1.

My code is basically this:

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
cmd_parser = subparsers.add_parser('import')
group = cmd_parser.add_argument_group('command arguments')
mutex = group.add_mutually_exclusive_group()
mutex.add_argument('--initial')
mutex.add_argument('--overwrite')

It runs, but the add_mutually_exclusive_group() call doesn't seem to
have any effect. If you remove the argument group step and add the
mutex directly to the subparser, it works as expected.

Am I doing something wrong? Is it not possible to combine
add_argument_group() and add_mutually_exclusive_group()?

Steven Bethard

unread,
Dec 10, 2009, 11:52:50 AM12/10/09
to argpars...@googlegroups.com
This is not currently supported. Please file a bug report though -
either it needs to be fixed so that it works, or it should at least
raise an exception rather than passing silently.

Of course, patches that make this work are also welcome. :-)

Steve
--
Where did you get that preposterous hypothesis?
Did Steve tell you that?
--- The Hiphopopotamus
Message has been deleted

Steven Bethard

unread,
Dec 21, 2010, 5:51:34 AM12/21/10
to argpars...@googlegroups.com
On Tue, Dec 21, 2010 at 1:07 AM, J <j...@recessnetworks.net> wrote:
> python's issue tracker is failing for me (it's not getting my registration
> email to my hotmail account).  in any case, here's a patch that should make
> this behavior work.

Thanks for the report. I did get your longer one too. Someone opened a
similar issue on the bug tracker:

http://bugs.python.org/issue10680

The fix seems to be a one line fix - see the patch attached to the
tracker. The patch still needs tests, etc. before it can be committed,
but if you want to fix your local copy you can see how from there.

J

unread,
Dec 21, 2010, 7:21:59 PM12/21/10
to argparse-users
that patch is much more succinct and achieves the same end result (the
patch i proposed overrode the add_mutually_exclusive_group method on
the _ArgumentGroup to append the mutex group on the container).

i tested the one-liner patch for my cases and it works fine. thanks!
Reply all
Reply to author
Forward
0 new messages