@@ -26,73 +26,51 @@ 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
29
Core Functionality
33
- ^^^^^^^^^^^^^^^^^^
30
+ ------------------
34
31
35
32
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')
33
+ around an instance of :class: `argparse.ArgumentParser `. It is a container for
34
+ argument specifications and has options that apply the parser as whole::
54
35
36
+ parser = argparse.ArgumentParser(
37
+ prog = 'ProgramName',
38
+ description = 'What the program does',
39
+ epilog = 'Text at the bottom of help')
55
40
56
- Basic Usage of :func: `add_argument `
57
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41
+ The :func: `ArgumentParser.add_argument ` function attaches individual argument
42
+ specifications to the parser. It supports positional arguments, options that
43
+ accept values, and on/off flags::
58
44
45
+ parser.add_argument('filename') # positional argument
46
+ parser.add_argument('-c', '--count') # option that takes value
47
+ parser.add_argument('-v', '--verbose',
48
+ action='store_true') # on/off flag
59
49
60
- **Name or Flags Type **
50
+ The :func: `ArgumentParser.parse_args ` function runs the parser and puts
51
+ the extracted data in a :class: `argparse.Namespace ` object::
61
52
62
- ====================== ===========================
63
- Type Example
64
- ====================== ===========================
65
- Positional ``'foo' ``
66
- Optional ``'-v' ``, ``'--verbose' ``
67
- ====================== ===========================
53
+ args = parser.parse_args()
54
+ print(args.filename, args.count, args.verbose)
68
55
69
56
70
- **Basic Arguments: **
57
+ Quick Links for add_argument()
58
+ ------------------------------
71
59
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' ``
60
+ ====================== =========================================================== ==========================================================================================================================
61
+ Name Description Values
62
+ ====================== =========================================================== ==========================================================================================================================
63
+ action _ Specify how an argument should be handled ``'store' ``, ``'store_const' ``, ``'store_true' ``, ``'append' ``, ``'append_const' ``, ``'count' ``, ``'help' ``, ``'version' ``
64
+ choices _ Limit values to specific set of choices ``['foo', 'bar'] ``, ``range(1, 10) ``, or an object that supports ``in `` operator
65
+ const _ Store a constant value
76
66
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
-
67
+ dest _ Specify the attribute name in result namespace
68
+ help _ Help message for an argument
69
+ metavar _ Alternate display name for the argument as shown in help
70
+ nargs _ Number of times the argument can be used ``N `` (:class: `int `), ``'?' ``, ``'*' ``, ``'+' ``, ``argparse.REMAINDER ``
71
+ required _ Indicate whether an optional argument is required or not ``True ``, ``False ``
72
+ type _ Automatically convert an argument to the given type :class: `int `, :class: `float `, ``argparse.FileType('w') ``, or any callable function
73
+ ====================== =========================================================== ==========================================================================================================================
96
74
97
75
98
76
Example
0 commit comments