argparse
The Python argparse
module is a framework for creating user-friendly command-line interfaces (CLI). It allows you to define command-line arguments and options. It takes care of parsing them, and automatically generates help and usage messages for you.
Here’s quick example:
>>> import argparse
>>> parser = argparse.ArgumentParser(
... description="Process some integers."
... )
>>> parser.add_argument(
... "integers",
... metavar="N",
... type=int,
... nargs="+",
... help="an integer for the accumulator"
... )
>>> parser.add_argument(
... "--operation",
... choices=["sum", "max"],
... default="max",
... help="operation to perform on the integers (default: max)"
... )
>>> args = parser.parse_args(["2", "3", "5", "--operation", "sum"])
>>> if args.operation == "sum":
... result = sum(args.integers)
... else:
... result = max(args.integers)
...
>>> result
10
Key Features
- Creates and parses command-line arguments, options, and subcommands
- Automatically generates help and usage messages
- Supports positional and optional command-line arguments
- Provides type checking and conversion for arguments
Frequently Used Classes and Functions
Object | Type | Description |
---|---|---|
ArgumentParser |
Class | Provides the main class for argument parsing |
ArgumentParser.add_argument() |
Method | Adds a new argument to the parser |
ArgumentParser.parse_args() |
Method | Parses command-line arguments |
ArgumentParser.print_help() |
Method | Prints a help message and exits |
ArgumentParser.add_argument_group() |
Method | Groups command-line arguments |
Examples
Creating a parser with a positional argument and an optional argument:
>>> import argparse
>>> parser = argparse.ArgumentParser(
... description="A simple example"
... )
>>> parser.add_argument(
... "filename",
... help="the file to process"
... )
>>> parser.add_argument(
... "--verbose",
... action="store_true",
... help="increase output verbosity"
... )
>>> args = parser.parse_args(["example.txt", "--verbose"])
>>> args.filename
'example.txt'
>>> args.verbose
True
Common Use Cases
- Creating command-line tools with various input parameters
- Providing command-line interfaces for packages and libraries
Real-World Example
Suppose you need a program to process a list of files and want to implement different verbosity levels for the output. Here’s how you can do it with argparse
:
>>> import argparse
>>> parser = argparse.ArgumentParser(
... description="Process files with verbosity control"
... )
>>> parser.add_argument(
... "files",
... metavar="F",
... type=str,
... nargs="+",
... help="files to process"
... )
>>> parser.add_argument(
... "--verbose",
... "-v",
... action="count",
... default=0,
... help="increase verbosity level"
... )
>>> args = parser.parse_args(["file1.txt", "file2.txt", "-vv"])
>>> if args.verbose >= 2:
... print(f"Processing files: {args.files}")
Processing files: ['file1.txt', 'file2.txt']
>>> for file in args.files:
... print(f"Processing {file}")
Processing file1.txt
Processing file2.txt
In this example, you use the argparse
module to define a command-line interface that accepts multiple file names and a verbosity option, demonstrating its versatility in handling complex argument parsing scenarios.
Related Resources
Tutorial
Build Command-Line Interfaces With Python's argparse
In this step-by-step Python tutorial, you'll learn how to take your command-line Python scripts to the next level by adding a convenient command-line interface (CLI) that you can write with the argparse module from the standard library.
For additional information on related topics, take a look at the following resources:
- Click and Python: Build Extensible and Composable CLI Apps (Tutorial)
- Build a Command-Line To-Do App With Python and Typer (Tutorial)
- Raining Outside? Build a Weather CLI App With Python (Tutorial)
- Build a Python Directory Tree Generator for the Command Line (Tutorial)
- Building Command Line Interfaces With argparse (Course)
- Build Command-Line Interfaces With Python's argparse (Quiz)
- Building a Python Command-Line To-Do App With Typer (Course)
By Leodanis Pozo Ramos • Updated June 23, 2025