Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53010
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'arguments': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:question': 0.10; 'python': 0.11; '2.7': 0.14; '[name': 0.16; 'andy': 0.16; 'invoked.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'selected.': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'library': 0.18; 'command': 0.22; 'example': 0.22; '(in': 0.22; 'shell': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'unix/linux': 0.24; 'options': 0.25; 'script': 0.25; 'header:X -Complaints-To:1': 0.27; 'programming.': 0.30; 'restrict': 0.30; 'restricted': 0.31; 'figure': 0.32; 'option': 0.32; 'url:python': 0.33; 'could': 0.34; 'subject: (': 0.35; "can't": 0.35; 'add': 0.35; 'there': 0.35; 'url:org': 0.36; 'should': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'new': 0.61; 'name': 0.63; 'skip:r 30': 0.69; 'exclusive': 0.81 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: optparse question (python 2.6) |
| Date | Mon, 26 Aug 2013 19:40:05 +0200 |
| Organization | None |
| References | <CADDfdi7S0+9km=SiQ4ZZ9q3YFj0DxFKbRdPFf5bKz44E_rykdg@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084935e.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| 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.243.1377538796.19984.python-list@python.org> (permalink) |
| Lines | 40 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1377538796 news.xs4all.nl 15944 [2001:888:2000:d::a6]:59300 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:53010 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: optparse question (python 2.6) Peter Otten <__peter__@web.de> - 2013-08-26 19:40 +0200
csiph-web