mirror of
https://github.com/python/cpython.git
synced 2026-04-15 00:00:57 +00:00
[3.14] gh-126676: Expand argparse docs for type=bool with warning and alternatives (GH-146435) (#148048)
gh-126676: Expand argparse docs for type=bool with warning and alternatives (GH-146435)
(cherry picked from commit 80d0a85d96)
Co-authored-by: Joshua Swanson <22283299+joshuaswanson@users.noreply.github.com>
Co-authored-by: joshuaswanson <joshuaswanson@users.noreply.github.com>
Co-authored-by: Savannah Ostrowski <savannah@python.org>
This commit is contained in:
parent
594b5a05dc
commit
f4c9bc899b
2 changed files with 11 additions and 1 deletions
|
|
@ -1111,7 +1111,15 @@ User defined functions can be used as well:
|
|||
|
||||
The :func:`bool` function is not recommended as a type converter. All it does
|
||||
is convert empty strings to ``False`` and non-empty strings to ``True``.
|
||||
This is usually not what is desired.
|
||||
This is usually not what is desired::
|
||||
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> _ = parser.add_argument('--verbose', type=bool)
|
||||
>>> parser.parse_args(['--verbose', 'False'])
|
||||
Namespace(verbose=True)
|
||||
|
||||
See :class:`BooleanOptionalAction` or ``action='store_true'`` for common
|
||||
alternatives.
|
||||
|
||||
In general, the ``type`` keyword is a convenience that should only be used for
|
||||
simple conversions that can only raise one of the three supported exceptions.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
Expand :mod:`argparse` documentation for ``type=bool`` with a demonstration
|
||||
of the surprising behavior and pointers to common alternatives.
|
||||
Loading…
Add table
Add a link
Reference in a new issue