@@ -752,7 +752,8 @@ Only actions that consume command-line arguments (e.g. ``'store'``,
752
752
753
753
The recommended way to create a custom action is to extend :class: `Action `,
754
754
overriding the :meth: `!__call__ ` method and optionally the :meth: `!__init__ ` and
755
- :meth: `!format_usage ` methods.
755
+ :meth: `!format_usage ` methods. You can also register custom actions using the
756
+ :meth: `~ArgumentParser.register ` method and reference them by their registered name.
756
757
757
758
An example of a custom action::
758
759
@@ -973,10 +974,11 @@ necessary type-checking and type conversions to be performed.
973
974
If the type _ keyword is used with the default _ keyword, the type converter
974
975
is only applied if the default is a string.
975
976
976
- The argument to ``type `` can be any callable that accepts a single string.
977
+ The argument to ``type `` can be a callable that accepts a single string or
978
+ the name of a registered type (see :meth: `~ArgumentParser.register `)
977
979
If the function raises :exc: `ArgumentTypeError `, :exc: `TypeError `, or
978
980
:exc: `ValueError `, the exception is caught and a nicely formatted error
979
- message is displayed. No other exception types are handled.
981
+ message is displayed. Other exception types are not handled.
980
982
981
983
Common built-in types and functions can be used as type converters:
982
984
@@ -2103,6 +2105,34 @@ Intermixed parsing
2103
2105
.. versionadded :: 3.7
2104
2106
2105
2107
2108
+ Registering custom types or actions
2109
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2110
+
2111
+ .. method :: ArgumentParser.register(registry_name, value, object)
2112
+
2113
+ Sometimes it's desirable to use a custom string in error messages to provide
2114
+ more user-friendly output. In these cases, :meth: `!register ` can be used to
2115
+ register custom actions or types with a parser and allow you to reference the
2116
+ type by their registered name instead of their callable name.
2117
+
2118
+ The :meth: `!register ` method accepts three arguments - a *registry_name *,
2119
+ specifying the internal registry where the object will be stored (e.g.,
2120
+ ``action ``, ``type ``), *value *, which is the key under which the object will
2121
+ be registered, and object, the callable to be registered.
2122
+
2123
+ The following example shows how to register a custom type with a parser::
2124
+
2125
+ >>> import argparse
2126
+ >>> parser = argparse.ArgumentParser()
2127
+ >>> parser.register('type', 'hexadecimal integer', lambda s: int(s, 16))
2128
+ >>> parser.add_argument('--foo', type='hexadecimal integer')
2129
+ _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)
2130
+ >>> parser.parse_args(['--foo', '0xFA'])
2131
+ Namespace(foo=250)
2132
+ >>> parser.parse_args(['--foo', '1.2'])
2133
+ usage: PROG [-h] [--foo FOO]
2134
+ PROG: error: argument --foo: invalid 'hexadecimal integer' value: '1.2'
2135
+
2106
2136
Exceptions
2107
2137
----------
2108
2138
0 commit comments