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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Less Common Features

00:00 In the previous lesson, I covered how to control the number of parameters given to an argument. In this lesson, I’m going to cover a mix of smaller things you might find useful.

00:11 This lesson is going to cover three quick, unrelated topics. First, how to restrict what values are acceptable to the parser. Second, how to make flags mutually exclusive. And third, how to read your arguments from a file.

00:27 Sometimes you want your parameters to be from a given set. Enter the choices parameter. You can use it both for positional arguments and optional flags. Here, I’ve set it for a positional argument that can only contain either "box" or "carton".

00:44 You can also use it with optional flags. choices takes an iterable. So instead of writing out 1 through 5, you can use the .range() method, like I have done here.

00:55 I’ve also introduced another small thing, the required parameter. Normally, optional flags are, well, optional. Adding the required flag makes them mandatory. Run, Forrest, run!

01:12 Nothing surprising here …

01:18 or here. If I miss the required flag, it complains …

01:30 and same if I use something besides "box" or "carton". Mmm, truckloads of chocolate. This next miscellaneous bit is mutually exclusive arguments.

01:47 Using the parser’s .add_mutually_exclusive_group() method, you get a group object, which you can then configure. In this case, I want to force the user to give exactly one of the two choices, so I set the group to be required.

02:00 You then add arguments to the group the same way you would add an argument to your parser. The exact same argument features are supported. My first argument is a Boolean named --light, and my second is a Boolean named --dark. What droids? I don’t see any droids.

02:21 There’s the light side. There’s the dark side.

02:33 And there’s the angry side. Got to pick one. You’ve got to pick one.

02:43 And there you go, required mutually exclusive arguments. If you’ve got a lot of positional arguments that you use over and over again, argparse has a feature that can shorten the amount of typing you have to do.

02:59 You can specify positional arguments in a file. To do this, you use the fromfile_prefix_chars parameter. What this does is allow you to give an argument that starts with the @ character in this case and contains the name of a file.

03:15 argparse will then read that file and use each line of that file as a positional argument. On line 4, I’m also setting another parameter. This is the allow_abbrev value. Setting this to False disallows the use of abbreviations in optional flags.

03:34 I’ll explain that more in a second. To set up the example, the snowwhite script takes four positional arguments and then a Boolean flag. The script is yet another Mad Libs example with the optional feature of turning it into shoutcase. Before I show you snowwhite, let me step back to starwars for a moment.

04:02 Remember this? It was long ago, galaxy far away. argparse supports the use of shortened optional flags. You don’t have to type the whole thing.

04:15 See? --li is good enough. In fact, as long as it doesn’t cause ambiguity, you can take it down to one letter. But what, you might say, if I don’t want this feature? Well, then set allow_abbrev to False.

04:34 Can I just say shortening abbreviation to abbrev just seems like the right thing to do? Bravo, Python core team. Bravo. All right, here’s snowwhite.

05:00 This time I attempted to shorten --upper, but because of the allow_abbrev flag, it was disallowed. On to fromfile_prefix. In the background, I’ve created a file called madlib_snow. Here it is: four lines for our four arguments. To use this, you specify the name of the file, preceeded by the fromfile_prefix_chars character, which in this case was the @ sign.

05:34 And there you go. All the positional arguments without all the typing. A couple more quick customizations. These are done through additional parameters to the ArgumentParser initializer when you create the object.

05:50 If for some reason you don’t want your program to support the help screen, you can turn it off by using add_help=False. It is completely unclear to me why anyone would want to do this, but you can.

06:04 This one makes a bit more sense to me. You can also modify what character is used for your optional flags. It’s normally the - (dash), but you can change it to something else.

06:13 Windows programs often use / (slash) as their options character, so if you’re writing code for Windows, this is a useful thing. Next up, a couple more advanced topics: callable types and custom actions.

Become a Member to join the conversation.