Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33861 > unrolled thread

argparse -- mutually exclusive sets of arguments?

Started byRoy Smith <roy@panix.com>
First post2012-11-23 13:46 -0500
Last post2012-11-23 18:07 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  argparse -- mutually exclusive sets of arguments? Roy Smith <roy@panix.com> - 2012-11-23 13:46 -0500
    Re: argparse -- mutually exclusive sets of arguments? Terry Reedy <tjreedy@udel.edu> - 2012-11-23 13:56 -0500
    Re: argparse -- mutually exclusive sets of arguments? Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-23 18:07 -0700

#33861 — argparse -- mutually exclusive sets of arguments?

FromRoy Smith <roy@panix.com>
Date2012-11-23 13:46 -0500
Subjectargparse -- mutually exclusive sets of arguments?
Message-ID<roy-DDAE52.13462523112012@news.panix.com>
My command either takes two positional arguments (in which case, both 
are required):

$ command foo bar

or the name of a config file (in which case, the positional arguments 
are forbidden):

$ command --config file

How can I represent this with argparse; add_mutually_exclusive_group() 
isn't quite the right thing.  It could specify that foo and --config are 
mutually exclusive, but not (as far as I can see) the more complicated 
logic described above.

[toc] | [next] | [standalone]


#33864

FromTerry Reedy <tjreedy@udel.edu>
Date2012-11-23 13:56 -0500
Message-ID<mailman.241.1353697034.29569.python-list@python.org>
In reply to#33861
On 11/23/2012 1:46 PM, Roy Smith wrote:
> My command either takes two positional arguments (in which case, both
> are required):
>
> $ command foo bar
>
> or the name of a config file (in which case, the positional arguments
> are forbidden):
>
> $ command --config file
>
> How can I represent this with argparse; add_mutually_exclusive_group()
> isn't quite the right thing.  It could specify that foo and --config are
> mutually exclusive, but not (as far as I can see) the more complicated
> logic described above.

Make the two positional arguments be one duple?
Or tell argparse that all three are optional and handle the 'more 
complicated logic' in your own code after argparse returns.


-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#33871

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-11-23 18:07 -0700
Message-ID<mailman.248.1353719298.29569.python-list@python.org>
In reply to#33861
On Fri, Nov 23, 2012 at 11:46 AM, Roy Smith <roy@panix.com> wrote:
> My command either takes two positional arguments (in which case, both
> are required):
>
> $ command foo bar
>
> or the name of a config file (in which case, the positional arguments
> are forbidden):
>
> $ command --config file
>
> How can I represent this with argparse; add_mutually_exclusive_group()
> isn't quite the right thing.  It could specify that foo and --config are
> mutually exclusive, but not (as far as I can see) the more complicated
> logic described above.

I don't think you could even do the former.  An argument must be
optional in order to be mutually exclusive with anything.  This works,
however:

    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--config', type=file)
    group.add_argument('--foobar', nargs=2, metavar=('FOO', 'BAR'))
    print parser.parse_args()

Downsides are that the resulting interface is a little more formal and
a little less friendly, and unless you customize the action you'll
wind up with a 2-element 'foobar' arg instead of separate 'foo' and
'bar' args.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web