From: Andrew Svetlov Date: Wed, 28 Nov 2012 17:17:26 +0000 (+0200) Subject: Issue #11076: document the way to convert argparse.Namespace to a dict. X-Git-Tag: v3.2.4rc1~311 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e15cb61ddeb54bf4a5e6099520e5712c72ff15bf;p=thirdparty%2FPython%2Fcpython.git Issue #11076: document the way to convert argparse.Namespace to a dict. Initial patch by Virgil Dupras. --- diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 5273e9b12c46..b10df390e7af 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -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 ---------------