optparse question (python 2.6)

2013-08-26 Thread Andy Kannberg
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 ?

cheers,
Andy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New VPS Provider needed

2013-08-27 Thread Andy Kannberg
Niko,

No offense, but I have to agree with David. This list is about Python, not
about webprogramming nor hosting.
If you are in need of a mailing group for such, again, Google or any other
search engine can help you with that.

best regards,
Andy


2013/8/27 Νικόλαος 

> Στις 27/8/2013 2:18 μμ, ο/η David έγραψε:
>
>> On 27 August 2013 17:13,   wrote:
>>
>>>
>>> I know this isn't the place to ask
>>>
>>
>>  is 100% correct about this.
>>
>>
>> So, this list is also not the correct place to answer.
>> So please everyone, do not respond. Thanks.
>>
>>
> Actually it is.
> Poeple are web programmers here and lots of them doing hosting.
> You dont like my questions, block me, but just stop answering unhelpfully.
>
>
> --
> What is now proved was at first only imagined!
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: optparse question (python 2.6)

2013-09-02 Thread Andy Kannberg
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 
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 

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