Skip to content

Commit 8e76d7e

Browse files
authored
bpo-21150: Add quick link/summary table to the top of argparse documentation (GH-12005)
No work has been done to move this forward. On the theory that perfect is the enemy of good, I'm going to push it and we can make minor edits as needed afterwards.
1 parent 0e6dca0 commit 8e76d7e

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

Doc/library/argparse.rst

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,75 @@ module also automatically generates help and usage messages and issues errors
2626
when users give the program invalid arguments.
2727

2828

29+
Summary
30+
-------
31+
32+
Core Functionality
33+
^^^^^^^^^^^^^^^^^^
34+
35+
The :mod:`argparse` module's support for command-line interfaces is built
36+
from the following:
37+
38+
The :class:`argparse.ArgumentParser` creates a new :class:`ArgumentParser`
39+
object. Commonly used arguments include prog_, description_, and
40+
formatter_class_. For example, the user can create an instance of
41+
:class:`ArgumentParser` through the following::
42+
43+
>>> parser = argparse.ArgumentParser(prog='PROG', description='DESC',
44+
... formatter_class=argparse.RawDescriptionHelpFormatter)
45+
46+
The :func:`ArgumentParser.add_argument` is a function that is used
47+
to define how a single command-line argument should be parsed. Commonly used
48+
arguments include `name or flags`_, action_, default_, type_, required_,
49+
and help_. An example of the function :func:`ArgumentParser.add_argument`
50+
is as follows::
51+
52+
>>> parser.add_argument('-v', '--verbose', action='store_true',
53+
... help='Show various debugging information')
54+
55+
56+
Basic Usage of :func:`add_argument`
57+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
58+
59+
60+
**Name or Flags Type**
61+
62+
====================== ===========================
63+
Type Example
64+
====================== ===========================
65+
Positional ``'foo'``
66+
Optional ``'-v'``, ``'--verbose'``
67+
====================== ===========================
68+
69+
70+
**Basic Arguments:**
71+
72+
====================== =========================================================== =========================================================================================================================
73+
Name Description Keywords
74+
====================== =========================================================== =========================================================================================================================
75+
action_ Specifies how an argument should be handled ``'store'``, ``'store_const'``, ``'store_true'``, ``'append'``, ``'append_const'``, ``'count'``, ``'help'``, ``'version'``
76+
default_ Default value used when an argument is not provided
77+
type_ Automatically converts an argument to the given type :class:`int`, :class:`float`, :class:`bool`, ``argparse.FileType('w')``, ``callable function``
78+
help_ Help message of an argument
79+
====================== =========================================================== =========================================================================================================================
80+
81+
82+
83+
**Advanced Arguments:**
84+
85+
====================== =========================================================== =======================================================================================================================
86+
Name Description Keywords
87+
====================== =========================================================== =======================================================================================================================
88+
nargs_ Associates a single action with the number of arguments ``N`` (:class:`int`), ``'?'``, ``'*'``, ``'+'``, ``argparse.REMAINDER``
89+
const_ Stores constant values of names or flags
90+
choices_ A container that lists the possible values ``['foo', 'bar']``, ``range(1, 10)``, Any object that supports ``in`` operator
91+
required_ Indicates if an optional argument is required or not ``True``, ``False``
92+
metavar_ An alternative display name for the argument
93+
dest_ Specifies name of attribute to be used in ``parse_args()``
94+
====================== =========================================================== =======================================================================================================================
95+
96+
97+
2998
Example
3099
-------
31100

@@ -196,6 +265,8 @@ ArgumentParser objects
196265
The following sections describe how each of these are used.
197266

198267

268+
.. _prog:
269+
199270
prog
200271
^^^^
201272

@@ -293,6 +364,8 @@ The ``%(prog)s`` format specifier is available to fill in the program name in
293364
your usage messages.
294365

295366

367+
.. _description:
368+
296369
description
297370
^^^^^^^^^^^
298371

@@ -373,6 +446,8 @@ and one in the child) and raise an error.
373446
not be reflected in the child.
374447

375448

449+
.. _formatter_class:
450+
376451
formatter_class
377452
^^^^^^^^^^^^^^^
378453

@@ -716,6 +791,8 @@ The add_argument() method
716791
The following sections describe how each of these are used.
717792

718793

794+
.. _name_or_flags:
795+
719796
name or flags
720797
^^^^^^^^^^^^^
721798

@@ -749,6 +826,8 @@ be positional::
749826
PROG: error: the following arguments are required: bar
750827

751828

829+
.. _action:
830+
752831
action
753832
^^^^^^
754833

@@ -884,6 +963,9 @@ An example of a custom action::
884963

885964
For more details, see :class:`Action`.
886965

966+
967+
.. _nargs:
968+
887969
nargs
888970
^^^^^
889971

@@ -971,6 +1053,8 @@ is determined by the action_. Generally this means a single command-line argume
9711053
will be consumed and a single item (not a list) will be produced.
9721054

9731055

1056+
.. _const:
1057+
9741058
const
9751059
^^^^^
9761060

@@ -997,6 +1081,8 @@ the various :class:`ArgumentParser` actions. The two most common uses of it are
9971081
``const=None`` by default, including when ``action='append_const'`` or
9981082
``action='store_const'``.
9991083

1084+
.. _default:
1085+
10001086
default
10011087
^^^^^^^
10021088

@@ -1055,6 +1141,8 @@ command-line argument was not present::
10551141
Namespace(foo='1')
10561142

10571143

1144+
.. _type:
1145+
10581146
type
10591147
^^^^
10601148

@@ -1124,6 +1212,8 @@ For type checkers that simply check against a fixed set of values, consider
11241212
using the choices_ keyword instead.
11251213

11261214

1215+
.. _choices:
1216+
11271217
choices
11281218
^^^^^^^
11291219

@@ -1166,6 +1256,8 @@ from *dest*. This is usually what you want because the user never sees the
11661256
many choices), just specify an explicit metavar_.
11671257

11681258

1259+
.. _required:
1260+
11691261
required
11701262
^^^^^^^^
11711263

@@ -1192,6 +1284,8 @@ present at the command line.
11921284
*options* to be *optional*, and thus they should be avoided when possible.
11931285

11941286

1287+
.. _help:
1288+
11951289
help
11961290
^^^^
11971291

@@ -1247,6 +1341,8 @@ setting the ``help`` value to ``argparse.SUPPRESS``::
12471341
-h, --help show this help message and exit
12481342

12491343

1344+
.. _metavar:
1345+
12501346
metavar
12511347
^^^^^^^
12521348

@@ -1311,6 +1407,8 @@ arguments::
13111407
--foo bar baz
13121408

13131409

1410+
.. _dest:
1411+
13141412
dest
13151413
^^^^
13161414

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Place summary/quick links table at the top of the documentation for the argparse module to benefit ease of use.

0 commit comments

Comments
 (0)