Re: argparse -- mutually exclusive sets of arguments?

2012-11-23 Thread Ian Kelly
On Fri, Nov 23, 2012 at 11:46 AM, 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.

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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: argparse -- mutually exclusive sets of arguments?

2012-11-23 Thread Joshua Landau
On 23 November 2012 18:46, 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.


Do you need to use argparse?

If not, I've been recommending docopt due to its power and simplicity:

-START -
"""
Command.

Usage:
command  
command --config=

Options:
foo  The egg that spams
bar  The spam that eggs
--config=  The config that configures
"""

from docopt import docopt

if __name__ == '__main__':
arguments = docopt(__doc__)
print(arguments)
- END 

- USAGE -
%~> python simple_docopt.py foobar barfoo
{'--config': None,
 '': 'barfoo',
 '': 'foobar'}
%~> python simple_docopt.py foobar
Usage:
simple_docopt.py  
simple_docopt.py --config=
%~> python simple_docopt.py --config=turtle.conf
{'--config': 'turtle.conf',
 '': None,
 '': None}
%~> python simple_docopt.py --config=turtle.conf not allowed
Usage:
simple_docopt.py  
simple_docopt.py --config=
--- END USAGE ---
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: argparse -- mutually exclusive sets of arguments?

2012-11-23 Thread Terry Reedy

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

--
http://mail.python.org/mailman/listinfo/python-list


argparse -- mutually exclusive sets of arguments?

2012-11-23 Thread Roy Smith
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.
-- 
http://mail.python.org/mailman/listinfo/python-list