Command Line Options in Python

Prabhakar Rangarao
4 min readJul 5, 2021

Many Operating Systems when implemented provide a Command Line Interface (CLI) to interface with the inner workings of the operating system. They are system programs supplied with the operating system. Sometimes, they are also called as command-line interpreter, command processor or shell. To interact with a program running in a text-based shell prompt, programming languages provide provide functions and methods to capture the command line information.

Shell interpreters such as Bash on Linux or Command Prompt on Windows have a command line interface for each enabled by the shell interpreter that exposes a command prompt. Typically, the following elements are important to consider in a command-line interface:

  • a command or a program
  • command line arguments (zero to many arguments)
  • documentation access through help parameter
  • result shown in standard-output, which is also the command window

The Python interpreter is usually installed as /usr/local/bin/pythonon those machines where it is available; putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:

To understand the Python Interpreter, let us consider a simple example that takes option -c for command. The option “-c” , also called as command line option or parameter, asks to execute the Python command line arguments following the option -c as a Python program.

The use of command-line arguments in Python has strong influence by other languages, specifically C programming language. Hence, Python command line arguments directly inherit from the C.

Python command line arguments directly inherit from the C programming language. The following two code segments does similar things in C and Python respectively.

One obvious difference is the absence of argc variable as seen in the C language example. In Python, sys.argv is a list and using the len() function gives the number of arguments. Let us check the output of running the following Python code in a command line.

Test the above Python code with no argument and one or more arguments and the output is as follows:

The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

However, optparse is a more convenient, flexible, and powerful library for parsing command-line options than the old getopt module. optparse uses a more declarative style of command-line parsing: you create an instance of OptionParser, populate it with options, and parse the command line.

Yet another method is by using getopt() API. This module helps scripts to parse the command line arguments in sys.argv. It supports the same conventions as the Unix getopt() function (including the special meanings of arguments of the form ‘-‘ and ‘ — ‘).

Thus, there are multiple ways to interact with capturing and accessing command-line options when you run Python programs from a command-line. The use of options will enable passing various parameters to change the behavior of a Python program, specifying input and output file paths, etc.

Reference:

  1. An Introduction to Python for Unix/C Programmers (1993)
  2. Using the Python Interpreter
  3. Parser for command line options
  4. Parser for command-line options, arguments, and subcommands

--

--