argparse
Command-line parsing with a tagged ArgumentValue and a dynamic Namespace.
Parsed attributes are tagged values: use args.count.as_int(),
args.name.as_str(), and args.files.values() to access native values.
Custom Actions
Named action registration normalizes constructor arguments to the standard
Action fields and does not forward additional constructor keywords. Pass
the action class directly to supply custom keywords.
Differences From CPython
- Positional arguments: Custom actions are supported only for options.
Using a custom class, factory, or registered action for a positional raises
TypeErrorbefore construction. - Initializer overloads: Class construction checks only the leading
__init__signature exposed by the compiler; it does not search additional overloads or base initializers. Use a single initializer, or a factory that explicitly calls the desired overload. - Parser defaults: For direct classes and factories, defaults from
set_defaults()andargument_defaultare passed asArgumentValue, not unboxed to a native constructor parameter type. Leave the default parameter unannotated or acceptArgumentValue; alternatively, passdefault=directly toadd_argument(), which preserves its native type.
Incompatible signatures are rejected at compile time when independent of parser
defaults, and with TypeError when they depend on runtime parser defaults.
SUPPRESS: Literal[str] = "==SUPPRESS=="
OPTIONAL: Literal[str] = "?"
ZERO_OR_MORE: Literal[str] = "*"
ONE_OR_MORE: Literal[str] = "+"
REMAINDER: Literal[str] = "..."
PARSER: Literal[str] = "A..."
VALUE_NULL: Literal[int] = json._KIND_NULL
VALUE_BOOL: Literal[int] = json._KIND_BOOL
VALUE_INT: Literal[int] = json._KIND_INT
VALUE_FLOAT: Literal[int] = json._KIND_FLOAT
VALUE_STR: Literal[int] = json._KIND_STR
VALUE_ARRAY: Literal[int] = json._KIND_ARRAY
VALUE_OBJECT: Literal[int] = 7
class ArgumentValue
Constructors
2
Properties
1
Methods
9
Special methods
7
class Namespace
Constructors
1
Special methods
9
class ArgumentTypeError
Constructors
1
class ArgumentError
Fields
1
argument_name
str
Constructors
1
Special methods
1
class Action
Fields
15
option_strings
List[str]
dest
str
nargs
str
const
ArgumentValue
default
ArgumentValue
type
Callable[str, ArgumentValue]
type_name
str
choices
_Choices
has_choices
bool
required
bool
help
Optional[str]
metavar
List[str]
deprecated
bool
kind
int
version
Optional[str]
Constructors
1
Methods
1
Special methods
2
class BooleanOptionalAction
Constructors
1
class HelpFormatter
Constructors
1
Methods
7
add_usage(self: Self, usage: Optional[str], actions: List[Action], groups: List[_MutuallyExclusiveGroup], prefix: Optional[str] = None, Self: type)
class RawDescriptionHelpFormatter
Constructors
1
class RawTextHelpFormatter
Constructors
1
class ArgumentDefaultsHelpFormatter
Constructors
1
class MetavarTypeHelpFormatter
Constructors
1
class FileType
Constructors
1
Special methods
2
class ArgumentParser
Fields
13
prog
str
usage
Optional[str]
description
Optional[str]
epilog
Optional[str]
prefix_chars
str
fromfile_prefix_chars
Optional[str]
argument_default
ArgumentValue
conflict_handler
str
allow_abbrev
bool
exit_on_error
bool
suggest_on_error
bool
color
bool
formatter_class
Callable[str, HelpFormatter]
Constructors
1
__init__(self: Self, prog: Optional[str] = None, usage: Optional[str] = None, description: Optional[str] = None, epilog: Optional[str] = None, parents = None, formatter_class = HelpFormatter, prefix_chars: str = "-", fromfile_prefix_chars: Optional[str] = None, argument_default = None, conflict_handler: str = "error", add_help: bool = True, allow_abbrev: bool = True, exit_on_error: bool = True, suggest_on_error: bool = False, color: bool = True, Self: type)
Methods
18
add_argument_group(self: Self, title: str = "", description: Optional[str] = None, Self: type)
add_mutually_exclusive_group(self: Self, required: bool = False, Self: type)
add_subparsers(self: Self, title: Optional[str] = None, description: Optional[str] = None, prog: Optional[str] = None, parser_class = None, dest: str = SUPPRESS, required: bool = False, help: Optional[str] = None, metavar = None, Self: type)
parse_known_intermixed_args(self: Self, args = None, namespace = None, Self: type)
class BooleanOptionalAction
Special methods
1