|
| 1 | +.. _argparse-tutorial: |
| 2 | + |
1 | 3 | *****************
|
2 | 4 | Argparse Tutorial
|
3 | 5 | *****************
|
4 | 6 |
|
5 | 7 | :author: Tshepang Mbambo
|
6 | 8 |
|
7 |
| -.. _argparse-tutorial: |
| 9 | +.. currentmodule:: argparse |
8 | 10 |
|
9 | 11 | This tutorial is intended to be a gentle introduction to :mod:`argparse`, the
|
10 | 12 | recommended command-line parsing module in the Python standard library.
|
11 | 13 |
|
12 | 14 | .. note::
|
13 | 15 |
|
14 | 16 | 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 |
16 | 18 | language) and the deprecated :mod:`optparse`.
|
17 | 19 | Note also that :mod:`argparse` is based on :mod:`optparse`,
|
18 | 20 | and therefore very similar in terms of usage.
|
@@ -137,13 +139,13 @@ And running the code:
|
137 | 139 |
|
138 | 140 | Here is what's happening:
|
139 | 141 |
|
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 |
141 | 143 | which command-line options the program is willing to accept. In this case,
|
142 | 144 | I've named it ``echo`` so that it's in line with its function.
|
143 | 145 |
|
144 | 146 | * Calling our program now requires us to specify an option.
|
145 | 147 |
|
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 |
147 | 149 | options specified, in this case, ``echo``.
|
148 | 150 |
|
149 | 151 | * The variable is some form of 'magic' that :mod:`argparse` performs for free
|
@@ -256,7 +258,7 @@ Here is what is happening:
|
256 | 258 |
|
257 | 259 | * To show that the option is actually optional, there is no error when running
|
258 | 260 | 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 |
260 | 262 | given ``None`` as a value, which is the reason it fails the truth
|
261 | 263 | test of the :keyword:`if` statement.
|
262 | 264 |
|
@@ -299,7 +301,7 @@ Here is what is happening:
|
299 | 301 | We even changed the name of the option to match that idea.
|
300 | 302 | Note that we now specify a new keyword, ``action``, and give it the value
|
301 | 303 | ``"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``. |
303 | 305 | Not specifying it implies ``False``.
|
304 | 306 |
|
305 | 307 | * It complains when you specify a value, in true spirit of what flags
|
@@ -698,7 +700,7 @@ Conflicting options
|
698 | 700 |
|
699 | 701 | So far, we have been working with two methods of an
|
700 | 702 | :class:`argparse.ArgumentParser` instance. Let's introduce a third one,
|
701 |
| -:meth:`add_mutually_exclusive_group`. It allows for us to specify options that |
| 703 | +:meth:`~ArgumentParser.add_mutually_exclusive_group`. It allows for us to specify options that |
702 | 704 | conflict with each other. Let's also change the rest of the program so that
|
703 | 705 | the new functionality makes more sense:
|
704 | 706 | we'll introduce the ``--quiet`` option,
|
|
0 commit comments