Skip to content

Commit 079b2c2

Browse files
Address PR comments
1 parent 5f206e9 commit 079b2c2

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

Doc/library/argparse.rst

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,41 +2167,26 @@ Registering custom types or actions
21672167

21682168
.. method:: ArgumentParser.register(registry_name, value, object)
21692169

2170-
It is possible to register custom actions, types, or other objects with
2171-
a parser using :meth:`!register`. This method is called when the action is
2172-
added to the parser. :meth:`!register` takes in the following arguments:
2170+
Sometimes it's desirable to use a custom string in error messages to provide
2171+
more user-friendly output. In these cases, :meth:`!register` can be used to
2172+
register custom actions or types with a parser and allow you to reference the
2173+
type by their registered name instead of their callable name.
21732174

2174-
* *registry_name* - the name of the internal registry where the object
2175-
will be stored (e.g. ``action``, ``type``).
2176-
2177-
* *value* - the key under which the object will be registered.
2178-
2179-
* *object* - the callable object to be registered.
2175+
The :meth:`!register` method accepts three arguments - a *registry_name*,
2176+
specifying the internal registry where the object will be stored (e.g.,
2177+
``action``, ``type``), *value*, which is the key under which the object will
2178+
be registered, and object, the callable to be registered.
21802179

21812180
The following example shows how to register a custom type with a parser::
21822181

21832182
>>> import argparse
2184-
>>> import enum
2185-
>>> class WinterMonths(enum.StrEnum):
2186-
... JAN = 'January'
2187-
... FEB = 'February'
2188-
... DEC = 'December'
2189-
...
2190-
>>> WinterMonths.__name__ = 'Winter Month'
21912183
>>> parser = argparse.ArgumentParser()
2192-
>>> parser.register('type', 'Winter Month', WinterMonths)
2193-
>>> parser.add_argument('--month', type=WinterMonths, action='store')
2194-
_StoreAction(option_strings=['--month'], dest='month', nargs=None, const=None, default=None, type=<enum 'Winter Month'>, choices=None, required=False, help=None, metavar=None, deprecated=False)
2195-
>>> parser.parse_args(['--month','January'])
2196-
Namespace(month=<Winter Month.JAN: 'January'>)
2197-
>>> parser.parse_args(['--month','March'])
2198-
usage: python.exe -m _pyrepl [-h] [--month MONTH]
2199-
python.exe -m _pyrepl: error: argument --month: invalid Winter Month value: 'March'
2200-
2201-
In this example, :meth:`!register` allows the type converter to be
2202-
referenced by its registered name instead of the class name. This approach
2203-
can improve error message clarity and provide more user-friendly output.
2204-
2184+
>>> parser.register('type', 'hexadecimal integer', lambda s: int(s, 16))
2185+
>>> parser.add_argument('--foo', type='hexadecimal integer')
2186+
_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)
2187+
>>> parser.parse_args(['--foo', '1.2'])
2188+
usage: PROG [-h] [--foo FOO]
2189+
PROG: error: argument --foo: invalid 'hexadecimal integer' value: '1.2'
22052190

22062191
Exceptions
22072192
----------

0 commit comments

Comments
 (0)