mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Issue #11076: document the way to convert argparse.Namespace to a dict.
Initial patch by Virgil Dupras.
This commit is contained in:
parent
c2a7fd60e1
commit
e15cb61dde
1 changed files with 15 additions and 0 deletions
|
@ -1425,6 +1425,21 @@ be achieved by specifying the ``namespace=`` keyword argument::
|
||||||
'BAR'
|
'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
|
Other utilities
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue