@@ -26,6 +26,75 @@ module also automatically generates help and usage messages and issues errors
2626when 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+
2998Example
3099-------
31100
@@ -196,6 +265,8 @@ ArgumentParser objects
196265The following sections describe how each of these are used.
197266
198267
268+ .. _prog :
269+
199270prog
200271^^^^
201272
@@ -293,6 +364,8 @@ The ``%(prog)s`` format specifier is available to fill in the program name in
293364your usage messages.
294365
295366
367+ .. _description :
368+
296369description
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+
376451formatter_class
377452^^^^^^^^^^^^^^^
378453
@@ -716,6 +791,8 @@ The add_argument() method
716791The following sections describe how each of these are used.
717792
718793
794+ .. _name_or_flags :
795+
719796name or flags
720797^^^^^^^^^^^^^
721798
@@ -749,6 +826,8 @@ be positional::
749826 PROG: error: the following arguments are required: bar
750827
751828
829+ .. _action :
830+
752831action
753832^^^^^^
754833
@@ -884,6 +963,9 @@ An example of a custom action::
884963
885964For more details, see :class: `Action `.
886965
966+
967+ .. _nargs :
968+
887969nargs
888970^^^^^
889971
@@ -971,6 +1053,8 @@ is determined by the action_. Generally this means a single command-line argume
9711053will be consumed and a single item (not a list) will be produced.
9721054
9731055
1056+ .. _const :
1057+
9741058const
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+
10001086default
10011087^^^^^^^
10021088
@@ -1055,6 +1141,8 @@ command-line argument was not present::
10551141 Namespace(foo='1')
10561142
10571143
1144+ .. _type :
1145+
10581146type
10591147^^^^
10601148
@@ -1124,6 +1212,8 @@ For type checkers that simply check against a fixed set of values, consider
11241212using the choices _ keyword instead.
11251213
11261214
1215+ .. _choices :
1216+
11271217choices
11281218^^^^^^^
11291219
@@ -1166,6 +1256,8 @@ from *dest*. This is usually what you want because the user never sees the
11661256many choices), just specify an explicit metavar _.
11671257
11681258
1259+ .. _required :
1260+
11691261required
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+
11951289help
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+
12501346metavar
12511347^^^^^^^
12521348
@@ -1311,6 +1407,8 @@ arguments::
13111407 --foo bar baz
13121408
13131409
1410+ .. _dest :
1411+
13141412dest
13151413^^^^
13161414
0 commit comments