Skip to content

Commit 99af040

Browse files
Docs editing
1 parent d0ccf81 commit 99af040

File tree

1 file changed

+65
-39
lines changed

1 file changed

+65
-39
lines changed

Doc/library/argparse.rst

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,69 +1700,55 @@ The Namespace object
17001700
Other utilities
17011701
---------------
17021702

1703-
Subcommands
1703+
Sub-commands
17041704
^^^^^^^^^^^^
17051705

17061706
.. method:: ArgumentParser.add_subparsers(*, [title], [description], [prog], \
17071707
[parser_class], [action], \
17081708
[dest], [required], \
17091709
[help], [metavar])
17101710

1711-
Many programs split up their functionality into a number of subcommands,
1712-
for example, the ``svn`` program can invoke subcommands like ``svn
1711+
Many programs split up their functionality into a number of sub-commands,
1712+
for example, the ``svn`` program can invoke sub-commands like ``svn
17131713
checkout``, ``svn update``, and ``svn commit``. Splitting up functionality
17141714
this way can be a particularly good idea when a program performs several
17151715
different functions which require different kinds of command-line arguments.
1716-
:class:`ArgumentParser` supports the creation of such subcommands with the
1716+
:class:`ArgumentParser` supports the creation of such sub-commands with the
17171717
:meth:`!add_subparsers` method. The :meth:`!add_subparsers` method is normally
17181718
called with no arguments and returns a special action object. This object
1719-
has a single method:
1719+
has a single method, :meth:`~_SubParsersAction.add_parser`, which takes a
1720+
command name and any :class:`!ArgumentParser` constructor arguments, and
1721+
returns an :class:`!ArgumentParser` object that can be modified as usual.
17201722

1721-
.. method:: _SubParsersAction.add_parser(name, *, help=None, aliases=None, deprecated=False, **kwargs)
1723+
Description of parameters:
17221724

1723-
Creates and returns a new :class:`!ArgumentParser` object for the
1724-
subcommand *name*.
1725+
* *title* - title for the sub-parser group in help output; by default
1726+
"subcommands" if description is provided, otherwise uses title for
1727+
positional arguments
17251728

1726-
The *name* argument is the name of the sub-command.
1729+
* *description* - description for the sub-parser group in help output, by
1730+
default ``None``
17271731

1728-
The *help* argument provides a short description for this sub-command.
1729-
If provided, it will be listed next to the command in the main parser’s
1730-
help message (e.g., ``PROG --help``).
17311732
* *prog* - usage information that will be displayed with subcommand help,
1732-
by default the name of the program and any positional arguments before the
1733-
subparser argument
1734-
1735-
The *aliases* argument allows providing alternative names for this
1736-
sub-command.
1733+
by default the name of the program and any positional arguments before the
1734+
subparser argument
17371735

1738-
For example::
1736+
* *parser_class* - class which will be used to create sub-parser instances, by
1737+
default the class of the current parser (e.g. :class:`ArgumentParser`)
17391738

1740-
>>> parser = argparse.ArgumentParser()
1741-
>>> subparsers = parser.add_subparsers()
1742-
>>> checkout = subparsers.add_parser('checkout', aliases=['co'])
1743-
>>> checkout.add_argument('foo')
1744-
>>> parser.parse_args(['co', 'bar'])
1745-
Namespace(foo='bar')
1739+
* action_ - the basic type of action to be taken when this argument is
1740+
encountered at the command line
17461741

1747-
The *deprecated* argument, if ``True``, marks the sub-command as
1748-
deprecated and will issue a warning when used.
1749-
1750-
.. versionadded:: 3.13
1751-
1752-
For example::
17531742
* dest_ - name of the attribute under which subcommand name will be
1754-
stored; by default ``None`` and no value is stored
1743+
stored; by default ``None`` and no value is stored
17551744

1756-
>>> parser = argparse.ArgumentParser(prog='chicken.py')
1757-
>>> subparsers = parser.add_subparsers()
1758-
>>> fly = subparsers.add_parser('fly', deprecated=True)
1759-
>>> parser.parse_args(['fly']) # doctest: +SKIP
1760-
chicken.py: warning: command 'fly' is deprecated
1761-
Namespace()
1745+
* required_ - Whether or not a subcommand must be provided, by default
1746+
``False`` (added in 3.7)
17621747

1748+
* help_ - help for sub-parser group in help output, by default ``None``
17631749

1764-
All other keyword arguments are passed directly to the
1765-
:class:`!ArgumentParser` constructor.
1750+
* metavar_ - string presenting available subcommands in help; by default it
1751+
is ``None`` and presents subcommands in form {cmd1, cmd2, ..}
17661752

17671753
Some example usage::
17681754

@@ -1908,7 +1894,47 @@ Subcommands
19081894
.. versionchanged:: 3.14
19091895
Subparser's *prog* is no longer affected by a custom usage message in
19101896
the main parser.
1897+
1898+
.. method:: _SubParsersAction.add_parser(name, *, help=None, aliases=None, deprecated=False, **kwargs)
1899+
1900+
Creates and returns a new :class:`!ArgumentParser` object for the
1901+
subcommand *name*.
19111902

1903+
The *name* argument is the name of the sub-command.
1904+
1905+
The *help* argument provides a short description for this sub-command.
1906+
If provided, it will be listed next to the command in the main parser’s
1907+
help message (e.g., ``PROG --help``).
1908+
1909+
The *aliases* argument allows providing alternative names for this
1910+
sub-command.
1911+
1912+
For example::
1913+
1914+
>>> parser = argparse.ArgumentParser()
1915+
>>> subparsers = parser.add_subparsers()
1916+
>>> checkout = subparsers.add_parser('checkout', aliases=['co'])
1917+
>>> checkout.add_argument('foo')
1918+
>>> parser.parse_args(['co', 'bar'])
1919+
Namespace(foo='bar')
1920+
1921+
The *deprecated* argument, if ``True``, marks the sub-command as
1922+
deprecated and will issue a warning when used.
1923+
1924+
.. versionadded:: 3.13
1925+
1926+
For example::
1927+
1928+
>>> parser = argparse.ArgumentParser(prog='chicken.py')
1929+
>>> subparsers = parser.add_subparsers()
1930+
>>> fly = subparsers.add_parser('fly', deprecated=True)
1931+
>>> parser.parse_args(['fly']) # doctest: +SKIP
1932+
chicken.py: warning: command 'fly' is deprecated
1933+
Namespace()
1934+
1935+
1936+
All other keyword arguments are passed directly to the
1937+
:class:`!ArgumentParser` constructor.
19121938

19131939
FileType objects
19141940
^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)