Python argparse list of strings Implementation : 4 Steps Only

Python’s argparse module allows you to write user-friendly command-line interfaces. It provides an easy way to declare the data types of arguments that your program expects. After that, it automatically generates helpful error messages if the users provide invalid arguments while running the code. In this entire tutorial, you will know how to implement python argparse using list of strings from the command prompt.

In most cases, argparse is used to parse a list of strings. Suppose you want to parse a list of filenames or a list of usernames. Then the argparse module allows you to do this by specifying the type of the list elements as a string.

Steps to Implement argparse list of strings in Python

In this section, you will learn how to use argparse to parse a list of strings through steps. Just follow all the simple steps for better understanding.

Step 1: Import the parser module

In the first step, you will import the argparser module. Import it using the below line of code.

import argparse

Step 2: Call the parser

The next step is to define the parser. There is a function for that and it is ArgumentParser(). Let’s create a parser using the below line of code.

parser = argparse.ArgumentParser()

Step3: Define the type of the parser

After defining the parser you now have to define the characteristics of the parser. For this, you will use the add_argument() function that will accept the three arguments. One is the type of the parser( list), the second is the data type of the element of the arguments and the third is the nargs that allow you to add more than one value.

Let’s create it.

parser.add_argument('--list', type=str, nargs='+')
args = parser.parse_args()
print(args.list)

Step 4: Run the program

Now the final step is to run the code. Open your command prompt and type the following lines of code.

python sample.py --list one two three

Full Code

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--list', type=str, nargs='+')
args = parser.parse_args()
print(args.list)

Output

Python argparse list of strings output
Python argparse list of strings output

You can see that the argparse has automatically parsed the list of strings into a Python list.

You will get the error if you try to pass the arguments of non-string type. For example, if I will type the below command then I will get the error.

python example.py --list one two three four five

Output

usage: sample.py [-h] [--list LIST [LIST ...]] example.py: error: argument --list: invalid str value: 'four'

Conclusion

Argparse is the best tool for parsing command-line arguments. It is very useful when writing user-friendly command-line interfaces. These are the steps to implement the python argparse list of strings through steps.

Code The Best
Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.