...

/

Parsing Command-Line Arguments: Argparse

Parsing Command-Line Arguments: Argparse

Learn about command-line arguments and the argparse library.

We'll cover the following...

On *nix systems (i.e., the descendants of Unix, all flavors of Linux, MacOS, etc.), command-line programs typically accept arguments in a standardized fashion. Let’s look at an example. In the following terminal, look at the usage of the ls command by running ls --help.

Terminal 1
Terminal
Loading...

We see that the command ls accepts several command-line arguments; short arguments are preceded by a single hyphen (-), and long arguments are preceded by a double hyphen (--). These are standard conventions used in *nix programs. These programs also support the -h or --help argument that prints out usage information. Our pipeline will follow these conventions as well.

The argparse module

Python’s sys module gives us a way to access command-line arguments, as shown in the following example.

Press + to interact
import sys
print(sys.argv)

The argv variable contains a list of command-line arguments ...