mirror of
https://github.com/python/cpython.git
synced 2025-11-03 19:34:08 +00:00
Issue #8982: Improve the documentation for the argparse Namespace object. (Merge from 3.2.)
This commit is contained in:
commit
0a3a7d35ca
2 changed files with 18 additions and 5 deletions
|
|
@ -1330,13 +1330,24 @@ of :data:`sys.argv`. This can be accomplished by passing a list of strings to
|
||||||
Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])
|
Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])
|
||||||
|
|
||||||
|
|
||||||
Custom namespaces
|
The Namespace object
|
||||||
^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
By default, :meth:`parse_args` will return a new object of type :class:`Namespace`
|
||||||
|
where the necessary attributes have been set. This class is deliberately simple,
|
||||||
|
just an :class:`object` subclass with a readable string representation. If you
|
||||||
|
prefer to have dict-like view of the attributes, you can use the standard Python
|
||||||
|
idiom via :func:`vars`::
|
||||||
|
|
||||||
|
>>> parser = argparse.ArgumentParser()
|
||||||
|
>>> parser.add_argument('--foo')
|
||||||
|
>>> args = parser.parse_args(['--foo', 'BAR'])
|
||||||
|
>>> vars(args)
|
||||||
|
{'foo': 'BAR'}
|
||||||
|
|
||||||
It may also be useful to have an :class:`ArgumentParser` assign attributes to an
|
It may also be useful to have an :class:`ArgumentParser` assign attributes to an
|
||||||
already existing object, rather than the newly-created :class:`Namespace` object
|
already existing object, rather than a new :class:`Namespace` object. This can
|
||||||
that is normally used. This can be achieved by specifying the ``namespace=``
|
be achieved by specifying the ``namespace=`` keyword argument::
|
||||||
keyword argument::
|
|
||||||
|
|
||||||
>>> class C:
|
>>> class C:
|
||||||
... pass
|
... pass
|
||||||
|
|
|
||||||
|
|
@ -299,6 +299,8 @@ Library
|
||||||
|
|
||||||
- Issue #9348: Raise an early error if argparse nargs and metavar don't match.
|
- Issue #9348: Raise an early error if argparse nargs and metavar don't match.
|
||||||
|
|
||||||
|
- Issue #8982: Improve the documentation for the argparse Namespace object.
|
||||||
|
|
||||||
Build
|
Build
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue