Argparse is a powerful library in Python for parsing command-line arguments. One common use case is passing lists as arguments to a script. In this tutorial, we will explore different ways to achieve this using argparse.
Introduction to Argparse
Before diving into passing lists, let’s cover the basics of argparse. Argparse is a built-in Python library that makes it easy to write user-friendly command-line interfaces. You can create an ArgumentParser object and add arguments to it using the add_argument
method.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--name', help='Your name')
args = parser.parse_args()
print(args.name)
Passing Lists with nargs
One way to pass lists as command-line arguments is by using the nargs
parameter in the add_argument
method. The nargs
parameter specifies the number of arguments that should be consumed.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--list', nargs='+', help='A list of items')
args = parser.parse_args()
print(args.list)
You can run this script from the command line like this:
python script.py -l item1 item2 item3
This will output: ['item1', 'item2', 'item3']
.
The nargs
parameter can take several values:
+
: one or more arguments*
: zero or more arguments?
: zero or one argument- an integer: exactly that many arguments
Passing Lists with action=’append’
Another way to pass lists as command-line arguments is by using the action='append'
parameter in the add_argument
method. This allows you to append multiple values to a list.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--list', action='append', help='A list of items')
args = parser.parse_args()
print(args.list)
You can run this script from the command line like this:
python script.py -l item1 -l item2 -l item3
This will output: ['item1', 'item2', 'item3']
.
Passing Lists as a Delimited String
You can also pass lists as a delimited string and then parse the string into a list in your script.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--list', help='A comma-separated list of items')
args = parser.parse_args()
# Split the string into a list
my_list = [item for item in args.list.split(',')]
print(my_list)
You can run this script from the command line like this:
python script.py -l item1,item2,item3
This will output: ['item1', 'item2', 'item3']
.
Passing Lists as JSON
Another option is to pass lists as JSON strings and then parse the string into a list in your script.
import argparse
import json
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--list', help='A JSON list of items')
args = parser.parse_args()
# Parse the JSON string into a list
my_list = json.loads(args.list)
print(my_list)
You can run this script from the command line like this:
python script.py -l "[\"item1\", \"item2\", \"item3\"]"
This will output: ['item1', 'item2', 'item3']
.
Conclusion
In conclusion, there are several ways to pass lists as command-line arguments using argparse. You can use the nargs
parameter, the action='append'
parameter, or pass lists as a delimited string or JSON string and then parse them in your script. The choice of method depends on your specific use case and requirements.