Argument Parser in Python
Python argparse - Concise Notes
argparse
is a Python library for parsing command-line arguments. It provides a simple interface to define, process, and handle arguments.
Basic Usage
-
Setup
-
Adding Arguments
-
Positional Argument: Required, order matters
-
Optional Argument: Starts with
--
or-
, order doesnβt matter. -
Parsing Arguments
args = parser.parse_args()
print(args.filename) # Positional argument
print(args.verbose) # True/False (store_true)
print(args.number) # Integer value (default: 1)
Key Argument Settings
- Positional Arguments:
- No special prefix.
-
Example:
python script.py input.txt
-
Optional Arguments:
- Use
-
or--
. -
Example:
python script.py --verbose
-
Argument Types:
- Specify with
type
(e.g.,int
,float
,str
). -
Example:
-
Default Values:
- Use
default
. -
Example:
-
Boolean Flags:
- Use
action="store_true"
oraction="store_false"
. -
Example:
Example Usage
Script:
import argparse
parser = argparse.ArgumentParser(description="Demo script")
parser.add_argument("filename", help="Input file name")
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbosity")
parser.add_argument("-n", "--number", type=int, default=10, help="Number of items")
args = parser.parse_args()
print(f"Filename: {args.filename}")
print(f"Verbose: {args.verbose}")
print(f"Number: {args.number}")
Run:
Output:
Common Methods
parser.add_argument()
: Define arguments.parser.add_subparsers()
: Create subcommands.parser.set_defaults()
: Set default functions for arguments.parser.parse_args()
: Parse command-line inputs.
CLI with subcommands
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, RawTextHelpFormatter
class DeepFormatter(ArgumentDefaultsHelpFormatter, RawTextHelpFormatter): ...
def parse_args():
parser = ArgumentParser(
description="A simple script to demonstrate argument parsing.",
formatter_class=DeepFormatter,
)
# Top-level subparser
# dest value is used to check which command was called
# if `dest=meow`, then `args.meow` will be set to the subcommand called (cache, or optimize)
subparsers = parser.add_subparsers(dest="command", title="Commands")
cache_parser = subparsers.add_parser(
"cache", help="Cache the dataset", formatter_class=DeepFormatter
)
cache_parser.add_argument(
"--cache-dir", type=str, help="Directory to cache the dataset"
)
cache_parser.add_argument(
"--cache-size", type=int, default=100, help="Size of the cache in MB"
)
cache_parser.set_defaults(func=handle_cache)
optimize_parser = subparsers.add_parser(
"optimize", help="Optimize the dataset", formatter_class=DeepFormatter
)
optimize_parser.add_argument(
"--optimize-level", type=int, default=1, help="Level of optimization (1-3)"
)
optimize_parser.set_defaults(func=handle_optimize)
optimize_parser.add_argument(
"--optimize-strategy",
type=str,
choices=["fast", "balanced", "thorough"],
default="balanced",
help="Strategy for optimization",
)
return parser.parse_args(), parser
def handle_cache(args):
print(f"running command: {args.command}")
print(f"Caching dataset in {args.cache_dir} with size {args.cache_size}MB")
def handle_optimize(args):
print(f"Optimizing dataset with level {args.optimize_level} and strategy {args.optimize_strategy}")
def main():
args, parser = parse_args()
if hasattr(args, "func"):
args.func(args)
else:
parser.print_help()
if __name__ == "__main__":
main()
Tips
- Use
help
for clear CLI documentation. - Default type is
str
if not specified. - Boolean flags (
store_true
) are ideal for toggles.
For most cases, this basic usage suffices!