summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2012-09-25 14:32:53 (GMT)
committerBarry Warsaw <barry@python.org>2012-09-25 14:32:53 (GMT)
commit0dea936e788a760e5d4dafb36b8584771b52da3c (patch)
treef7efe58e358b31dbc33e6f463ce91ba0fa82f768
parent671138f27dcdc3d259e85f7603acf01a46a44515 (diff)
downloadcpython-0dea936e788a760e5d4dafb36b8584771b52da3c.zip
cpython-0dea936e788a760e5d4dafb36b8584771b52da3c.tar.gz
cpython-0dea936e788a760e5d4dafb36b8584771b52da3c.tar.bz2
- Issue #15935: Clarification of argparse docs, re: add_argument() type and
default arguments. Patch contributed by Chris Jerdonek.
-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 8bb740e..2380a50 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -906,6 +906,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::
@@ -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())
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
factory FileType which takes the ``mode=`` and ``bufsize=`` arguments of the
``file`` object. For example, ``FileType('w')`` can be used to create a
diff --git a/Misc/NEWS b/Misc/NEWS
index fdd84fe..82b1769 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -442,6 +442,9 @@ Build
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
of JSON decoding functions.