Python Glossary Русский

all Python 3argparsefunction

getopt.gnu_getopt()

getopt — C-style parsing

The GNU-style variant of getopt: options and plain arguments may be intermixed, and parsing does not stop at the first file name.

Syntax

opts, args = getopt.gnu_getopt(args, shortopts, longopts=[])

This is how familiar Linux tools behave: prog file -v still parses -v. The argument '--' stops parsing: everything after it is treated as plain arguments. If the option string starts with '+' or the POSIXLY_CORRECT environment variable is set, the function behaves like plain getopt — a surprise on systems where that variable is set.

Examples

import getopt
opts, rest = getopt.gnu_getopt(['-a', 'file', '-b', 'val', '-c'], 'ab:c')
print(opts)  # → [('-a', ''), ('-b', 'val'), ('-c', '')]
print(rest)  # → ['file']
print(getopt.gnu_getopt(['-a', '--', '-c'], 'ac'))  # → ([('-a', '')], ['-c'])

See also

Open in the full glossarydocs.python.orgReport an inaccuracy