Python argparse: command-line arguments
Python argparse: command-line arguments
Summary
argparse is the standard-library module for command-line interfaces. You create an ArgumentParser, declare positional arguments and --options with add_argument, then call parse_args() to get a namespace object. It writes -h/--help and usage errors for you.
Draft. License: CC BY 4.0 (this summary). Sources: [S1] Python Software Foundation, "argparse (Python 3 docs)" (PSF License 2.0 docs; restated in our own words, no copied text).
Example
import argparse
p = argparse.ArgumentParser(description="Resize images")
p.add_argument("path")
p.add_argument("--width", type=int, default=800)
p.add_argument("--dry-run", action="store_true")
args = p.parse_args()
print(args.path, args.width, args.dry_run)
Facts
- id: F1 claim: ArgumentParser builds the parser; parse_args() reads sys.argv and returns a Namespace with one attribute per argument. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F2 claim: Arguments whose names start with - or -- are optional; others are positional and required by default. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F3 claim: A -h/--help option is added automatically, with usage text built from the argument definitions. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F4 claim: type=int (or any callable) converts the string value; choices=[...] restricts allowed values; default sets the value when the option is absent. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F5 claim: action='store_true' makes a flag that is False unless given. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F6 claim: nargs controls how many values are consumed: '?' (zero or one), '*' (zero or more), '+' (one or more), or an integer. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F7 claim: Dashes in long option names become underscores in the attribute name, e.g. --dry-run becomes args.dry_run. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F8 claim: On invalid input argparse prints a usage message and exits with status 2. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F9 claim: add_subparsers() supports git-style subcommands, each with its own arguments. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
Caveats
- required=True on an --option is allowed but the docs consider required options bad form; prefer positionals.
Pointers
- id: P1 answers: Full details and updates where: https://docs.python.org/3/library/argparse.html access: free, public web page
Source: https://docs.python.org/3/library/argparse.html · License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted), condensed and paraphrased; sources: Python Software Foundation, Python 3 documentation, argparse (PSF License 2.0); restated in our own words
Cite this
iinventedlight Library. 2026. "Python argparse: command-line arguments." iinventedlight Library, iil:note:ey76v6x6, version of 2026-11-02. https://iinventedlight.com/library/reference/python-docs/python-argparse/. License: CC-BY-4.0. Summary of https://docs.python.org/3/library/argparse.html.
@misc{iil_note_ey76v6x6,
author = {{iinventedlight Library}},
title = {{Python argparse: command-line arguments}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-02},
url = {https://iinventedlight.com/library/reference/python-docs/python-argparse/},
note = {Permanent ID iil:note:ey76v6x6. License: CC-BY-4.0. Summary of https://docs.python.org/3/library/argparse.html.},
}
For language models and agents: start at /llms.txt. This page in Markdown: /library/reference/python-docs/python-argparse.md.