Argparse is a Python library utilized to parse command-line arguments in an easy to use way. It makes it simple to compose easy to use command-line user interfaces, and it is extensively utilized in Python applications. In this tutorial, we will go over how to pass a list as a command-line argument utilizing the Argparse library in Python.
Passing a List as a Command Line Argument with Argparse
To pass a Python list as a command-line argument with the Argparse library, we will utilize the “nargs” argument in the add_argument() approach. The “nargs” represents “variety of arguments”, and it informs the argparse the number of arguments a particular alternative need to anticipate.
Steps to pass a List as Command Line Argument with Argparse
Let us see the actions associated with passing lists as command line arguments with Argparse Library in Python
Action 1: Import the needed module
To utilize the argparse, you require to import the argparse module. You can do this by including the following line at the start of your Python script:
import argparse
Action 2: Develop an argument parser
Next, develop an argument parser things by calling the ArgumentParser() approach:
parser = argparse.ArgumentParser()
Action 3: Include an argument
Include the argument to the argument parser utilizing the add_argument() approach. Utilize the type specification to define the information kind of the argument, The list can be of any type, a string, an integer, and so on
parser.add _ argument("-- numbers", type= int)
Action 4: Parse the arguments
Lastly, parse the command-line arguments by calling the parse_args() approach on the argument parser things. This will return a things which contains the parsed arguments.
args = parser.parse _ args()
Step 5: Gain access to the list
You can access the list of integers passed as the “– numbers” argument utilizing the “numbers” quality of the args object.
print( args.numbers)
Passing a list of Strings
In this example, the list_of_strings work takes a string as input and returns a list of strings. The type specification of add_argument is set to list_of_strings, so when parse_args is called, the string worth of– str-list is transformed into a list of strings.
Python3
|
Output:
You can run this script with the following command. Here ‘script.py’ describes the name of the conserved Python file.
python script.py-- str-list foo, bar, baz
![Passing a list of strings as command line arguments](https://media.geeksforgeeks.org/wp-content/uploads/20230504111526/Screenshot-2023-05-04-111516.png)
Passing a list of strings as command line arguments
Note: Make certain that there is no area in between the products of the list, else it can produce a mistake. We will see how we can solve this mistake in the upcoming examples.
Passing a list of Integers
In this example, the list_of_ints work takes a string as input and returns a list of Python integers. The type specification of add_argument is set to list_of_ints, so when parse_args is called, the string worth of– int-list is transformed into a list of integers.
Python3
|
Output:
You can run this script with the following command:
python script.py-- int-list 1,2,3,4,5,6
![Passing a list of integers as command line arguments](https://media.geeksforgeeks.org/wp-content/uploads/20230504111955/Screenshot-2023-05-04-111836.png)
Passing a list of integers
Pass a List as a Command-Line Argument
Let us see a couple of examples to pass lists as command-line arguments in Python.
Example 1: Passing several worths utilizing nargs=’+’
In this example, we include an argument called “my_list” utilizing the “add_argument” approach. The “metavar” specification is utilized to define the argument’s name in the use message. The “type” specification is set to “str” because we desire the list to be a list of strings. The “nargs” specification is set to “+” to suggest that the argument can take several worths.
Python3
|
Output:
Passing just one argument:
python my_script. py geeksforgeeks
![Passing only one value as command line argument using nargs=](https://media.geeksforgeeks.org/wp-content/uploads/20230504114628/Screenshot-2023-05-04-114620.png)
Passing just one worth utilizing nargs=’+’
Passing more than one argument:
python my_script. py geeks for geeks
![Passing more than one value as command line argument using nargs=](https://media.geeksforgeeks.org/wp-content/uploads/20230504114342/Screenshot-2023-05-04-114326.png)
Passing more than one worth utilizing nargs=’+’
Example 2: Passing no or more worths utilizing nargs=’ *’
In this example, the ‘nargs’ specification is set to ‘*’ to suggest that the argument can take no or more worths.
Python3
|
Output:
Passing no arguments:
python my_script. py
![Passing zero value as command line argument using nargs=](https://media.geeksforgeeks.org/wp-content/uploads/20230504115112/Screenshot-2023-05-04-115056.png)
Passing no worth utilizing nargs=’ *’
Passing more than no arguments:
python my_script. py geeks for geeks
![Passing more than zero values as command line arguments using nargs=](https://media.geeksforgeeks.org/wp-content/uploads/20230504114342/Screenshot-2023-05-04-114326.png)
Passing more than no worths utilizing nargs=’ *’
Example 3: Passing Optional Arguments
In this example, we supplied 2 arguments, one is obligatory and the other is optional. We set the ‘needed’ specification to ‘Real’ which implies it is an obligatory one. In the 2nd argument, we did not specify the needed specification, which implies it is an optional argument.
Python3
|
Output:
When both arguments are specified:
python my_script. py-- string1 Hi-- string2 world
![Passing optional arguments as command line argument](https://media.geeksforgeeks.org/wp-content/uploads/20230504120304/Screenshot-2023-05-04-120255.png)
Passing optional arguments as command line argument
When the optional specification is not specified:
python my_script. py-- string1 Hi
![Without passing optional arguments as command line argument](https://media.geeksforgeeks.org/wp-content/uploads/20230504120421/Screenshot-2023-05-04-120411.png)
Without passing optional arguments as command line argument