Adding the capability of processing Python command line arguments provides a user-friendly interface to your text-based command line program. It’s similar to what a graphical user interface is for a visual application that’s manipulated by graphical elements or widgets.
Python exposes a mechanism to capture and extract your Python command line arguments. These values can be used to modify the behavior of a program. For example, if your program processes data read from a file, then you can pass the name of the file to your program, rather than hard-coding the value in your source code.
By the end of this tutorial, you’ll know:
- The origins of Python command line arguments
- The underlying support for Python command line arguments
- The standards guiding the design of a command line interface
- The basics to manually customize and handle Python command line arguments
- The libraries available in Python to ease the development of a complex command line interface
If you want a user-friendly way to supply Python command line arguments to your program without importing a dedicated library, or if you want to better understand the common basis for the existing libraries that are dedicated to building the Python command line interface, then keep on reading!
Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you'll need to take your Python skills to the next level.
The Command Line Interface
A command line interface (CLI) provides a way for a user to interact with a program running in a text-based shell interpreter. Some examples of shell interpreters are Bash on Linux or Command Prompt on Windows. A command line interface is enabled by the shell interpreter that exposes a command prompt. It can be characterized by the following elements:
- A command or program
- Zero or more command line arguments
- An output representing the result of the command
- Textual documentation referred to as usage or help
Not every command line interface may provide all these elements, but this list isn’t exhaustive, either. The complexity of the command line ranges from the ability to pass a single argument, to numerous arguments and options, much like a Domain Specific Language. For example, some programs may launch web documentation from the command line or start an interactive shell interpreter like Python.
The two following examples with the Python command illustrates the description of a command line interface:
$ python -c "print('Real Python')" Real Python
In this first example, the Python interpreter takes option
-c for command, which says to execute the Python command line arguments following the option
-c as a Python program.
Another example shows how to invoke Python with
-h to display the help:
$ python -h usage: python3 [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -b : issue warnings about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str. (-bb: issue errors) [ ... complete help text not shown ... ]
Try this out in your terminal to see the complete help documentation.
The C Legacy
Python command line arguments directly inherit from the C programming language. As Guido Van Rossum wrote in An Introduction to Python for Unix/C Programmers in 1993, C had a strong influence on Python. Guido mentions the definitions of literals, identifiers, operators, and statements like
return. The use of Python command line arguments is also strongly influenced by the C language.
To illustrate the similarities, consider the following C program: