Python Argparse Examples

“The interpretation of command line arguments has been made by the use of the Python module argparse. By providing user input elements to be processed, argparse would be used to give customization and reuse of the program in place of explicitly specifying variables within the function.”

Command Line Interface

A method to negotiate with a command line script would be through the command line interface or CLI. Although Python has several modules which we may use to construct command line interfaces for different scripts, the Python argparse package is now the efficient method for doing so.

The argparse module for Python was made available as a component of the standard framework. It is the preferred method for creating a Python Interface. The earlier “getopt” and “optparse” libraries were replaced with this one as they lacked several crucial functionalities.

Uses of Argparse Module

These are the uses of the Python argparse module:

    • Provides different units of arguments for a single item.
    • Supports the utilization of positional parameters.
    • Allow intermediate for subcommands.
    • Enable modification of the prefix elements.

Based on certain parameters, a primary command line parser may utilize additional command line parsers.

Utilization of a Command Line Interface

Now that we are aware of what a command line interface is, we might be concerned as to when it would be appropriate to incorporate it into the program. The general rule is that we will think about a command line interface if we decide to offer a user-friendly method of customizing the project.

The suitable technique is to use the Python argparse module. It is a great way to handle the value by constructing a command line interface using the Python argparse module unless we are constructing a complicated command line script that requires a configuration file to function. This way can let the user designate whose configuration file to be using.

How to Construct a Command Line Interface by Using Python Argparse Package

There are 4 phases to using the Python argparse module:

    • Include the argparse framework in Python.
    • Construct the parser.
    • Provide the parser with optional and positional parameters.
    • Run parse_args() function.

A User-defined object with a basic attribute for every input parameter obtained from the command line is what we can get after calling the parse_args() method.

Let’s execute different examples in which we utilize the argparse module.

Example no 1

An efficient interface for processing command line parameters is provided by the argparse tool. It shows the program’s general usage, instruction, and exceptions. The use of appropriate command line parameters is demonstrated in the succeeding illustration.

This example computes the circle’s area using a one-parameter radius from the command line. To add parameters and analyze them, the ArgumentParser object parser has been constructed. A radius parameter is added by the user, although it is left as an optional parameter. The method would not return an exception and set the parameter’s value to None if the user will not give a value for it, as seen in the scenario below.

import argparse

parser = argparse.ArgumentParser(description = 'Find the radius of any given circle')

parser.add_argument('-r','--radius', type = int, help='Calculated radius of any given circle')
args = parser.parse_args()

def main():


At the start of the program, we will import the header file “argparse”. In the next step, we will initialize a variable “parser”, and at the same time, we will call the argumentpraser() method. This function is related to the argparse module. We have given the “description” as the parameter of the argumentpraser() function. Then we add different parameters so we utilize the function add_argument() function. This function has three parameters which include the radius of the circle, the data type of the radius, and the value of the “help” argument.

After specifying the arguments, we will invoke the function parse_args() of the parser package. Now let’s define the body of the main() method. Within this function, we have been using the function print() to display the radius of the given circle. In the end, we utilize the main() method.

Example no 2

The “required” is an argument of the add_argument() method of the ArgumentParser object. Parameters of the attribute “f” or “foo” are unnecessary and, therefore, will be eliminated by default. The parameter “required” need can be set to True if the user wants to insert this parameter. As shown below, whenever we set an attribute as “required” but do not specify a value for this, the compiler returns an exception.

import argparse

parser = argparse.ArgumentParser(description = 'Determine radius of any circle', prog = 'The program', usage = '%(prog)s [options]')
parser.add_argument('-r','--radius', type = int, required = True, help='Calculated radius of any given circle')
args = parser.parse_args()
def main():


First of all, we are going to integrate the module “argparse”. The following step involves initializing the variable “parser” and applying the argumentparser() method simultaneously. This method is associated with the argparse package. The arguments for the argumentparser() method are description and prog.

Next, we add some additional parameters by using the add_argument() method. The radius of the circle, the radius’s data type, and the value of the “help” parameter are the four arguments for this function. We set the value of the “required” parameter to “True”.

In addition to this, we will call the method parse_args() of the parser header file after providing the arguments. Then we will define the main() function’s body. The print() method has been used here in the body of the main function to show the radius of the circle. We finally use the main() function.


By completing this article, we now comprehend a command line interface and the method of constructing a single command line argument using the argparse package. We also discussed what the Python argparse module is and why users will utilize it if they have to generate command line Python code. How and when to efficiently build a basic Interface using the argparse framework. We also observed the innovative use of the argparse module. An effective command line interface can provide users with a mechanism to engage with their system.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content