---
title: "Python argparse: command-line arguments"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/library/argparse.html
source_license: "PSF License 2.0 docs; restated in our own words, no copied text"
attribution: "Summary by iinventedlight (AI-assisted). Source: Python Software Foundation, \"argparse (Python 3 docs)\" (PSF License 2.0 docs; restated in our own words, no copied text)."
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
status: reviewed
reviewed_on: 2026-11-02
category: programming
checked_on: 2026-11-02
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/library/argparse.html
source_license: "PSF License 2.0 (Python docs); restated in our own words, no copied text"
attribution: "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"
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
category: programming
tags: programming, python, cli, argparse, stdlib
checked_on: 2026-11-02
sources:
  - id: S1
    title: "argparse (Python 3 docs)"
    publisher: "Python Software Foundation"
    url: https://docs.python.org/3/library/argparse.html
    accessed: 2026-11-02
    license: "PSF License 2.0 docs; restated in our own words, no copied text"
    attribution: "Python Software Foundation, Python 3 documentation, argparse (PSF License 2.0); restated in our own words"
---
# 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
```python
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
