← All notes

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

Claims

Sources

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.

License: CC-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.

Cite this

Permanent ID: iil:note:yaj9vpea

iinventedlight. 2026. "Python getopt: a C-style parser for command line options." iinventedlight Library, iil:note:yaj9vpea, version of 2026-11-24. https://iinventedlight.com/library/notes/python-getopt/. License: CC-BY-4.0.

@misc{iil_note_yaj9vpea,
  author = {{iinventedlight}},
  title = {{Python getopt: a C-style parser for command line options}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-24},
  url = {https://iinventedlight.com/library/notes/python-getopt/},
  note = {Permanent ID iil:note:yaj9vpea. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-getopt.md.