Less Common Features
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.
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
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.
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.
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.
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.
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.
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.
--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
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
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.
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.
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.