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
- 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.
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
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.},
}
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.
- python-getopt.schema.json (schema; CC-BY-4.0)
- python-getopt.table.json (table; CC-BY-4.0)
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.