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.

The Basics of argparse

00:00 In the previous lesson, I gave you an overview of the course. In this lesson, I’ll show you the basics of the argparse library. argparse is a powerful library for managing command line arguments to your Python programs. It’s part of the standard library, so there’s nothing extra to install. You do have to do a bit of work, though.

00:21 There are five basic steps. You need to import argparse, create a parser, add arguments to the parser, and once you’ve got that all configured, you call the parser’s .parse_args() method, which does exactly what it says. It parses the command line and returns to you a namespace object. That namespace object contains flags and data of what arguments were used.

00:47 You use this object to control the execution of your script. Let’s go see some examples. I’m going to start without argparse so that you can see the difference.

00:58 The script in the top window here takes a couple of command line parameters and prints out a statement a number of times. To get at what was passed in on the command line, you need the sys module. More specifically, you need the argv part of the sys module. Note that I’m using the second item in this list. The first item contains the name of the calling program.

01:20 This may seem strange, but it is to be consistent with the way things have been done in Unix programs since the dawn of time. Well, not quite that old, but near enough for our purposes.

01:32 I’m storing the first argument from sys.argv in the name variable. Then the second argument is being converted to an integer and stored in num.

01:42 As I mentioned before, everything off the command line is a string, unless you do something to change that, like I have here. Lines 8 and 9 use these two variables to print out Hal’s protesting statement to name, and it does that num times.

01:58 Let’s run this … passing in Dave and 3 gives the famous 2001 movie quote three times over. Let’s try it again. This time, I’m going to forget something.

02:13 Well, that’s not terribly friendly. A better-written program would have checked the length of a argv and printed out a message if it was too long or too short. Let me try something else.

02:28 Right number of arguments, but the wrong type. Spelling out Three is no good. It blows up the call on line 6. Again, you could write some code that checks this or handles it better, or you could use argparse. It slices, it dices, it makes short work of tomatoes—oh, and it parses the command line too.

02:50 This new program is the argparse equivalent of what you just saw. A couple of lines longer, but it has way more error-proofing than the previous example.

02:58 As the previous example had none, that’s not particularly hard to achieve. To get started, you have to import the argparse library. Then you create a parser. Once you have a parser, you call add_argument() to add an argument to the parser. In this case, I’ve called it name, which is consistent with the previous program.

03:22 On line 6, I do the same thing again, but for num. I also do one more thing, which is tell argparse that this argument is expected to be an integer.

03:31 It will automatically convert it to such for me and handles the case if the user gives me something that can’t be converted. With my arguments defined, the last step is to invoke the parser using the .parse_args() method. Notice you don’t have to do anything with sys.argv.

03:48 It does all that under the covers. Finally, lines 10 and 11 are essentially the same as before. The only difference is instead of using name and num as variables, I am accessing them from inside of the args namespace returned from .parse_args(). Fire up the command line.

04:11 No difference there.

04:16 Hmm, that’s better. Instead of crashing, you get a usage message and an indication of the problem: the num argument is required.

04:29 And as for the type-conversion problem, again much cleaner than before.

04:36 That’s it. That’s all you really need. Short course, huh? Wait, but there’s more. Next up, I’ll show you how to improve your help messages and dig into the different kinds of arguments.

04:49 Are you waiting for a Monty Python reference? You’re in the wrong room for that.

Become a Member to join the conversation.