How to pass a list as a command-line argument to a Python program
We are used to Python programs being self-contained but some times the inputs to a Python program can come from outside the program, e.g., from the user at invocation time. Such inputs are given as command line arguments to the Python program.
The argparse module provides a powerful way to parse command-line arguments. In this blogpost, we will explore the ArgumentParser class, focusing specifically on using it to handle lists of arguments.
Let's start by understanding the basic usage of the ArgumentParser class without considering list arguments. We will create a simple Python script that takes two integers as command-line arguments and prints their sum:
In the above program, we import argparse as “a”, then use it to parse the inputs from the command line. Note that after the message “Enter two numbers:”, we specify two arguments into variables “num1” and “num2” and parse it. The result is then computed and printed in a suitably formatted message.
If we run this program without any command line arguments the usual manner, we will get:
Thus, we can do:
and this will output:
Note that the program has correctly parsed the two command line inputs 36 and 64 and printed the output as 100.
Handling List Arguments to ArgParse
Often, we may encounter situations where a script requires multiple inputs of the same type. For example, a script might need to process a list of numbers, file paths, or strings. In our case, assume that instead of adding two numbers we desire to add a list of numbers, i.e., a list whose length is not predetermined. To handle such scenarios, we can use the nargs parameter of the add_argument method. The nargs parameter specifies how many arguments a specific option should consume.
In the above code, we've changed the nargs parameter to "+", which means that the numbers argument should consume one or more values. The sum() function is then used to calculate the sum of all the numbers in the list.
Now, if we run the script with multiple numbers as command-line arguments:
If we run it without any command line arguments like so:
we will get the error:
because as you can see it expects at least one element in “numbers”.
Another situation where you will need an arbitrary number of arguments is in writing a program that processes multiple file paths. Each file should be opened, and its contents should be printed. It is very easy to adapt the above program to meet this need.
These are various possible values for the nargs parameter in ArgumentParser. By using the appropriate nargs value, we can effectively handle different types of command-line arguments and tailor the script's behavior to suit our specific needs.
Use nargs=”+” means that the program needs to have at least one value but can accept more than one value. Similarly, nargs="*" allows the option to have zero or more values. nargs=N specifies that the option should consume exactly N values. This is useful when the script requires a specific number of arguments for an option. Finally, nargs="?" indicates that the option can take zero or one value but not more values.
So in summary, the argparse module in Python provides a robust and user-friendly way to handle different types of arguments, including lists.
With what you have learned in this blogpost, you can now confidently use ArgumentParser to create command-line interfaces for your Python scripts!
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.