optparse question (python 2.6)

Andy Kannberg andy.kannberg at gmail.com
Mon Sep 2 10:21:03 EDT 2013


Hi all,

I tried with the example Peter gave me, and it works. But only when the
options are boolean. At least, that is my conclusion with experimenting.
I'll elaborate:

The code to create 'mutually exclusive options':

option_names =  [ "l",  "o" , "s" ]
toggled_options = [name for name in option_names if getattr(opts, name)]
if len(toggled_options) > 1:
  s = repr(toggled_options).strip("[]")
  parser.error("options %s are mutually exclusive" % s)

The options:

parser = optparse.OptionParser()
parser.add_option('-l', help='Show optionset list',  dest='l',
action='store_true'  )
parser.add_option('-o', help='Show content of optionset ', dest='o',
action='store')
parser.add_option('-s', help='Set optionset for a host', dest='s',
action='store' , nargs=2)

The first option, -l, doesn't require an argument
The second option, -o, does require one argument.
The third option, -s does require 2 arguments.

I need to add 4 more options, which all need one or more arguments.

Now, when I run the program with the  options defined as above, everything
works, except, the 'mutually exclusive' part. Because only the -l option is
set to 'true' when selected. When I modify the options to this:

parser = optparse.OptionParser()
parser.add_option('-l', help='Show optionset list',  dest='l',
action='store_true'  )
parser.add_option('-o', help='Show content of optionset ', dest='o',
action='store_true')
parser.add_option('-s', help='Set optionset for a host', dest='s',
action='store_true' , nargs=2)

I get this error:

# ./listopt3.py -o -l
Traceback (most recent call last):
  File "./listopt3.py", line 8, in <module>
    parser.add_option('-s', help='Set optionset for a host', dest='s',
action='store_true' , nargs=2)
  File "/usr/lib64/python2.6/optparse.py", line 1012, in add_option
    option = self.option_class(*args, **kwargs)
  File "/usr/lib64/python2.6/optparse.py", line 577, in __init__
    checker(self)
  File "/usr/lib64/python2.6/optparse.py", line 706, in _check_nargs
    self)
optparse.OptionError: option -s: 'nargs' must not be supplied for action
'store_true'

So, apparanly, using boolean AND arguments isn't allowed.
I'm still learning Python, so if someone can point me in the right
direction would be great!

cheers,
Andy


2013/8/27 Andy Kannberg <andy.kannberg at gmail.com>

> Hello Peter,
>
> Thanks for the example. For now I am restricted to Python 2.6, so no
> argparse for me at the moment.
>
> cheers,
> Andy
>
>
> 2013/8/26 Peter Otten <__peter__ at web.de>
>
>> Andy Kannberg wrote:
>>
>> > Hi python-guru's,
>> >
>> > I am new to Python, coming from a long history of Unix/linux shell
>> > programming.
>> > I am creating a Python script (In Python 2.6)  which should be able to
>> > read command line options and arguments.
>> > So far, I figured out how to do that with optparse. I can add options
>> (and
>> > arguments ) .
>> > There are about 7 options that can be selected.
>> >
>> > However, I can't seem to figure out how to force that only one option is
>> > allowed when the script is invoked. In other words: How to restrict the
>> > script to accept only one of the available options ?
>>
>> You have to do it manually, like in the example
>>
>>
>> http://docs.python.org/2.6/library/optparse.html#how-optparse-handles-errors
>>
>> if options.a and options.b:
>>     parser.error("options -a and -b are mutually exclusive")
>>
>> which could be generalized to (untested)
>>
>> option_names = ["foo", "bar", "baz", ...]
>> toggled_options = [name for name in option_names if getattr(options,
>> name)]
>> if len(toggled_options) > 1:
>>     s = repr(toggled_options).strip("[]")
>>     parser.error("options %s are mutually exclusive" % s)
>>
>> If you are not restricted to the standard library use
>>
>> https://pypi.python.org/pypi/argparse
>>
>> which was added to the stdlib in 2.7 and has "mutually exclusive groups",
>> see
>>
>> http://docs.python.org/2/library/argparse.html#mutual-exclusion
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130902/72f19a3b/attachment.html>


More information about the Python-list mailing list