[Python-checkins] cpython (merge 3.2 -> 3.3): Merge issue #11076: document the way to convert argparse.Namespace to a dict.

andrew.svetlov python-checkins at python.org
Wed Nov 28 18:18:37 CET 2012


http://hg.python.org/cpython/rev/ee4e31845977
changeset:   80622:ee4e31845977
branch:      3.3
parent:      80618:f18793b103de
parent:      80621:bbecbcff0ce4
user:        Andrew Svetlov <andrew.svetlov at gmail.com>
date:        Wed Nov 28 19:18:09 2012 +0200
summary:
  Merge issue #11076: document the way to convert argparse.Namespace to a dict.

Initial patch by Virgil Dupras.

files:
  Doc/library/argparse.rst |  15 +++++++++++++++
  1 files changed, 15 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
@@ -1444,6 +1444,21 @@
    'BAR'
 
 
+Converting the namespace to a dict
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+It's possible to convert a namespace to a :class:`dict` by using the built-in
+function :func:`vars` in this fashion::
+
+    args = parser.parse_args()
+    argdict = vars(args)
+
+This makes it easy to introspect the namespace or to pass the command-line
+arguments to a function taking a bunch of keyword arguments::
+
+    somefunction(**vars(parser.parse_args()))
+
+
 Other utilities
 ---------------
 

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


More information about the Python-checkins mailing list