diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 5cfe047d746f68..8a0a9b407fd845 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -1716,39 +1716,53 @@ Subcommands :class:`ArgumentParser` supports the creation of such subcommands with the :meth:`!add_subparsers` method. The :meth:`!add_subparsers` method is normally called with no arguments and returns a special action object. This object - has a single method, :meth:`~_SubParsersAction.add_parser`, which takes a - command name and any :class:`!ArgumentParser` constructor arguments, and - returns an :class:`!ArgumentParser` object that can be modified as usual. + has a single method: - Description of parameters: +.. method:: _SubParsersAction.add_parser(name, *, help=None, aliases=None, deprecated=False, **kwargs) - * *title* - title for the sub-parser group in help output; by default - "subcommands" if description is provided, otherwise uses title for - positional arguments + Creates and returns a new :class:`!ArgumentParser` object for the + subcommand *name*. - * *description* - description for the sub-parser group in help output, by - default ``None`` + The *name* argument is the name of the sub-command. + The *help* argument provides a short description for this sub-command. + If provided, it will be listed next to the command in the main parser’s + help message (e.g., ``PROG --help``). * *prog* - usage information that will be displayed with subcommand help, by default the name of the program and any positional arguments before the subparser argument - * *parser_class* - class which will be used to create sub-parser instances, by - default the class of the current parser (e.g. :class:`ArgumentParser`) + The *aliases* argument allows providing alternative names for this + sub-command. - * action_ - the basic type of action to be taken when this argument is - encountered at the command line + For example:: + + >>> parser = argparse.ArgumentParser() + >>> subparsers = parser.add_subparsers() + >>> checkout = subparsers.add_parser('checkout', aliases=['co']) + >>> checkout.add_argument('foo') + >>> parser.parse_args(['co', 'bar']) + Namespace(foo='bar') + + The *deprecated* argument, if ``True``, marks the sub-command as + deprecated and will issue a warning when used. + + .. versionadded:: 3.13 + For example:: * dest_ - name of the attribute under which subcommand name will be stored; by default ``None`` and no value is stored - * required_ - Whether or not a subcommand must be provided, by default - ``False`` (added in 3.7) + >>> parser = argparse.ArgumentParser(prog='chicken.py') + >>> subparsers = parser.add_subparsers() + >>> fly = subparsers.add_parser('fly', deprecated=True) + >>> parser.parse_args(['fly']) # doctest: +SKIP + chicken.py: warning: command 'fly' is deprecated + Namespace() - * help_ - help for sub-parser group in help output, by default ``None`` - * metavar_ - string presenting available subcommands in help; by default it - is ``None`` and presents subcommands in form {cmd1, cmd2, ..} + All other keyword arguments are passed directly to the + :class:`!ArgumentParser` constructor. Some example usage:: @@ -1771,7 +1785,7 @@ Subcommands >>> parser.parse_args(['--foo', 'b', '--baz', 'Z']) Namespace(baz='Z', foo=True) - Note that the object returned by :meth:`parse_args` will only contain + Note that the object returned by :meth:`~ArgumentParser.parse_args` will only contain attributes for the main parser and the subparser that was selected by the command line (and not any other subparsers). So in the example above, when the ``a`` command is specified, only the ``foo`` and ``bar`` attributes are @@ -1782,7 +1796,7 @@ Subcommands for that particular parser will be printed. The help message will not include parent parser or sibling parser messages. (A help message for each subparser command, however, can be given by supplying the ``help=`` argument - to :meth:`~_SubParsersAction.add_parser` as above.) + to ``add_parser()`` as above.) :: @@ -1814,7 +1828,7 @@ Subcommands -h, --help show this help message and exit --baz {X,Y,Z} baz help - The :meth:`add_subparsers` method also supports ``title`` and ``description`` + The :meth:`~ArgumentParser.add_subparsers` method also supports ``title`` and ``description`` keyword arguments. When either is present, the subparser's commands will appear in their own group in the help output. For example:: @@ -1835,34 +1849,8 @@ Subcommands {foo,bar} additional help - Furthermore, :meth:`~_SubParsersAction.add_parser` supports an additional - *aliases* argument, - which allows multiple strings to refer to the same subparser. This example, - like ``svn``, aliases ``co`` as a shorthand for ``checkout``:: - - >>> parser = argparse.ArgumentParser() - >>> subparsers = parser.add_subparsers() - >>> checkout = subparsers.add_parser('checkout', aliases=['co']) - >>> checkout.add_argument('foo') - >>> parser.parse_args(['co', 'bar']) - Namespace(foo='bar') - - :meth:`~_SubParsersAction.add_parser` supports also an additional - *deprecated* argument, which allows to deprecate the subparser. - - >>> import argparse - >>> parser = argparse.ArgumentParser(prog='chicken.py') - >>> subparsers = parser.add_subparsers() - >>> run = subparsers.add_parser('run') - >>> fly = subparsers.add_parser('fly', deprecated=True) - >>> parser.parse_args(['fly']) # doctest: +SKIP - chicken.py: warning: command 'fly' is deprecated - Namespace() - - .. versionadded:: 3.13 - One particularly effective way of handling subcommands is to combine the use - of the :meth:`add_subparsers` method with calls to :meth:`set_defaults` so + of the :meth:`~ArgumentParser.add_subparsers` method with calls to :meth:`~ArgumentParser.set_defaults` so that each subparser knows which Python function it should execute. For example:: @@ -1898,12 +1886,12 @@ Subcommands >>> args.func(args) ((XYZYX)) - This way, you can let :meth:`parse_args` do the job of calling the + This way, you can let :meth:`~ArgumentParser.parse_args` do the job of calling the appropriate function after argument parsing is complete. Associating functions with actions like this is typically the easiest way to handle the different actions for each of your subparsers. However, if it is necessary to check the name of the subparser that was invoked, the ``dest`` keyword - argument to the :meth:`add_subparsers` call will work:: + argument to the :meth:`~ArgumentParser.add_subparsers` call will work:: >>> parser = argparse.ArgumentParser() >>> subparsers = parser.add_subparsers(dest='subparser_name') diff --git a/Misc/NEWS.d/next/Documentation/2025-10-25-14-08-42.gh-issue-84116.lKcoHw.rst b/Misc/NEWS.d/next/Documentation/2025-10-25-14-08-42.gh-issue-84116.lKcoHw.rst new file mode 100644 index 00000000000000..3a0ea841da94d3 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2025-10-25-14-08-42.gh-issue-84116.lKcoHw.rst @@ -0,0 +1,3 @@ +Document ``help`` and ``aliases`` parameters for +``argparse._SubParsersAction.add_parser`` in the :mod:`argparse` +documentation.