@@ -192,6 +192,12 @@ arguments it contains. The default message can be overridden with the
192192The ``%(prog)s `` format specifier is available to fill in the program name in
193193your usage messages.
194194
195+ When a custom usage message is specified for the main parser, you may also want to
196+ consider passing the ``prog `` argument to :meth: `~ArgumentParser.add_subparsers `
197+ or the ``prog `` and the ``usage `` arguments to
198+ :meth: `~_SubParsersAction.add_parser `, to ensure consistent command prefixes and
199+ usage information across subparsers.
200+
195201
196202.. _description :
197203
@@ -583,6 +589,14 @@ are strings::
583589 >>> parser.parse_args(['--action', 'sumn', 1, 2, 3])
584590 tester.py: error: argument --action: invalid choice: 'sumn', maybe you meant 'sum'? (choose from 'sum', 'max')
585591
592+ If you're writing code that needs to be compatible with older Python versions
593+ and want to opportunistically use ``suggest_on_error `` when it's available, you
594+ can set it as an attribute after initializing the parser instead of using the
595+ keyword argument::
596+
597+ >>> parser = argparse.ArgumentParser(description='Process some integers.')
598+ >>> parser.suggest_on_error = True
599+
586600.. versionadded :: 3.14
587601
588602
@@ -801,7 +815,8 @@ Only actions that consume command-line arguments (e.g. ``'store'``,
801815
802816The recommended way to create a custom action is to extend :class: `Action `,
803817overriding the :meth: `!__call__ ` method and optionally the :meth: `!__init__ ` and
804- :meth: `!format_usage ` methods.
818+ :meth: `!format_usage ` methods. You can also register custom actions using the
819+ :meth: `~ArgumentParser.register ` method and reference them by their registered name.
805820
806821An example of a custom action::
807822
@@ -1020,10 +1035,11 @@ necessary type-checking and type conversions to be performed.
10201035If the type _ keyword is used with the default _ keyword, the type converter
10211036is only applied if the default is a string.
10221037
1023- The argument to ``type `` can be any callable that accepts a single string.
1038+ The argument to ``type `` can be a callable that accepts a single string or
1039+ the name of a registered type (see :meth: `~ArgumentParser.register `)
10241040If the function raises :exc: `ArgumentTypeError `, :exc: `TypeError `, or
10251041:exc: `ValueError `, the exception is caught and a nicely formatted error
1026- message is displayed. No other exception types are handled.
1042+ message is displayed. Other exception types are not handled.
10271043
10281044Common built-in types and functions can be used as type converters:
10291045
@@ -1808,6 +1824,10 @@ Sub-commands
18081824 .. versionchanged :: 3.7
18091825 New *required * keyword-only parameter.
18101826
1827+ .. versionchanged :: 3.14
1828+ Subparser's *prog * is no longer affected by a custom usage message in
1829+ the main parser.
1830+
18111831
18121832FileType objects
18131833^^^^^^^^^^^^^^^^
@@ -1906,11 +1926,10 @@ Argument groups
19061926 Note that any arguments not in your user-defined groups will end up back
19071927 in the usual "positional arguments" and "optional arguments" sections.
19081928
1909- .. versionchanged :: 3.11
1910- Calling :meth: `add_argument_group ` on an argument group is deprecated.
1911- This feature was never supported and does not always work correctly.
1912- The function exists on the API by accident through inheritance and
1913- will be removed in the future.
1929+ .. deprecated-removed :: 3.11 3.14
1930+ Calling :meth: `add_argument_group ` on an argument group now raises an
1931+ exception. This nesting was never supported, often failed to work
1932+ correctly, and was unintentionally exposed through inheritance.
19141933
19151934 .. deprecated :: 3.14
19161935 Passing prefix_chars _ to :meth: `add_argument_group `
@@ -1973,11 +1992,11 @@ Mutual exclusion
19731992 --foo FOO foo help
19741993 --bar BAR bar help
19751994
1976- .. versionchanged :: 3.11
1977- Calling :meth: `add_argument_group ` or :meth: `add_mutually_exclusive_group `
1978- on a mutually exclusive group is deprecated. These features were never
1979- supported and do not always work correctly. The functions exist on the
1980- API by accident through inheritance and will be removed in the future .
1995+ .. deprecated-removed :: 3.11 3.14
1996+ Calling :meth: `add_argument_group ` or :meth: `add_mutually_exclusive_group `
1997+ on a mutually exclusive group now raises an exception. This nesting was
1998+ never supported, often failed to work correctly, and was unintentionally
1999+ exposed through inheritance.
19812000
19822001
19832002Parser defaults
@@ -2163,6 +2182,34 @@ Intermixed parsing
21632182 .. versionadded :: 3.7
21642183
21652184
2185+ Registering custom types or actions
2186+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2187+
2188+ .. method :: ArgumentParser.register(registry_name, value, object)
2189+
2190+ Sometimes it's desirable to use a custom string in error messages to provide
2191+ more user-friendly output. In these cases, :meth: `!register ` can be used to
2192+ register custom actions or types with a parser and allow you to reference the
2193+ type by their registered name instead of their callable name.
2194+
2195+ The :meth: `!register ` method accepts three arguments - a *registry_name *,
2196+ specifying the internal registry where the object will be stored (e.g.,
2197+ ``action ``, ``type ``), *value *, which is the key under which the object will
2198+ be registered, and object, the callable to be registered.
2199+
2200+ The following example shows how to register a custom type with a parser::
2201+
2202+ >>> import argparse
2203+ >>> parser = argparse.ArgumentParser()
2204+ >>> parser.register('type', 'hexadecimal integer', lambda s: int(s, 16))
2205+ >>> parser.add_argument('--foo', type='hexadecimal integer')
2206+ _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)
2207+ >>> parser.parse_args(['--foo', '0xFA'])
2208+ Namespace(foo=250)
2209+ >>> parser.parse_args(['--foo', '1.2'])
2210+ usage: PROG [-h] [--foo FOO]
2211+ PROG: error: argument --foo: invalid 'hexadecimal integer' value: '1.2'
2212+
21662213Exceptions
21672214----------
21682215
0 commit comments