← Reference notes

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

Caveats

Pointers

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

Permanent ID: iil:note:ey76v6x6

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.