Home »
Python
How to read/process command line arguments in Python?
By IncludeHelp Last updated : December 07, 2024
How to Read and Process Command-Line Arguments in Python
In Python, the recommended way to read or process command-line arguments is by using the argparse
module, which provides a powerful and flexible method to create user-friendly command-line interfaces.
The argparse
module allows your Python script to define what arguments it requires and handles parsing those arguments from sys.argv
. It also automatically generates help and usage messages and raises errors when invalid arguments are provided.
Python Program to Read and Process Command-Line Arguments
Below is a Python example that demonstrates how to read and process command-line arguments using the argparse module:
import argparse
# Initialize the argument parser
parser = argparse.ArgumentParser(description='Process the numbers')
# Define a positional argument (list of integers)
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for addition')
# Define an optional argument
parser.add_argument('--sum', dest='addition', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
# Parse arguments from command line
args = parser.parse_args()
# Perform the operation based on the argument
print(args.addition(args.integers))
Save this script in a file named argparse_example.py
. You can run it from the command line and test different use cases:
Case 1: Running without passing any arguments
If you run the script without any arguments, it shows an error:
python argparse_example.py
usage: argparse_example.py [-h] [--sum] N [N ...]
argparse_example.py: error: the following arguments are required: N
Case 2: Displaying help message using -h
This command shows the usage and description of the arguments:
python argparse_example.py -h
usage: argparse_example.py [-h] [--sum] N [N ...]
Process the numbers
positional arguments:
N an integer for addition
optional arguments:
-h, --help show this help message and exit
--sum sum the integers (default: find the max)
Case 3: Providing numbers without --sum
(default: max)
When only numbers are passed, the script returns the maximum of the list:
python argparse_example.py 1 2 3 4
4
Case 4: Providing numbers with --sum
option
If you include the --sum
flag, it calculates the total sum of the integers:
python argparse_example.py 1 2 3 4 --sum
10
Conclusion
Using the argparse module in Python is an efficient and scalable way to handle command-line arguments. It enhances your script’s usability by enabling argument validation, help messages, and multiple input formats. Whether you're creating utilities, automation scripts, or data-processing tools, mastering argparse
is a valuable skill for Python developers.
Python Read and Process Command-Line Arguments Exercise
Select the correct option to complete each statement about reading and processing command-line arguments in Python.
- To read command-line arguments in Python, you can use the ___ module.
- The command-line arguments are stored in a list, accessible via the ___ attribute.
- To process command-line arguments in Python, you can use the ___ module to easily parse them.
Advertisement
Advertisement