In the previous lesson, I helped you write better help. Then I showed you the difference between positional arguments and optional flags. As part of that, you saw the
store_true action to define a Boolean flag. In this lesson, I’ll cover some of the other types of actions that are available to you.
In the previous lesson, you saw me use the
action parameter to change what was stored. There are actually several different actions that you can specify. The
store action sticks the argument or the argument’s parameter into the namespace result.
This is the default, and you don’t have to specify it. The example of changing the action I’ve shown you already was
store_true, changing an option flag into a Boolean If your argument is
store_true, and the option flag is given, then the namespace will contain
As you might guess, there’s a
store_false to go with it. Same idea, but the result is
False if the option flag is given. The
append action stores a list.
You can use it multiple times to keep adding things to that list. If you’re not happy with using
-h for help—for example, you’re writing a Windows program where a question mark is more common—you can replace the option for help with using the
version action shows the program’s version, which of course you have to have set. And then it exits. And finally, the
count action stores an integer containing a count of how many times the flag appears. Some Unix programs support levels of verboseness or debug by specifying the same flag multiple times. For example,
-v for verbose and
-vvv for very, very verbose.
count action will contain
3, respectively, for these two flags. Let’s go play with some of these.
This script is building another Mad Lib, this time based on the Brando flick On the Waterfront. I’m showing off three different kinds of actions here,
version. To use the
version action, the parser needs to know the program’s version.
You indicate that by setting the
version attribute of the parser. Oddly enough, this can’t be sent in as a parameter when constructing the object.
You have to do it as I’ve done here. On lines 7 and 8, I’m using the
append action. This is how I’ll feed the Mad Lib words into the script.
-w is given, another word will be replaced. This is dangerous and stupid, as giving too few will crash the program. But it does show off the
append action, so I’ll stick with dangerous and stupid. Notice that I’m also setting the
help parameter of this argument.
This is even more help information for the script. When the help is invoked, this will provide more descriptive information for this specific argument. Speaking of help, line 9 is the
This will change
help from being
-?. Line 10 is the
version action. Give this, and the program’s version will show and then exit. The rest of the script is just the usual Mad Libs output. Note the use of the list named
word from the
03:19 I’ve opened up a shell …
And there you go.
I could've been somebody. Each use of the
-w flag feeds the
word list, which is then filled into the quote. Now I’ll try some help.
Let me just scroll back a line. And here, instead of using
-h, I used
-?. You can also see the extra help information for the
-w flag, which is what was attached to the argument with the
there’s the version number. Note that the other flags are ignored. Once you use the
version action, all you get is the version printed out, and your script exits.
04:16 Remember how I mentioned this script was dangerous and stupid? Let me show you.
Because the quote expects three items in the
word list, if you don’t give it enough arguments, you’re going to get an
04:33 There are ways of fixing this. I’ll touch on those later.
This script demonstrates the
count action. It takes Jack Nicholson’s famous quote from A Few Good Men and makes it shoutier. On line 6, I use the
count action to define a counting flag.
-s shows up will increment the value of
count. I’ve set a default here so that I always get an integer. Remember that unless otherwise specified, empty arguments are
Line 7 takes the shouty court quote and splits it up into a list on word boundaries. Inside of the
for loop, each word is processed. If the enumeration index named
count is less than the
shout counter, then the word is printed in uppercase.
If you haven’t seen it before,the
end parameter to
print() indicates what to put after the string when printing it out. It defaults to a newline. By setting it to empty (
''), like I’ve done here, this
print() will print the string without a newline character at the end.
shout counter has been surpassed, I print the word out without the call to
.upper(). Finally, because there hasn’t been a newline yet, I call
print() on its own so that a newline gets spit out. Can you handle the truth?
-s flag, only the base sentence is written. This isn’t very Jack, though, is it?
With a single
-s, the first word gets shout-cased. Still not very Jack.
I’ve covered some of the more common actions, but there are a few more you might find useful. Personally. I’ve never used any of these, but you never know.
store_const is kind of like
store_true, but instead of storing a Boolean, it stores some constant that you give it.
This is kind of a shortcut. If you’re finding that you were going to use
store_true and then set a variable based on that flag, you could skip the extra step and use
store_constant instead. What if you want multiple constants? Well,
append_constant has your back. Like
append, it stores a list, but instead of taking a parameter, it just stores the given constant in the list each time the flag is given.
New in Python 3.8 is the
extend action. This is very similar to
append, but it supports multiple parameters. With
append, you only get a single parameter each time you give the flag, and that parameter gets put in the list. With
extend, you can have multiple parameters to the flag, and the whole chunk gets put in the resulting list. And that’s not a bad segue.
07:33 Next up, how to consume multiple positional arguments or handle multiple parameters to your optional flags.
Become a Member to join the conversation.