Skip to content

Commit 1b58c0f

Browse files
[3.13] GH-122679: Add register() to argparse docs (GH-126939) (GH-127149)
(cherry picked from commit fcfdb55) Co-authored-by: Savannah Ostrowski <[email protected]>
1 parent af00c58 commit 1b58c0f

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

Doc/library/argparse.rst

+33-3
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,8 @@ Only actions that consume command-line arguments (e.g. ``'store'``,
752752

753753
The recommended way to create a custom action is to extend :class:`Action`,
754754
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.
756757

757758
An example of a custom action::
758759

@@ -973,10 +974,11 @@ necessary type-checking and type conversions to be performed.
973974
If the type_ keyword is used with the default_ keyword, the type converter
974975
is only applied if the default is a string.
975976

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`)
977979
If the function raises :exc:`ArgumentTypeError`, :exc:`TypeError`, or
978980
: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.
980982

981983
Common built-in types and functions can be used as type converters:
982984

@@ -2103,6 +2105,34 @@ Intermixed parsing
21032105
.. versionadded:: 3.7
21042106

21052107

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+
21062136
Exceptions
21072137
----------
21082138

0 commit comments

Comments
 (0)