If you are into programming, you might have used commands like cp
, mv
, cat
, etc, to perform different operations using a text interface like bash or Windows PowerShell. This article discusses implementing command-line applications in Python with functionalities such as keyword arguments, flags, positional arguments, and mode selection. It also discusses how to implement the Linux head
command in Python.
What is a command-line application?
A command-line application is a computer program we use from a text interface like Bash, Windows PowerShell, Zsh, Dash, etc. We can execute a command line application by typing the command name and the required arguments. For example, we can use the mv
command to move a file from one directory to another by passing the source and destination locations as shown below.
In the above image, we have used the mv
command to move the sample_code.py
file from the /HoneyBadger/Codes
directory to the /HoneyBadger/ProgramCodes
directory. Here, mv
is the command line application name, and the two locations are input arguments to the application.
What are the different types of command-line arguments?
Positional arguments in a command-line application
Positional arguments in a command-line application are mandatory values passed to the application in a specific order. For example, ‘/HoneyBadger/Codes/sample_code.py
’ and ‘/HoneyBadger/ProgramCodes
’ are positional arguments for the above-mentioned mv
command. Here, you observe the following things.
- The
mv
command cannot work without these two arguments. - The first argument is the source address of the file.
- The second argument is the destination directory to which the file will be moved.
Keyword arguments in a command-line application
The keyword arguments in a command-line application are key-value pairs we pass to the application as input. We specify the keyword argument to the command-line application using single hyphen (-
) or double hyphen characters (--
) followed by the argument name. The input value to the argument follows the argument name after a space character or assignment operator (=
).
Options in a command-line application
Options are keyword arguments that have predefined allowed values. For instance, in the spark-submit
command, the deploy-mode
parameter is an option with two values, i.e., cluster
and client
.
Flags in a command-line application
Flags work as switches or toggles. We use flags to turn on or off specific features of a command-line application. For example, the cp
command uses the -r
flag to copy directories.
How to build command-line applications in Python
Usually, we execute a Python program from the command-line terminal using the syntax python filename.py
or python3 filename.py
. To add different functionalities to the Python command-line application, we can use the argparse module.
Creating and parsing command-line arguments in Python
We can create an argument parser to create or parse command-line arguments in a Python application using the ArgumentParser()
function defined in the argparse module. After creating an ArgumentParser object, we can use the add_argument()
and parse_args()
methods to create and parse command-line arguments in a Python application.