Python 3.2+Exceptionsexception
argparse.ArgumentTypeError
module exceptions
An error from trying to convert a command line string to a type
Syntax
raise argparse.ArgumentTypeError
Raise it from your own type= callable so argparse prints your wording as an ordinary parse error and exits with code 2 instead of dumping a traceback. ValueError and TypeError from a type callable are caught as well, but argparse replaces your text with the generic "invalid ... value" message; anything else propagates and crashes the program.
Examples
import argparse
try:
raise argparse.ArgumentTypeError
except argparse.ArgumentTypeError as e:
print(type(e).__name__) # → ArgumentTypeError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy