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
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.
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.
-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
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
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?
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.
Become a Member to join the conversation.