Help me debug this script with argparse and if statements

Chris Angelico rosuav at gmail.com
Thu Feb 21 05:13:37 EST 2013


On Thu, Feb 21, 2013 at 9:05 PM, Santosh Kumar <sntshkmr60 at 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



More information about the Python-list mailing list