Home »
Python
How to read/process command line arguments in Python?
Last Updated : April 28, 2025
In Python, command-line arguments allow you to pass information to a program when it starts. The arguments are passed during executing the program from command line.
Reading and Processing Command-Line Arguments Using argparse Module
The recommended way to read or process command-line arguments is by using the argparse module.
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
Reading Command-Line Arguments using sys.argv
The sys module provides access to command-line arguments via sys.argv, a list that contains the command-line arguments passed to the script.
The first element sys.argv[0] is always the name of the script itself, and the rest are the arguments.
Python Program to Read Command-Line Arguments using sys.argv
import sys
# Check if arguments are provided
if len(sys.argv) < 2:
print("Please provide some integers as arguments.")
sys.exit(1)
# Read arguments (excluding script name) and convert them to integers
numbers = list(map(int, sys.argv[1:]))
# Find maximum and sum
max_num = max(numbers)
total_sum = sum(numbers)
print(f"Maximum number: {max_num}")
print(f"Sum of numbers: {total_sum}")
Case 1: No Arguments Passed
python sysargv_example.py
The output will be:
Please provide some integers as arguments.
Case 2: Passing Arguments
python sysargv_example.py 3 5 9 1
The output will be:
Maximum number: 9
Sum of numbers: 18
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