Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Available Actions

00:00 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.

00:18 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.

00:33 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 True.

00:50 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.

01:00 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 help action.

01:18 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.

01:46 The count action will contain 1 and 3, respectively, for these two flags. Let’s go play with some of these.

01:55 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, append, help, and version. To use the version action, the parser needs to know the program’s version.

02:10 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.

02:18 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.

02:27 Each time -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.

02:45 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 help action.

02:59 This will change help from being -h to -?. 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 append action.

03:19 I’ve opened up a shell …

03:29 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.

03:43 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 help parameter.

03:59 And finally,

04:06 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.

04:26 Because the quote expects three items in the word list, if you don’t give it enough arguments, you’re going to get an IndexError.

04:33 There are ways of fixing this. I’ll touch on those later.

04:40 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.

04:54 Each time -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 None.

05:08 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.

05:26 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.

05:43 Once the 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?

06:02 Without the -s flag, only the base sentence is written. This isn’t very Jack, though, is it?

06:12 With a single -s, the first word gets shout-cased. Still not very Jack.

06:21 Now that’s better.

06:26 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.

06:41 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.

07:06 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.