[Python-checkins] cpython (3.2): - Issue #15935: Clarification of argparse docs, re: add_argument() type and

barry.warsaw python-checkins at python.org
Tue Sep 25 16:48:37 CEST 2012


http://hg.python.org/cpython/rev/b738e42e664a
changeset:   79166:b738e42e664a
branch:      3.2
parent:      79160:d2df5bc89fc9
user:        Barry Warsaw <barry at python.org>
date:        Tue Sep 25 10:37:58 2012 -0400
summary:
  - Issue #15935: Clarification of argparse docs, re: add_argument() type and
  default arguments.  Patch contributed by Chris Jerdonek.

files:
  Doc/library/argparse.rst |  14 ++++++++++++++
  Misc/NEWS                |   3 +++
  2 files changed, 17 insertions(+), 0 deletions(-)


diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -904,6 +904,17 @@
    >>> 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::
 
@@ -942,6 +953,9 @@
    >>> 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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -589,6 +589,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list