Python argparse command line flags without arguments

How do I add an optional flag to my command line args?

eg. so I can write

python myprog.py 

or

python myprog.py -w

I tried

parser.add_argument('-w')

But I just get an error message saying

Usage [-w W]
error: argument -w: expected one argument

which I take it means that it wants an argument value for the -w option. What’s the way of just accepting a flag?

I’m finding http://docs.python.org/library/argparse.html rather opaque on this question.

4 Answers
4

Leave a Comment