---
source: https://iinventedlight.com/library/notes/python-getopt.md
title: "Python getopt: a C-style parser for command line options"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, getopt — C-style parser for command line options; CPython, getopt documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-24
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-24
as_of: 2026-11-24
confidence: medium
untested: "Not reproduced (2026-11-24). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-getopt"
tags: [python, getopt, argparse, command-line, options]
client: Librarian
sources:
  - id: S1
    title: "getopt — C-style parser for command line options"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/getopt.html
    accessed: 2026-11-24
    license: "PSF License Version 2"
  - id: S2
    title: "getopt documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/getopt.rst
    accessed: 2026-11-24
    license: "PSF License Version 2"
---
# Python getopt: a C-style parser for command line options
**Answer:** This module helps scripts to parse the command line arguments in sys.argv.

## What the module is for

This module is considered feature complete.

A more declarative and extensible alternative is provided in the optparse module.

This module provides two functions and an exception.

Long and short options may be mixed.

## How parsing stops

To accept only long options, shortopts should be an empty string.

The getopt function stops processing options as soon as it encounters a non-option argument.

This is similar to the way non-GNU Unix systems work.

The leading dashes should not be included in the option name.

## See also

- [python-argparse](https://iinventedlight.com/library/notes/python-argparse/)
- [python-shlex](https://iinventedlight.com/library/notes/python-shlex/)
- [python-sysconfig](https://iinventedlight.com/library/notes/python-sysconfig/)

## Claims

- id: C1
  claim: This module helps scripts to parse the command line arguments in sys.argv.
  source: S1
  evidence: "This module helps scripts to parse the command line arguments in sys.argv"

- id: C2
  claim: This module is considered feature complete.
  source: S1
  evidence: "This module is considered feature complete."

- id: C3
  claim: Long and short options may be mixed.
  source: S1
  evidence: "Long and short options may be mixed."

- id: C4
  claim: This is similar to the way non-GNU Unix systems work.
  source: S2
  evidence: "This is similar to the way non-GNU Unix systems work."

## Sources

- [S1] getopt — C-style parser for command line options. Python Software Foundation. https://docs.python.org/3/library/getopt.html . License: PSF License Version 2. Accessed 2026-11-24.

- [S2] getopt documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/getopt.rst . License: PSF License Version 2. Accessed 2026-11-24.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, getopt — C-style parser for command line options; CPython, getopt documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
