Introduction to argparse

Learn about the Python module, named argparse, the recommended command-line parsing module in the Python standard library.
12.6
Welcome back to the course. In this lecture, I’m using the python_argparse_ex1 script. So you can open that and follow along. We will begin to learn about the Python module argparse. This is part of the Python standard library, and it is the preferred way to parse command line arguments. In the past few lectures, we learned about sys.argv, which also allowed us to use command line arguments. And the reason we learned about that is because it is frequently used, and it’s easy to use. However, argparse provides more functionality for just a little more work, so it is the preferred method to use. Let’s go ahead and start learning about it now.
56.5
So to use it, you just need to import argparse, and then you will call the argument parser method here. And this is a special kind of method, and later in the course, we’ll learn more about methods and objects. But this is a special kind of method, which is kind of like a function, that returns an object. So when we call this method, it returns this parser object. And this parser object is what we will use to parse the arguments. So all you have to do is do this. And then, from the parser object, we call the parse arg’s method. And this will parse arguments.
97.2
Now, we haven’t added anything to the parser yet, and we’ll do that in the next lecture, so it doesn’t even know what it’s parsing. That’ll make more sense in the next lecture, once we start to add more information to the parser object. Right now it’s not parsing anything. This is a very bare bones usage. So it’s not really doing anything. However, we can still run this. So what I’m going to do is I’m actually going to run it from the terminal in VS Code. But I’m not going to use - actually, I will use the Command-R shortcut, and then I’ll show you why I want to use that. So if I hit Command-R, it’ll run this file.
143.8
And it runs the file, but see, it won’t add any argument. So if I want to add arguments to the command line here, I’m going to have to manually add them down here. So if I just use the up arrow, it will go back to the last call, which is the call that was used to run the script. We’ve gone over this before. And now I can add arguments. Now, again, parser right now doesn’t know what to look for. So if I had an argument, it really wouldn’t do anything. But using argument parser in this barebones way does give us one thing, and that is the help flag.
173.9
So this is automatically a feature, a default feature, of argument parser - I’m sorry, of argparse. So if I just add the help flag and hit Enter, I get a little help output. What does it show? Well, it shows the usage of the script. Right now, there are no arguments. This will change as we add things, and it’ll make more sense. But right now, it just shows you have this optional flag, which is the help flag, so one dash and h is equivalent to two dashes and help. And I can just show you that. So I’m going to change this two dashes and help to one dash and h.
211.2
So this is just the short version of the argument, abbreviated version, one dash and h. So it shows you that is an optional argument. You can also tell because it’s in the square brackets up here.
226.5
So this gives us just bare bones. We haven’t really processed any arguments, but we have created a parser object, and that gives us the help flag. Now, there is one thing I want to add while we’re here, and you can actually add a description argument to the parser object. This is a great place to add this docstring. So we learned about docstrings in the previous lectures. When you’re using argparse, you can pass it to the description argument. Now, if I save this file and run it again - so down here, I’m going to run it again - with the help flag, it will add the docstring to the help output. So you can see how useful this is.
268.4
You can have your docstring, and you can have it print any time you just apply the help tag. This is great for yourself or for others, if you’re writing some scripts, and they know that they can - especially if they don’t know Python. So if someone knows Python and they’re looking at your code anyways, they could always just read the docstring directly from the code. But if it’s more of an end user that you don’t really want to have to open up the code to learn about it, they can just use the help flag, and that will print out the docstring and also information about the arguments. All right, so good job. We’ve introduced argparse. We’ve created the parser argument.
306
We’ve shown how that gives us the help flag, and we’ve also added the description keyword, so we have a docstring as part of the output when we use the help flag. Great job, and we’ll see you in the next lecture.

In this video, you will begin to learn about the Python module, named argparse.

argparse provides more functionality, and although it requires a bit more work, it is the preferred method to parse command-line arguments.

This video tutorial is an introduction to argparse, the recommended command-line parsing module in the Python standard library.

Join the discussion

This is the args starting point. Share with your peers why you believe this is part of the Python standard library?
Use the Comments section below and let us know your thoughts. Try to respond to at least one other post and once you are happy with your contribution, click the Mark as complete button to check the step off, then you can move to the next step.

In the next step, we will learn how to add the first position argument to the parser object.