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


Groups > comp.lang.python > #39411

Re: Help me debug this script with argparse and if statements

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'args': 0.04; '21,': 0.07; 'arg,': 0.09; 'arguments,': 0.09; 'subject:script': 0.09; 'subject:Help': 0.10; "wouldn't": 0.11; 'options.': 0.15; '-- help': 0.16; 'argparse': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:argparse': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'feb': 0.19; 'bit': 0.21; "i'd": 0.22; 'second': 0.24; 'script': 0.24; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'kumar': 0.29; 'things,': 0.29; 'keyword': 0.30; 'expect': 0.31; 'running': 0.32; 'goes': 0.33; 'strict': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'whatever': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'subject:with': 0.36; 'skip:p 20': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'there,': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; "you've": 0.61; "you'll": 0.62; 'provide': 0.62; 'become': 0.65; 'subject:this': 0.84; '2013': 0.84; "it'd": 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=fvxk/e9EWkU3zDjGgf5kalfQ9vsRbR2Zi0b1XLV/9zQ=; b=mw/hQwC93Sr44TO3JkjSAAzx+Ols75BPcZS7xOU59wykqqAHpSXMcotdVapvhh2vsW +GYjo4ogWYKDOsgMboo12wDM+x/i+C8Ynds293jQNuw/NvAEIGBEVjxiSF1/ODJ1Dkto Kzihk808axxfLbzoxdVuhso3g/lQpuDWvzAZj6b/k3XURtsM9Cut1W9wb7sxAZDLa5CE RxvyrhN6dvjD6K9v3Qt+70N4f2IwHq7Z2mwEUR5zpz/NF1YnaxblZhzGtW8Sgq/zoa3e u2lXiZ1eFBaJQA4YyXSa3AtzxOJdE9hd+d6upk+xcyGEH6x41bTKA3UqoMN2Wya10EB+ LXSw==
MIME-Version 1.0
X-Received by 10.52.67.164 with SMTP id o4mr26088225vdt.42.1361441617267; Thu, 21 Feb 2013 02:13:37 -0800 (PST)
In-Reply-To <CAE7MaQa=SuuD1SyS897+T8U9gxbSBnrusD7un6sNKtyaiHvd-w@mail.gmail.com>
References <CAE7MaQa=SuuD1SyS897+T8U9gxbSBnrusD7un6sNKtyaiHvd-w@mail.gmail.com>
Date Thu, 21 Feb 2013 21:13:37 +1100
Subject Re: Help me debug this script with argparse and if statements
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.2153.1361441619.2939.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361441619 news.xs4all.nl 6842 [2001:888:2000:d::a6]:55896
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39411

Show key headers only | View raw


On Thu, Feb 21, 2013 at 9:05 PM, Santosh Kumar <sntshkmr60@gmail.com> wrote:
> parser.add_argument(
>     'install',
>     nargs='?',
>     help='install myapp'
>     )
>
> parser.add_argument(
>     'uninstall',
>     nargs='?',
>     help='uninstall myapp'
>     )
>
> args = parser.parse_args()

What you've done is make your program expect arguments, not options.
Try running your script --help and you'll see how it parses. Whatever
keyword is given goes into args.install, and if you provide a second
arg, it'll become args.uninstall.

To do what you're looking for there, I wouldn't bother with argparse
at all - I'd just look at sys.argv[1] for the word you're looking for.
Yes, it'd be a bit strict and simplistic, but by the look of things,
you don't need sophistication.

ChrisA

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


Thread

Re: Help me debug this script with argparse and if statements Chris Angelico <rosuav@gmail.com> - 2013-02-21 21:13 +1100

csiph-web