mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-136032: Fix argparse.BooleanOptionalAction
doc (#136133)
Some checks are pending
Tests / Windows MSI (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Undefined behavior sanitizer (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run
Some checks are pending
Tests / Windows MSI (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Undefined behavior sanitizer (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run
This commit is contained in:
parent
5dac137b9f
commit
1953713d0d
1 changed files with 20 additions and 17 deletions
|
@ -839,23 +839,11 @@ how the command-line arguments should be handled. The supplied actions are:
|
|||
>>> parser.parse_args(['--version'])
|
||||
PROG 2.0
|
||||
|
||||
Only actions that consume command-line arguments (e.g. ``'store'``,
|
||||
``'append'`` or ``'extend'``) can be used with positional arguments.
|
||||
|
||||
.. class:: BooleanOptionalAction
|
||||
|
||||
You may also specify an arbitrary action by passing an :class:`Action` subclass or
|
||||
other object that implements the same interface. The :class:`!BooleanOptionalAction`
|
||||
is available in :mod:`!argparse` and adds support for boolean actions such as
|
||||
``--foo`` and ``--no-foo``::
|
||||
|
||||
>>> import argparse
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--foo', action=argparse.BooleanOptionalAction)
|
||||
>>> parser.parse_args(['--no-foo'])
|
||||
Namespace(foo=False)
|
||||
|
||||
.. versionadded:: 3.9
|
||||
You may also specify an arbitrary action by passing an :class:`Action` subclass
|
||||
(e.g. :class:`BooleanOptionalAction`) or other object that implements the same
|
||||
interface. Only actions that consume command-line arguments (e.g. ``'store'``,
|
||||
``'append'``, ``'extend'``, or custom actions with non-zero ``nargs``) can be used
|
||||
with positional arguments.
|
||||
|
||||
The recommended way to create a custom action is to extend :class:`Action`,
|
||||
overriding the :meth:`!__call__` method and optionally the :meth:`!__init__` and
|
||||
|
@ -1429,6 +1417,21 @@ this API may be passed as the ``action`` parameter to
|
|||
and return a string which will be used when printing the usage of the program.
|
||||
If such method is not provided, a sensible default will be used.
|
||||
|
||||
.. class:: BooleanOptionalAction
|
||||
|
||||
A subclass of :class:`Action` for handling boolean flags with positive
|
||||
and negative options. Adding a single argument such as ``--foo`` automatically
|
||||
creates both ``--foo`` and ``--no-foo`` options, storing ``True`` and ``False``
|
||||
respectively::
|
||||
|
||||
>>> import argparse
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--foo', action=argparse.BooleanOptionalAction)
|
||||
>>> parser.parse_args(['--no-foo'])
|
||||
Namespace(foo=False)
|
||||
|
||||
.. versionadded:: 3.9
|
||||
|
||||
|
||||
The parse_args() method
|
||||
-----------------------
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue