summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2012-09-25 14:47:55 (GMT)
committerBarry Warsaw <barry@python.org>2012-09-25 14:47:55 (GMT)
commitd09a05ebc51eb012b87eacdba99ff593d16ec37b (patch)
tree7aa0d5bb7f3c3fb1e38c51ae37af4e115b9b9149
parentdbe508870f9394b2c215795326f2b706f5a9dbca (diff)
parent1dedd0a4a4b88eeabb95af4b372a447b4d41b223 (diff)
downloadcpython-d09a05ebc51eb012b87eacdba99ff593d16ec37b.zip
cpython-d09a05ebc51eb012b87eacdba99ff593d16ec37b.tar.gz
cpython-d09a05ebc51eb012b87eacdba99ff593d16ec37b.tar.bz2
Merge 3.2 except the massive Misc/NEWS conflict, so hand apply the NEWS entry.
-rw-r--r--Doc/library/argparse.rst14
-rw-r--r--Misc/NEWS3
2 files changed, 17 insertions, 0 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index 8987622..9f6a1ea 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -923,6 +923,17 @@ was not present at the command line::
>>> parser.parse_args(''.split())
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
is used when no command-line argument was present::
@@ -961,6 +972,9 @@ types and functions can be used directly as the value of the ``type`` argument::
>>> parser.parse_args('2 temp.txt'.split())
Namespace(bar=<_io.TextIOWrapper name='temp.txt' encoding='UTF-8'>, 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
factory FileType which takes the ``mode=`` and ``bufsize=`` arguments of the
:func:`open` function. For example, ``FileType('w')`` can be used to create a
diff --git a/Misc/NEWS b/Misc/NEWS
index f705688..18d743b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -99,6 +99,9 @@ Build
Documentation
-------------
+- Issue #15935: Clarification of argparse docs, re: add_argument() type and
+ default arguments. Patch contributed by Chris Jerdonek.
+
- Issue #11964: Document a change in v3.2 to the behavior of the indent
parameter of json encoding operations.