Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   argparse -- mutually exclusive sets of arguments? (http://www.velocityreviews.com/forums/t954794-argparse-mutually-exclusive-sets-of-arguments.html)

Roy Smith 11-23-2012 06:46 PM

argparse -- mutually exclusive sets of arguments?
 
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.

Terry Reedy 11-23-2012 06:56 PM

Re: argparse -- mutually exclusive sets of arguments?
 
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


Ian Kelly 11-24-2012 01:07 AM

Re: argparse -- mutually exclusive sets of arguments?
 
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.


All times are GMT. The time now is 04:01 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.