1
- :mod: `argparse ` --- Parser for command line options, arguments and sub-commands
1
+ :mod: `argparse ` --- Parser for command- line options, arguments and sub-commands
2
2
===============================================================================
3
3
4
4
.. module :: argparse
@@ -108,10 +108,10 @@ Parsing arguments
108
108
^^^^^^^^^^^^^^^^^
109
109
110
110
:class: `ArgumentParser ` parses args through the
111
- :meth: `~ArgumentParser.parse_args ` method. This will inspect the command- line,
111
+ :meth: `~ArgumentParser.parse_args ` method. This will inspect the command line,
112
112
convert each arg to the appropriate type and then invoke the appropriate action.
113
113
In most cases, this means a simple namespace object will be built up from
114
- attributes parsed out of the command- line::
114
+ attributes parsed out of the command line::
115
115
116
116
>>> parser.parse_args(['--sum', '7', '-1', '42'])
117
117
Namespace(accumulate=<built-in function sum>, integers=[7, -1, 42])
@@ -221,7 +221,7 @@ the parser's help message. For example, consider a file named
221
221
parser.add_argument('--foo', help='foo help')
222
222
args = parser.parse_args()
223
223
224
- If ``-h `` or ``--help `` is supplied at the command- line, the ArgumentParser
224
+ If ``-h `` or ``--help `` is supplied at the command line, the ArgumentParser
225
225
help will be printed::
226
226
227
227
$ python myprogram.py --help
@@ -578,21 +578,21 @@ The add_argument() method
578
578
[const], [default], [type], [choices], [required], \
579
579
[help], [metavar], [dest])
580
580
581
- Define how a single command line argument should be parsed. Each parameter
581
+ Define how a single command- line argument should be parsed. Each parameter
582
582
has its own more detailed description below, but in short they are:
583
583
584
584
* `name or flags `_ - Either a name or a list of option strings, e.g. ``foo ``
585
585
or ``-f, --foo ``
586
586
587
587
* action _ - The basic type of action to be taken when this argument is
588
- encountered at the command- line.
588
+ encountered at the command line.
589
589
590
590
* nargs _ - The number of command-line arguments that should be consumed.
591
591
592
592
* const _ - A constant value required by some action _ and nargs _ selections.
593
593
594
594
* default _ - The value produced if the argument is absent from the
595
- command- line.
595
+ command line.
596
596
597
597
* type _ - The type to which the command-line arg should be converted.
598
598
@@ -752,7 +752,7 @@ single action to be taken. The ``nargs`` keyword argument associates a
752
752
different number of command-line arguments with a single action.. The supported
753
753
values are:
754
754
755
- * N (an integer). N args from the command- line will be gathered together into a
755
+ * N (an integer). N args from the command line will be gathered together into a
756
756
list. For example::
757
757
758
758
>>> parser = argparse.ArgumentParser()
@@ -764,7 +764,7 @@ values are:
764
764
Note that ``nargs=1 `` produces a list of one item. This is different from
765
765
the default, in which the item is produced by itself.
766
766
767
- * ``'?' ``. One arg will be consumed from the command- line if possible, and
767
+ * ``'?' ``. One arg will be consumed from the command line if possible, and
768
768
produced as a single item. If no command-line arg is present, the value from
769
769
default _ will be produced. Note that for optional arguments, there is an
770
770
additional case - the option string is present but not followed by a
@@ -839,7 +839,7 @@ ArgumentParser actions. The two most common uses of it are:
839
839
840
840
* When :meth: `add_argument ` is called with option strings (like ``-f `` or
841
841
``--foo ``) and ``nargs='?' ``. This creates an optional argument that can be
842
- followed by zero or one command-line args. When parsing the command- line, if
842
+ followed by zero or one command-line args. When parsing the command line, if
843
843
the option string is encountered with no command-line arg following it, the
844
844
value of ``const `` will be assumed instead. See the nargs _ description for
845
845
examples.
@@ -851,7 +851,7 @@ default
851
851
^^^^^^^
852
852
853
853
All optional arguments and some positional arguments may be omitted at the
854
- command- line. The ``default `` keyword argument of :meth: `add_argument `, whose
854
+ command line. The ``default `` keyword argument of :meth: `add_argument `, whose
855
855
value defaults to ``None ``, specifies what value should be used if the
856
856
command-line arg is not present. For optional arguments, the ``default `` value
857
857
is used when the option string was not present at the command line::
@@ -949,7 +949,7 @@ choices
949
949
950
950
Some command-line args should be selected from a restricted set of values.
951
951
These can be handled by passing a container object as the ``choices `` keyword
952
- argument to :meth: `add_argument `. When the command- line is parsed, arg values
952
+ argument to :meth: `add_argument `. When the command line is parsed, arg values
953
953
will be checked, and an error message will be displayed if the arg was not one
954
954
of the acceptable values::
955
955
@@ -982,7 +982,7 @@ required
982
982
^^^^^^^^
983
983
984
984
In general, the argparse module assumes that flags like ``-f `` and ``--bar ``
985
- indicate *optional * arguments, which can always be omitted at the command- line.
985
+ indicate *optional * arguments, which can always be omitted at the command line.
986
986
To make an option *required *, ``True `` can be specified for the ``required= ``
987
987
keyword argument to :meth: `add_argument `::
988
988
@@ -1008,7 +1008,7 @@ help
1008
1008
1009
1009
The ``help `` value is a string containing a brief description of the argument.
1010
1010
When a user requests help (usually by using ``-h `` or ``--help `` at the
1011
- command- line), these ``help `` descriptions will be displayed with each
1011
+ command line), these ``help `` descriptions will be displayed with each
1012
1012
argument::
1013
1013
1014
1014
>>> parser = argparse.ArgumentParser(prog='frobble')
@@ -1179,7 +1179,7 @@ passed as two separate arguments::
1179
1179
Namespace(foo='FOO', x=None)
1180
1180
1181
1181
For long options (options with names longer than a single character), the option
1182
- and value can also be passed as a single command line argument, using ``= `` to
1182
+ and value can also be passed as a single command- line argument, using ``= `` to
1183
1183
separate them::
1184
1184
1185
1185
>>> parser.parse_args('--foo=FOO'.split())
@@ -1205,7 +1205,7 @@ as long as only the last option (or none of them) requires a value::
1205
1205
Invalid arguments
1206
1206
^^^^^^^^^^^^^^^^^
1207
1207
1208
- While parsing the command- line, ``parse_args `` checks for a variety of errors,
1208
+ While parsing the command line, ``parse_args `` checks for a variety of errors,
1209
1209
including ambiguous options, invalid types, invalid options, wrong number of
1210
1210
positional arguments, etc. When it encounters such an error, it exits and
1211
1211
prints the error along with a usage message::
@@ -1641,7 +1641,7 @@ Parser defaults
1641
1641
Most of the time, the attributes of the object returned by :meth: `parse_args `
1642
1642
will be fully determined by inspecting the command-line args and the argument
1643
1643
actions. :meth: `ArgumentParser.set_defaults ` allows some additional
1644
- attributes that are determined without any inspection of the command- line to
1644
+ attributes that are determined without any inspection of the command line to
1645
1645
be added::
1646
1646
1647
1647
>>> parser = argparse.ArgumentParser()
@@ -1712,7 +1712,7 @@ Partial parsing
1712
1712
1713
1713
.. method :: ArgumentParser.parse_known_args(args=None, namespace=None)
1714
1714
1715
- Sometimes a script may only parse a few of the command line arguments, passing
1715
+ Sometimes a script may only parse a few of the command- line arguments, passing
1716
1716
the remaining arguments on to another script or program. In these cases, the
1717
1717
:meth: `parse_known_args ` method can be useful. It works much like
1718
1718
:meth: `~ArgumentParser.parse_args ` except that it does not produce an error when
0 commit comments