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

References <CAE7MaQa=SuuD1SyS897+T8U9gxbSBnrusD7un6sNKtyaiHvd-w@mail.gmail.com>
Date 2013-02-21 21:13 +1100
Subject Re: Help me debug this script with argparse and if statements
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2153.1361441619.2939.python-list@python.org> (permalink)

Show all headers | 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