On this page
argparse.ArgumentTypeError
Prev: argparse.ArgumentParser | Up: argparse | Next: argparse.BooleanOptionalAction
Rel: argparse.Action, argparse.ArgumentDefaultsHelpFormatter, argparse.ArgumentError, argparse.ArgumentParser
Ref: docs.python.org โ argparse.ArgumentTypeError
Tags: #public #python3 #stdlib #argparse
Import
import argparse
from argparse import ArgumentTypeError
Signature & Overview
argparse.ArgumentTypeError(...)
An error from trying to convert a command line string to a type.
Methods & Attributes
ArgumentTypeError.add_note(self, note, /)โ Add a note to the exceptionArgumentTypeError.with_traceback(self, tb, /)โ Set self.traceback to tb and return self.
Usage
import argparse
def positive_int(raw: str) -> int:
val = int(raw)
if val <= 0:
raise argparse.ArgumentTypeError(f"{raw!r} must be > 0")
return val
p = argparse.ArgumentParser(exit_on_error=False)
p.add_argument("--jobs", type=positive_int)
assert p.parse_args(["--jobs", "4"]).jobs == 4
Gotchas
- Raise
ArgumentTypeErrorinside customtype=validators:argparseformats its message cleanly as a CLI usage error instead of printing a traceback.
REPL Inspect
from argparse import ArgumentTypeError
help(ArgumentTypeError)
Linked from
Related notes
- tuts/python3/stdlib_module/argparse/Action Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentDefaultsHelpFormatter Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentError Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentParser Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/BooleanOptionalAction Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse/FileType shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/HelpFormatter shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/MetavarTypeHelpFormatter shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/Namespace shared tags: #argparse, #stdlib