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

Command Line Interfaces With argparse (Summary)

Now you know what a command line interface is and how you can create one in Python by using the Python argparse library.

In this video course, you’ve learned:

  • What the Python argparse library is, and why it’s important to use it if you need to write command line scripts in Python
  • How to use the Python argparse library to quickly create a simple CLI in Python
  • What the advanced usage of the Python argparse library is

Writing a good command line interface is a good way to create self-explanatory programs and give users a means of interacting with your application.

If you still have questions, don’t hesitate to reach out in the comment section below and take a look at the official documentation and the how-to guide.

For alternatives to argparse, check out:


Sample Code (.zip)

11.6 KB

Course Slides (.pdf)

983.1 KB

00:00 In the previous lesson, I showed you how to build a sub-parser. In this lesson, I’ll summarize the course and point you at some other command line parsing libraries. In this course, you learned all about the command line argument parser built into the Python standard library, called argparse.

00:17 This library supports both positional arguments as well as optional flags. It automatically generates help information, creating a -h flag for you.

00:28 You can modify how arguments are stored using the action parameter. This allows you to convert command line strings into numbers, use Boolean flags, and more.

00:37 You can also control how many parameters are consumed by your arguments through the use of nargs. nargs supports specific numbers of arguments or variable numbers of arguments with the special symbols *, +, and ?. argparse is pretty powerful, allowing all sorts of fine-grain control.

00:58 You can even specify that an argument must be one of a given set of choices, or you can create mutually exclusive groups of flags. Tired of typing all those arguments over and over? Built into the parser is a way of reading arguments from a file. This is done with an argument prefix, which is often set the @ symbol.

01:19 In addition to using the type parameter to convert command line strings into integers, you can also use any callable. This allows you to do fancier things, like directly convert the argument into a pathlib object. Although argparse comes with many storage actions, you can also write your own if you need something fancier. And finally, in the previous lesson, I covered how to use argparse to create sub-parsers.

01:44 This allows you to have your programs take commands similar to how Git uses checkout, status, and more.

01:53 argparse is a pretty deep library. It’s very well documented, and there is also a how-to guide available at the Python site if you want to read the details.

02:02 There are also several libraries out there to make argument management require less code than argparse. One of the most popular is called Click.

02:10 It uses function decorators to create subcommands., meaning there is less code necessary to write per sub-parser equivalent.

02:18 Another popular library is docopt, where you use docstrings to define your command line interface.

02:25 And if you want a summary, this article compares those two libraries with argparse so that you can choose which is best for your situation.

02:35 That’s all for me. I hope you found this course valuable. In the words of Siskel and Ebert, I’ll see you at the movies.

Avatar image for dana

dana on Sept. 8, 2022

I was disappointed to find that this course did not cover some of the basic formats for parameters such as:

parser.addargument("-t", "--text", help="The text data to encode into the image, this only should be specified for encoding")

showing you can use multiple formats for the keyword arguments or that the above code will produce a help item like:

-t TEXT, --text TEXT The text data to encode into the image, this only should be specified for encoding

automatically filling in the arg name in CAPS.

I don’t think this is intuitively obvious to the naive student of the course who has never used ‘argparse’.

Avatar image for Christopher Trudeau

Christopher Trudeau RP Team on Sept. 9, 2022

Hi Dana,

I’m always interested in learning how to make the courses better, and maybe the concepts you describe could have been more clear, but:

  • The use of both a “-p” and “--place” argument are shown in the Casablanca example in lesson 3, and
  • The result of the all-caps placeholder text is covered with the introduction of metavar in the Indy example as part of the “doom” sub-parser.

I agree it might have been helpful to be more explicit about it, but the concepts were there.

Hope the next course you take covers things the way you are looking for.

Avatar image for Santosh

Santosh on Oct. 3, 2022

Great course, with great humor! Never thought I’d type “Kali Ma” into a function. :)

Avatar image for Christopher Trudeau

Christopher Trudeau RP Team on Oct. 3, 2022

Glad you enjoyed it Santosh. I recently re-watched all four movies, they seem to be following the Star Trek rule of every other one being good. Hopefully when #5 comes out next year it will follow the pattern.

Happy coding!

Avatar image for gunnarpruss

gunnarpruss on Dec. 21, 2023

Hi Christopher,

thanks for the course, I enjoyed the references a lot! I’m still not sure how to best deal with the the parser during development, though. Usually I manually set sys.argv in the REPL before calling parse_args(), but that feels a bit clunky. Do you know a better way?

Avatar image for Christopher Trudeau

Christopher Trudeau RP Team on Dec. 21, 2023

Hi gunnarpruss,

The call to parse_args() takes arguments. If you don’t give it arguments, it uses sys.argv. If you wrap your call to the parser in a function you can then test that function by passing arguments in.

This stackoverflow question shows examples:


That said, I do as you do, stuffing sys.argv instead. Works fine enough :)

Avatar image for taich

taich on May 7, 2024

I find it very confusing that all examples are linked to things that have nothing to do with the actual topic.

Avatar image for taich

taich on May 8, 2024

And BTW, the topic was very helpful for me, cause I now use argparse to validate a complex configuration file.

Become a Member to join the conversation.