]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #11076: document the way to convert argparse.Namespace to a dict.
authorAndrew Svetlov <andrew.svetlov@gmail.com>
Wed, 28 Nov 2012 17:17:26 +0000 (19:17 +0200)
committerAndrew Svetlov <andrew.svetlov@gmail.com>
Wed, 28 Nov 2012 17:17:26 +0000 (19:17 +0200)
Initial patch by Virgil Dupras.

Doc/library/argparse.rst

index 5273e9b12c4686c1d71379ac9abef012850cb674..b10df390e7af97b4e1d53635ac46d5910d1c1a7a 100644 (file)
@@ -1425,6 +1425,21 @@ be achieved by specifying the ``namespace=`` keyword argument::
    '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
 ---------------