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


Groups > comp.lang.python > #53484

Re: optparse question (python 2.6)

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <andy.kannberg@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'url:pypi': 0.03; 'argument': 0.05; 'allowed.': 0.07; 'error:': 0.07; 'modify': 0.07; 'parser': 0.07; '__init__': 0.09; 'arguments': 0.09; 'option,': 0.09; 'works.': 0.09; 'subject:question': 0.10; 'python': 0.11; '2.7': 0.14; '"o"': 0.16; '"s"': 0.16; '**kwargs)': 0.16; '-o,': 0.16; '[name': 0.16; 'andy': 0.16; 'argument.': 0.16; 'boolean': 0.16; 'great!': 0.16; 'invoked.': 0.16; 'selected.': 0.16; 'supplied': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'library': 0.18; 'all,': 0.19; 'command': 0.22; 'example': 0.22; '(in': 0.22; 'email addr:gmail.com&gt;': 0.22; 'shell': 0.22; 'creating': 0.23; 'example.': 0.24; 'skip:\xa0 20': 0.24; 'unix/linux': 0.24; 'cheers,': 0.24; 'options': 0.25; 'script': 0.25; '&gt;': 0.26; 'skip:" 30': 0.26; 'this:': 0.26; 'second': 0.26; 'defined': 0.27; 'tried': 0.27; 'point': 0.28; "doesn't": 0.30; 'direction': 0.30; 'programming.': 0.30; 'restrict': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; '8bit%:2': 0.31; 'restricted': 0.31; 'file': 0.32; 'figure': 0.32; 'option': 0.32; 'run': 0.32; 'url:python': 0.33; '(most': 0.33; 'third': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'subject: (': 0.35; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'options:': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; "i'll": 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'skip:o 20': 0.38; 'skip:& 10': 0.38; '8bit%:4': 0.38; 'url:library': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'does': 0.39; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'read': 0.60; 'above,': 0.60; 'new': 0.61; 'first': 0.61; 'name': 0.63; 'more': 0.64; 'skip:r 40': 0.68; 'skip:r 30': 0.69; 'exclusive': 0.81; "'true'": 0.84; 'otten': 0.84; 'peter,': 0.84; 'skip:] 10': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=FRb68uFUFbanc56T4xMFpDVOciIs1Jz+C6RaUZSUeQk=; b=Sj8eY+XS+aVKAmFZhnxrSuZ3Cw3Jc1fYhNaFzasQFkUjY05/blflj/4r10F0uJQmEV /x4tjY/UmtiOCiV2rIeTqGpK4N28yl4Smi4TNuld4MyM1RkOVfChTFmVxFibEy61Uo8O izQpV9xJFT+1fJVz/+DViwokUET/pxiRdMrvSTuvah7FXNdDVYz2+pNJnimEqDqOgGcx URL2Tuf3tpN4BHk95mvEylMJop1PC+0Rq1TPlnONK4HmWV++xa0M+NUmCKBpEuOoNI0w ncjDYtp133o+9QFqQWuX5h7AoIGHt0nkKegvlz5u7jqJpd6z/pKIL7Mftm935xBKBgqQ 3uiQ==
X-Received by 10.66.186.12 with SMTP id fg12mr2611323pac.178.1378131685056; Mon, 02 Sep 2013 07:21:25 -0700 (PDT)
MIME-Version 1.0
From Andy Kannberg <andy.kannberg@gmail.com>
Date Mon, 2 Sep 2013 16:21:03 +0200
Subject Re: optparse question (python 2.6)
To python-list@python.org
Content-Type multipart/alternative; boundary=047d7bf0e4268aae5404e567497a
X-Mailman-Approved-At Mon, 02 Sep 2013 16:26:53 +0200
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.489.1378132014.19984.python-list@python.org> (permalink)
Lines 280
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1378132014 news.xs4all.nl 15926 [2001:888:2000:d::a6]:41305
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53484

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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@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__@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
>>
>
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: optparse question (python 2.6) Andy Kannberg <andy.kannberg@gmail.com> - 2013-09-02 16:21 +0200

csiph-web