Help and Positional vs Optional Arguments
The more arguments your program takes, the more confusing it can get. Go Google all the valid arguments for the
ls command on Unix. Confusing is an understatement. Thankfully,
argparse provides a mechanism for writing help.
Inspired by the Matrix movie—there was only one, I’ll fight you—this program prints out columns of random characters so that I feel cooler when programming wearing sunglasses. On line 4, I’m using the
string library to string together—see what I did there?—a bunch of characters.
string library defines some constants that are a subset of the ASCII table. Here, I’m creating a single string comprised of all of the upper and lowercase letters, the numbers, and some common punctuation.
ArgumentParser class can be configured to change the behavior of the parser. Here, I’m going to show you some parameters that affect the help message. This first parameter I’m passing in is
Recall the previous lesson where I was arguing with Hal. If I didn’t put in the right command line arguments, I got a
usage statement. On line 8, I’m overriding the default one, inserting an appropriate Keanu-ism into the beginning of the string. Note the use of the
%(prog) variable indicator.
I’m doing that twenty times in a list comprehension in order to get twenty columns of output. On line 18, I print the columns by joining the content together, putting some space in between. All of this will be done
args.num times. Taking the blue pill, or is it the red one?
I kind of glossed over this in the previous example.
argparse handles both positional and optional arguments. The number of rows in the
matrix script was a positional argument. In fact, because I didn’t say otherwise, it was a required positional argument.
This is known as an optional argument and indicated by flags. In most Unix programs, it is common to support both single (
-) and double (
--) dash flags, where the double dash is a human readable word, and the single dash is a short form, typically a single letter.
-h isn’t your only choice of an option.
You can define your own flags. Let’s go do that. You’ll find it doesn’t amount to a hill of beans. Wait for it. That’ll make sense in a second. Or of course, if you’re not into the silver screen, that beans thing is going to seem rather abstract. Anyhow, the
casablanca script shows off the use of optional flags.
I’ve done both here, which means the user can specify either
--place as their argument, because I haven’t specified otherwise, this flag expects an argument to go with it. In this case, it’s going to store whatever the user gave to the first flag inside of the
args.place namespace item.
Line 8 is like line 6, but for a verb. And line 9 is a little different. It uses the
action parameter to change what happens when this argument is used. Passing
argparse to change its behavior in a couple of ways. First, what gets stored in
-r is a Boolean. Second, it does not expect a parameter to go with the flag. This flag is a pure flag.
It is either there or not. Like in line 7, I provided a default value here. Also, in this case, there’s no double-dash version. Having both kinds of flags is good practice, but
argparse lets you do whatever you want. Scrolling down …
Lines 13 and 14 are where I print out the results. I do this by accessing the optional flags the same way I accessed the positional ones. If you have both a single- and a double-dash argument,
argparse will store it using the long-form name and, in either case, without the dash or dashes. You can control this, but I’ll show you how to do that later. On line 16, I check for the
-r flag, which in this case is short for
Unlike with required positional arguments, the program doesn’t spit out an error message. The default value for any argument in
None unless you specify otherwise. You specify otherwise by using the
default parameter, like I did with
False for the
-r flags. Without the flags, I end up with
Of all the None in all the
towns in all the world, she None into mine. It’s a very Pythonesque Mad Lib. Let’s look at the help.
That time, instead of using
-h, I used
--help, the long form. And
argparse shows all the possible flags. It indicates which takes a parameter, shows you the names of the parameter and the flag. Later, I’ll show you how to make this prettier. Maybe not Bogart-pretty but prettier nonetheless.
I showed you the
store_true action in this lesson. There are actually several others, as well, that allow you to control the arguments. Actions shout louder than words. Again with the oblique foreshadowing!
Become a Member to join the conversation.