Command-Line Modules

argparse

Declarative command-line parsing with positional arguments, options, types, defaults and subcommands. Invalid input normally prints usage and raises SystemExit. Pass a list to parse_args for deterministic unit tests instead of reading the host command line.

Formal Syntax & Structure

python syntaxpython
import argparse

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
from argparse import ArgumentParserparser = ArgumentParser()parser.add_argument("--count", type=int, default=1)args = parser.parse_args(["--count", "3"])print(args.count)
Python Reference