◐ Shell
clean mode source ↗

[3.11] gh-101100: Fix Sphinx warnings in `argparse` module (#103289) by hugovk · Pull Request #103803 · python/cpython

@@ -1,18 +1,20 @@ .. _argparse-tutorial:
***************** Argparse Tutorial *****************
:author: Tshepang Lekhonkhobe
.. _argparse-tutorial: .. currentmodule:: argparse
This tutorial is intended to be a gentle introduction to :mod:`argparse`, the recommended command-line parsing module in the Python standard library.
.. note::
There are two other modules that fulfill the same task, namely :mod:`getopt` (an equivalent for :c:func:`getopt` from the C :mod:`getopt` (an equivalent for ``getopt()`` from the C language) and the deprecated :mod:`optparse`. Note also that :mod:`argparse` is based on :mod:`optparse`, and therefore very similar in terms of usage. Expand Down Expand Up @@ -137,13 +139,13 @@ And running the code:
Here is what's happening:
* We've added the :meth:`add_argument` method, which is what we use to specify * We've added the :meth:`~ArgumentParser.add_argument` method, which is what we use to specify which command-line options the program is willing to accept. In this case, I've named it ``echo`` so that it's in line with its function.
* Calling our program now requires us to specify an option.
* The :meth:`parse_args` method actually returns some data from the * The :meth:`~ArgumentParser.parse_args` method actually returns some data from the options specified, in this case, ``echo``.
* The variable is some form of 'magic' that :mod:`argparse` performs for free Expand Down Expand Up @@ -256,7 +258,7 @@ Here is what is happening:
* To show that the option is actually optional, there is no error when running the program without it. Note that by default, if an optional argument isn't used, the relevant variable, in this case :attr:`args.verbosity`, is used, the relevant variable, in this case ``args.verbosity``, is given ``None`` as a value, which is the reason it fails the truth test of the :keyword:`if` statement.
Expand Down Expand Up @@ -299,7 +301,7 @@ Here is what is happening: We even changed the name of the option to match that idea. Note that we now specify a new keyword, ``action``, and give it the value ``"store_true"``. This means that, if the option is specified, assign the value ``True`` to :data:`args.verbose`. assign the value ``True`` to ``args.verbose``. Not specifying it implies ``False``.
* It complains when you specify a value, in true spirit of what flags Expand Down Expand Up @@ -669,7 +671,7 @@ Conflicting options
So far, we have been working with two methods of an :class:`argparse.ArgumentParser` instance. Let's introduce a third one, :meth:`add_mutually_exclusive_group`. It allows for us to specify options that :meth:`~ArgumentParser.add_mutually_exclusive_group`. It allows for us to specify options that conflict with each other. Let's also change the rest of the program so that the new functionality makes more sense: we'll introduce the ``--quiet`` option, Expand Down