Commit 0dea936e authored by Barry Warsaw's avatar Barry Warsaw

- Issue #15935: Clarification of argparse docs, re: add_argument() type and

  default arguments.  Patch contributed by Chris Jerdonek.
parent 671138f2
...@@ -906,6 +906,17 @@ was not present at the command line:: ...@@ -906,6 +906,17 @@ was not present at the command line::
>>> parser.parse_args(''.split()) >>> parser.parse_args(''.split())
Namespace(foo=42) Namespace(foo=42)
If the ``default`` value is a string, the parser parses the value as if it
were a command-line argument. In particular, the parser applies any type_
conversion argument, if provided, before setting the attribute on the
:class:`Namespace` return value. Otherwise, the parser uses the value as is::
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--length', default='10', type=int)
>>> parser.add_argument('--width', default=10.5, type=int)
>>> parser.parse_args()
Namespace(length=10, width=10.5)
For positional arguments with nargs_ equal to ``?`` or ``*``, the ``default`` value For positional arguments with nargs_ equal to ``?`` or ``*``, the ``default`` value
is used when no command-line argument was present:: is used when no command-line argument was present::
...@@ -944,6 +955,9 @@ types and functions can be used directly as the value of the ``type`` argument:: ...@@ -944,6 +955,9 @@ types and functions can be used directly as the value of the ``type`` argument::
>>> parser.parse_args('2 temp.txt'.split()) >>> parser.parse_args('2 temp.txt'.split())
Namespace(bar=<open file 'temp.txt', mode 'r' at 0x...>, foo=2) Namespace(bar=<open file 'temp.txt', mode 'r' at 0x...>, foo=2)
See the section on the default_ keyword argument for information on when the
``type`` argument is applied to default arguments.
To ease the use of various types of files, the argparse module provides the To ease the use of various types of files, the argparse module provides the
factory FileType which takes the ``mode=`` and ``bufsize=`` arguments of the factory FileType which takes the ``mode=`` and ``bufsize=`` arguments of the
``file`` object. For example, ``FileType('w')`` can be used to create a ``file`` object. For example, ``FileType('w')`` can be used to create a
......
...@@ -442,6 +442,9 @@ Build ...@@ -442,6 +442,9 @@ Build
Documentation Documentation
------------- -------------
- Issue #15935: Clarification of argparse docs, re: add_argument() type and
default arguments. Patch contributed by Chris Jerdonek.
- Issue #13769: Document the effect of ensure_ascii to the return type - Issue #13769: Document the effect of ensure_ascii to the return type
of JSON decoding functions. of JSON decoding functions.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment