@@ -26,6 +26,75 @@ module also automatically generates help and usage messages and issues errors
26
26
when users give the program invalid arguments.
27
27
28
28
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
+
29
98
Example
30
99
-------
31
100
@@ -196,6 +265,8 @@ ArgumentParser objects
196
265
The following sections describe how each of these are used.
197
266
198
267
268
+ .. _prog :
269
+
199
270
prog
200
271
^^^^
201
272
@@ -293,6 +364,8 @@ The ``%(prog)s`` format specifier is available to fill in the program name in
293
364
your usage messages.
294
365
295
366
367
+ .. _description :
368
+
296
369
description
297
370
^^^^^^^^^^^
298
371
@@ -373,6 +446,8 @@ and one in the child) and raise an error.
373
446
not be reflected in the child.
374
447
375
448
449
+ .. _formatter_class :
450
+
376
451
formatter_class
377
452
^^^^^^^^^^^^^^^
378
453
@@ -716,6 +791,8 @@ The add_argument() method
716
791
The following sections describe how each of these are used.
717
792
718
793
794
+ .. _name_or_flags :
795
+
719
796
name or flags
720
797
^^^^^^^^^^^^^
721
798
@@ -749,6 +826,8 @@ be positional::
749
826
PROG: error: the following arguments are required: bar
750
827
751
828
829
+ .. _action :
830
+
752
831
action
753
832
^^^^^^
754
833
@@ -884,6 +963,9 @@ An example of a custom action::
884
963
885
964
For more details, see :class: `Action `.
886
965
966
+
967
+ .. _nargs :
968
+
887
969
nargs
888
970
^^^^^
889
971
@@ -971,6 +1053,8 @@ is determined by the action_. Generally this means a single command-line argume
971
1053
will be consumed and a single item (not a list) will be produced.
972
1054
973
1055
1056
+ .. _const :
1057
+
974
1058
const
975
1059
^^^^^
976
1060
@@ -997,6 +1081,8 @@ the various :class:`ArgumentParser` actions. The two most common uses of it are
997
1081
``const=None `` by default, including when ``action='append_const' `` or
998
1082
``action='store_const' ``.
999
1083
1084
+ .. _default :
1085
+
1000
1086
default
1001
1087
^^^^^^^
1002
1088
@@ -1055,6 +1141,8 @@ command-line argument was not present::
1055
1141
Namespace(foo='1')
1056
1142
1057
1143
1144
+ .. _type :
1145
+
1058
1146
type
1059
1147
^^^^
1060
1148
@@ -1124,6 +1212,8 @@ For type checkers that simply check against a fixed set of values, consider
1124
1212
using the choices _ keyword instead.
1125
1213
1126
1214
1215
+ .. _choices :
1216
+
1127
1217
choices
1128
1218
^^^^^^^
1129
1219
@@ -1166,6 +1256,8 @@ from *dest*. This is usually what you want because the user never sees the
1166
1256
many choices), just specify an explicit metavar _.
1167
1257
1168
1258
1259
+ .. _required :
1260
+
1169
1261
required
1170
1262
^^^^^^^^
1171
1263
@@ -1192,6 +1284,8 @@ present at the command line.
1192
1284
*options * to be *optional *, and thus they should be avoided when possible.
1193
1285
1194
1286
1287
+ .. _help :
1288
+
1195
1289
help
1196
1290
^^^^
1197
1291
@@ -1247,6 +1341,8 @@ setting the ``help`` value to ``argparse.SUPPRESS``::
1247
1341
-h, --help show this help message and exit
1248
1342
1249
1343
1344
+ .. _metavar :
1345
+
1250
1346
metavar
1251
1347
^^^^^^^
1252
1348
@@ -1311,6 +1407,8 @@ arguments::
1311
1407
--foo bar baz
1312
1408
1313
1409
1410
+ .. _dest :
1411
+
1314
1412
dest
1315
1413
^^^^
1316
1414
0 commit comments