Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b52528e

Browse filesBrowse files
authored
Merge branch '3.11' into backport-7ef614c-3.11
2 parents f56f910 + 56d50dd commit b52528e
Copy full SHA for b52528e

File tree

Expand file treeCollapse file tree

12 files changed

+200
-95
lines changed
Filter options
Expand file treeCollapse file tree

12 files changed

+200
-95
lines changed

‎Doc/conf.py

Copy file name to clipboardExpand all lines: Doc/conf.py
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@
7171
if venvdir is not None:
7272
exclude_patterns.append(venvdir + '/*')
7373

74+
nitpick_ignore = [
75+
# Do not error nit-picky mode builds when _SubParsersAction.add_parser cannot
76+
# be resolved, as the method is currently undocumented. For context, see
77+
# https://github.com/python/cpython/pull/103289.
78+
('py:meth', '_SubParsersAction.add_parser'),
79+
]
80+
7481
# Disable Docutils smartquotes for several translations
7582
smartquotes_excludes = {
7683
'languages': ['ja', 'fr', 'zh_TW', 'zh_CN'], 'builders': ['man', 'text'],

‎Doc/howto/argparse.rst

Copy file name to clipboardExpand all lines: Doc/howto/argparse.rst
+9-7Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
.. _argparse-tutorial:
2+
13
*****************
24
Argparse Tutorial
35
*****************
46

57
:author: Tshepang Lekhonkhobe
68

7-
.. _argparse-tutorial:
9+
.. currentmodule:: argparse
810

911
This tutorial is intended to be a gentle introduction to :mod:`argparse`, the
1012
recommended command-line parsing module in the Python standard library.
1113

1214
.. note::
1315

1416
There are two other modules that fulfill the same task, namely
15-
:mod:`getopt` (an equivalent for :c:func:`getopt` from the C
17+
:mod:`getopt` (an equivalent for ``getopt()`` from the C
1618
language) and the deprecated :mod:`optparse`.
1719
Note also that :mod:`argparse` is based on :mod:`optparse`,
1820
and therefore very similar in terms of usage.
@@ -137,13 +139,13 @@ And running the code:
137139
138140
Here is what's happening:
139141

140-
* We've added the :meth:`add_argument` method, which is what we use to specify
142+
* We've added the :meth:`~ArgumentParser.add_argument` method, which is what we use to specify
141143
which command-line options the program is willing to accept. In this case,
142144
I've named it ``echo`` so that it's in line with its function.
143145

144146
* Calling our program now requires us to specify an option.
145147

146-
* The :meth:`parse_args` method actually returns some data from the
148+
* The :meth:`~ArgumentParser.parse_args` method actually returns some data from the
147149
options specified, in this case, ``echo``.
148150

149151
* The variable is some form of 'magic' that :mod:`argparse` performs for free
@@ -256,7 +258,7 @@ Here is what is happening:
256258

257259
* To show that the option is actually optional, there is no error when running
258260
the program without it. Note that by default, if an optional argument isn't
259-
used, the relevant variable, in this case :attr:`args.verbosity`, is
261+
used, the relevant variable, in this case ``args.verbosity``, is
260262
given ``None`` as a value, which is the reason it fails the truth
261263
test of the :keyword:`if` statement.
262264

@@ -299,7 +301,7 @@ Here is what is happening:
299301
We even changed the name of the option to match that idea.
300302
Note that we now specify a new keyword, ``action``, and give it the value
301303
``"store_true"``. This means that, if the option is specified,
302-
assign the value ``True`` to :data:`args.verbose`.
304+
assign the value ``True`` to ``args.verbose``.
303305
Not specifying it implies ``False``.
304306

305307
* It complains when you specify a value, in true spirit of what flags
@@ -669,7 +671,7 @@ Conflicting options
669671

670672
So far, we have been working with two methods of an
671673
:class:`argparse.ArgumentParser` instance. Let's introduce a third one,
672-
:meth:`add_mutually_exclusive_group`. It allows for us to specify options that
674+
:meth:`~ArgumentParser.add_mutually_exclusive_group`. It allows for us to specify options that
673675
conflict with each other. Let's also change the rest of the program so that
674676
the new functionality makes more sense:
675677
we'll introduce the ``--quiet`` option,

‎Doc/library/argparse.rst

Copy file name to clipboardExpand all lines: Doc/library/argparse.rst
+19-5Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ done downstream after the arguments are parsed.
11811181
For example, JSON or YAML conversions have complex error cases that require
11821182
better reporting than can be given by the ``type`` keyword. A
11831183
:exc:`~json.JSONDecodeError` would not be well formatted and a
1184-
:exc:`FileNotFound` exception would not be handled at all.
1184+
:exc:`FileNotFoundError` exception would not be handled at all.
11851185

11861186
Even :class:`~argparse.FileType` has its limitations for use with the ``type``
11871187
keyword. If one argument uses *FileType* and then a subsequent argument fails,
@@ -1435,7 +1435,7 @@ Action classes
14351435
Action classes implement the Action API, a callable which returns a callable
14361436
which processes arguments from the command-line. Any object which follows
14371437
this API may be passed as the ``action`` parameter to
1438-
:meth:`add_argument`.
1438+
:meth:`~ArgumentParser.add_argument`.
14391439

14401440
.. class:: Action(option_strings, dest, nargs=None, const=None, default=None, \
14411441
type=None, choices=None, required=False, help=None, \
@@ -1710,7 +1710,7 @@ Sub-commands
17101710
:class:`ArgumentParser` supports the creation of such sub-commands with the
17111711
:meth:`add_subparsers` method. The :meth:`add_subparsers` method is normally
17121712
called with no arguments and returns a special action object. This object
1713-
has a single method, :meth:`~ArgumentParser.add_parser`, which takes a
1713+
has a single method, :meth:`~_SubParsersAction.add_parser`, which takes a
17141714
command name and any :class:`ArgumentParser` constructor arguments, and
17151715
returns an :class:`ArgumentParser` object that can be modified as usual.
17161716

@@ -1776,7 +1776,7 @@ Sub-commands
17761776
for that particular parser will be printed. The help message will not
17771777
include parent parser or sibling parser messages. (A help message for each
17781778
subparser command, however, can be given by supplying the ``help=`` argument
1779-
to :meth:`add_parser` as above.)
1779+
to :meth:`~_SubParsersAction.add_parser` as above.)
17801780

17811781
::
17821782

@@ -2125,7 +2125,7 @@ the populated namespace and the list of remaining argument strings.
21252125

21262126
.. warning::
21272127
:ref:`Prefix matching <prefix-matching>` rules apply to
2128-
:meth:`parse_known_args`. The parser may consume an option even if it's just
2128+
:meth:`~ArgumentParser.parse_known_args`. The parser may consume an option even if it's just
21292129
a prefix of one of its known options, instead of leaving it in the remaining
21302130
arguments list.
21312131

@@ -2263,3 +2263,17 @@ A partial upgrade path from :mod:`optparse` to :mod:`argparse`:
22632263

22642264
* Replace the OptionParser constructor ``version`` argument with a call to
22652265
``parser.add_argument('--version', action='version', version='<the version>')``.
2266+
2267+
Exceptions
2268+
----------
2269+
2270+
.. exception:: ArgumentError
2271+
2272+
An error from creating or using an argument (optional or positional).
2273+
2274+
The string value of this exception is the message, augmented with
2275+
information about the argument that caused it.
2276+
2277+
.. exception:: ArgumentTypeError
2278+
2279+
Raised when something goes wrong converting a command line string to a type.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.