Loading video player…

Command Line Interfaces With argparse (Overview)

One of the strengths of Python is that it comes with batteries included: it has a rich and versatile standard library that makes it one of the best programming languages for writing scripts for the command line. But, if you write scripts for the command line, then you also need to provide a good command line interface, which you can create with the Python argparse library.

In this video course, you’ll learn:

  • 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
Download

Sample Code (.zip)

11.6 KB
Download

Course Slides (.pdf)

983.1 KB

00:00 Welcome to Building Command Line Interfaces With argparse. My name is Christopher, and I will be your guide. This course is all about the argparse library, which is part of Python’s standard-library collection. argparse helps you process command line arguments passed to your script.

00:19 In this course, you’ll learn about the argparse library, positional and optional arguments, handling different types of data, consuming multiple arguments, and how to write sub-parsers.

00:35 Everything in this course was tested using Python 3.10. The argparse library was introduced in Python 3.2. Most of this course can use 3.2 or newer.

00:45 There are a few items that were introduced later. I’ll point them out as I go along.

00:52 Although there are GUI things and web things out there, the vast majority of Python scripts are command line programs. There are programs that don’t require input, but most need something from the user to act upon.

01:04 It’s just the nature of code. There are different ways of getting input from the user. You can read a configuration file, you could use the input() function, or take in arguments on the command line.

01:15 This course is about argparse, a standard-library module that helps you do that last one. Command line arguments can be a bit tricky. You can get it all of them in the argv list of the sys module, but processing them by hand can get messy fast.

01:31 Everything passed in on the command line is a string. If you need a number, you have to remember to convert it. Some of your arguments might be optional, or you may need more than one. For each of these cases, you’re going to need to write a bunch of if statements to handle that. Command line flags, like -h, can get complicated.

01:50 They’re usually allowed to show up in any order. Some flags also have parameters that go with them, which means you have to consume more than one thing out of your sys.argv at a time. And if you want really messy, you might have groups of arguments, some of which have to be together or are mutually exclusive of each other.

02:10 As I’m sure you’ve guessed by my late-night-infomercial-style intro of everything’s hard, isn’t there an easier way?—well, there’s an easier way.

02:19 The argparse library does all these things and more. No tin cans were hurt in the filming of this commercial. argparse actually isn’t Python’s only foray into dealing with the command line.

02:30 It was introduced in Python 3.2 and replaced an older module. argparse is far more powerful than its predecessor, but even with that, there are also all sorts of third-party libraries out there that can do even more. At the end of this course, I’ll point you at some alternatives.

02:48 But why wait? Let’s dive into argparse.

Become a Member to join the conversation.