Python Glossary Русский

all Python 3argparseexception

getopt.GetoptError

getopt — C-style parsing

The getopt exception for an unknown option, an option missing its required argument, or an argument given to an option that takes none.

Syntax

except getopt.GetoptError as err:

Unlike argparse, getopt prints nothing and does not exit: catch the parse error and turn it into a usage message yourself. The msg attribute holds the error text and opt holds the option name without dashes ('x' for -x), or an empty string when no single option is to blame. The name getopt.error is the same class kept for compatibility. It derives from Exception, not ValueError.

Examples

import getopt
try:
    getopt.getopt(['-x'], 'a')
except getopt.GetoptError as err:
    print(err.msg)  # → option -x not recognized
    print(err.opt)  # → x
print(getopt.error is getopt.GetoptError)  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy