Skip to content

Commit 32bc8e8

Browse files
[3.12] GH-122679: Add register() to argparse docs (GH-126939) (GH-127148)
(cherry picked from commit fcfdb55) Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
1 parent caad904 commit 32bc8e8

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

Doc/library/argparse.rst

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,8 @@ Only actions that consume command-line arguments (e.g. ``'store'``,
750750

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

755756
An example of a custom action::
756757

@@ -971,10 +972,11 @@ necessary type-checking and type conversions to be performed.
971972
If the type_ keyword is used with the default_ keyword, the type converter
972973
is only applied if the default is a string.
973974

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

979981
Common built-in types and functions can be used as type converters:
980982

@@ -2058,6 +2060,34 @@ Intermixed parsing
20582060
.. versionadded:: 3.7
20592061

20602062

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+
20612091
Exceptions
20622092
----------
20632093

0 commit comments

Comments
 (0)