How to use the Python argparse module – with example

  • Reading time:16 mins read

What is the Python argparse module?

The argparse module is one of the many Python modules that are used to pass arguments to a python script when running Python programs through the command-line interface (CLI). 

 

What is the Python argparse module used for?

The argparse module stands out for its ability to handle errors and exceptions without writing any extra lines of code. This module further supports the use of positional arguments and the customization of prefix chars. These are some of the reasons why the argparse module is the most preferred module when passing arguments into a program through the terminal.       

How to get started with Python argparse?

In addition to this, the Python argparse library is part of the Python standard library and therefore needs no installation. Sometimes you may come across programs that run specifically in the terminal using the python keyword followed by the name of the file. In such instances, the argparse module can also enable us to parse some specific arguments into the program.  

 

python argparse

 

How to use Python argparse in practice?

To learn more about how we can use this module we are going to create a simple Python script that requires us to pass an argument say; “John” so that it prints out the message “Hello John”. In addition, we are also going to add a custom help text so that whoever is running the program can ask for help and know exactly the argument needed.

Importing the argparse module

Now we will start off by importing the argparse module at the top of our python script using the import keyword.  

 

import argparse

 

Creating an argument parser object

Now we are going to create an argument parser object using the argparse.ArgumentParser() initializer. Once this is in place we can now proceed to add our custom arguments using parser.add_argument(), this function allows us to add arguments to the parser object that we have created above.  

This function accepts some important parameters such as the name of the argument, the type which dictates the type of the argument we can pass, help which will contain our text message and required parameter is a boolean parameter which when set to True makes it mandatory to pass an argument.

 

How to use Python argparse

 

Accessing the arguments

Now finally to access the argument(s) that we have created we can make use of args.name where name is the name of the argument that we passed in the parser.add_argument() function.

However, in the script below we have created a variable named args that will store any information passed in from the command line as the argument. The full script is shown below.

 

Running the Python script

Now if we run the above Python script, we will certainly get an error letting us know that the name argument is required. Now we can also get further help information using the -h tag, this will give us more information about the kind of argument required.  

 

python .\demo.py -h

#Output:
usage: demo.py [-h] --name NAME              
                                             
options:
  -h, --help   show this help message and exit
  --name NAME  please give a name

Passing the argument to the script

Now when we pass the argument required to the program the program will execute as expected. In this case, we need to pass the name argument as shown below.  

 

python .\demo.py --name John

#Output:

Hello John

 

This is really helpful and really useful for any kind of terminal program that needs to take some input from the user to actually do whatever it is that it needs to do.

 

Python argparse module

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.