argparse

Python's standard-library module for building command-line interfaces that parse arguments and flags.

argparse turns command-line input into usable values. You create an ArgumentParser, declare the arguments you expect with add_argument, and call parse_args() to read them. It handles positional arguments, optional --flags, type conversion, and an automatic -h help screen.

It saves you from hand-parsing sys.argv and gives users clear errors when they get the command-line arguments wrong.

Example
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("name")
parser.add_argument("--shout", action="store_true")

args = parser.parse_args(["Ada", "--shout"])
print(args.name, args.shout)
Output
Ada True

Where this shows up in real Python

argparse powers real command-line tools: named flags, defaults, types, and an auto-generated --help.

Commonly used argparse tools

  • ArgumentParser() — create the parser
  • .add_argument('path') — declare a positional argument
  • type=int, default=... — convert and supply a fallback
  • action='store_true' — an on/off flag
  • .parse_args() — read the arguments into an object

Official documentation: Python Library Reference: argparse