On this page
getopt
Prev: genericpath | Up: stdlib_module | Next: getpass
Rel: os, io, time, argparse, optparse
Ref: docs.python.org — getopt
Tags: #public #python3 #stdlib
Import
import getopt
Overview
C-style command-line option parser (getopt(), gnu_getopt()) mirroring the POSIX C getopt(3) function. Use getopt only when maintaining legacy scripts or porting a C CLI tool whose flag-parsing behavior must match getopt(3) verbatim; for new Python CLIs, use argparse.
Quick Start
import getopt
argv = ["-v", "-o", "out.txt", "input.txt"]
opts, args = getopt.getopt(argv, "vo:", ["verbose", "output="])
assert opts == [("-v", ""), ("-o", "out.txt")]
assert args == ["input.txt"]
Implementation Walkthrough
1. getopt() vs. gnu_getopt()
Standard getopt() stops parsing options at the first non-option positional argument, while gnu_getopt() allows options and positional arguments to be intermixed:
import getopt
opts, args = getopt.gnu_getopt(["input.txt", "--output=out.txt"], "o:", ["output="])
assert opts == [("--output", "out.txt")]
assert args == ["input.txt"]
Gotchas & Boundaries
- No automatic
--helpor type conversion: Every option value arrives as a rawstr, and you must write your own usage text, validation, andgetopt.GetoptErrorhandling. - Easy to forget the
:/=suffix: In"vo:"and["output="], omitting:or=makes the flag boolean and leaves its argument inargs. - Prefer argparse:
argparsehandles help formatting, defaults, types, subcommands, and mutually exclusive groups automatically.
Common Functions
- getopt.getopt() —
getopt(args, shortopts, longopts=[])— Parses command line options and parameter list. args is the argument list to be parsed, without the leading reference to the running program. Typically, this means "sys.argv[1:]". shortopts - getopt.gnu_getopt() —
gnu_getopt(args, shortopts, longopts=[])— This function works like getopt(), except that GNU style scanning mode is used by default. This means that option and non-option arguments may be intermixed. The getopt() function stops
Classes & Objects
- getopt.error —
getopt.errorclass. (Methods:.add_note(),.with_traceback()) - getopt.GetoptError —
getopt.GetoptErrorclass. (Methods:.add_note(),.with_traceback())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/argparse
- tuts/python3/stdlib_module/asyncio
- tuts/python3/stdlib_module/concurrent
- tuts/python3/stdlib_module/contextvars
- tuts/python3/stdlib_module/ctypes
- tuts/python3/stdlib_module/curses
- tuts/python3/stdlib_module/errno
- tuts/python3/stdlib_module/fcntl
- tuts/python3/stdlib_module/genericpath
- tuts/python3/stdlib_module/getpass
- tuts/python3/stdlib_module/grp
- tuts/python3/stdlib_module/logging
- tuts/python3/stdlib_module/mmap
- tuts/python3/stdlib_module/msvcrt
- tuts/python3/stdlib_module/multiprocessing
- tuts/python3/stdlib_module/nt
- tuts/python3/stdlib_module/optparse
- tuts/python3/stdlib_module/platform
- tuts/python3/stdlib_module/posix
- tuts/python3/stdlib_module/pty
- tuts/python3/stdlib_module/pwd
- tuts/python3/stdlib_module/queue
- tuts/python3/stdlib_module/resource
- tuts/python3/stdlib_module/sched
- tuts/python3/stdlib_module/select
- tuts/python3/stdlib_module/selectors
- tuts/python3/stdlib_module/signal
- tuts/python3/stdlib_module/socket
- tuts/python3/stdlib_module/ssl
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/subprocess
- tuts/python3/stdlib_module/syslog
- tuts/python3/stdlib_module/termios
- tuts/python3/stdlib_module/threading
- tuts/python3/stdlib_module/tty
- tuts/python3/stdlib_module/winreg
- tuts/python3/stdlib_module/winsound
Related notes
- tuts/python3/stdlib_module/argparse Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/genericpath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/getpass Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/optparse Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/asyncio links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/concurrent links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/contextvars links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ctypes links to this page; shared tags: #stdlib