@@ -750,7 +750,8 @@ Only actions that consume command-line arguments (e.g. ``'store'``,
750
750
751
751
The recommended way to create a custom action is to extend :class: `Action `,
752
752
overriding the :meth: `!__call__ ` method and optionally the :meth: `!__init__ ` and
753
- :meth: `!format_usage ` methods.
753
+ :meth: `!format_usage ` methods. You can also register custom actions using the
754
+ :meth: `~ArgumentParser.register ` method and reference them by their registered name.
754
755
755
756
An example of a custom action::
756
757
@@ -971,10 +972,11 @@ necessary type-checking and type conversions to be performed.
971
972
If the type _ keyword is used with the default _ keyword, the type converter
972
973
is only applied if the default is a string.
973
974
974
- The argument to ``type `` can be any callable that accepts a single string.
975
+ The argument to ``type `` can be a callable that accepts a single string or
976
+ the name of a registered type (see :meth: `~ArgumentParser.register `)
975
977
If the function raises :exc: `ArgumentTypeError `, :exc: `TypeError `, or
976
978
:exc: `ValueError `, the exception is caught and a nicely formatted error
977
- message is displayed. No other exception types are handled.
979
+ message is displayed. Other exception types are not handled.
978
980
979
981
Common built-in types and functions can be used as type converters:
980
982
@@ -2058,6 +2060,34 @@ Intermixed parsing
2058
2060
.. versionadded :: 3.7
2059
2061
2060
2062
2063
+ Registering custom types or actions
2064
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2065
+
2066
+ .. method :: ArgumentParser.register(registry_name, value, object)
2067
+
2068
+ Sometimes it's desirable to use a custom string in error messages to provide
2069
+ more user-friendly output. In these cases, :meth: `!register ` can be used to
2070
+ register custom actions or types with a parser and allow you to reference the
2071
+ type by their registered name instead of their callable name.
2072
+
2073
+ The :meth: `!register ` method accepts three arguments - a *registry_name *,
2074
+ specifying the internal registry where the object will be stored (e.g.,
2075
+ ``action ``, ``type ``), *value *, which is the key under which the object will
2076
+ be registered, and object, the callable to be registered.
2077
+
2078
+ The following example shows how to register a custom type with a parser::
2079
+
2080
+ >>> import argparse
2081
+ >>> parser = argparse.ArgumentParser()
2082
+ >>> parser.register('type', 'hexadecimal integer', lambda s: int(s, 16))
2083
+ >>> parser.add_argument('--foo', type='hexadecimal integer')
2084
+ _StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type='hexadecimal integer', choices=None, required=False, help=None, metavar=None, deprecated=False)
2085
+ >>> parser.parse_args(['--foo', '0xFA'])
2086
+ Namespace(foo=250)
2087
+ >>> parser.parse_args(['--foo', '1.2'])
2088
+ usage: PROG [-h] [--foo FOO]
2089
+ PROG: error: argument --foo: invalid 'hexadecimal integer' value: '1.2'
2090
+
2061
2091
Exceptions
2062
2092
----------
2063
2093
0 commit comments