https://github.com/python/cpython/commit/1953713d0d67a4f54ff75bf8449895a2f08cc750
commit: 1953713d0d67a4f54ff75bf8449895a2f08cc750
branch: main
author: W. H. Wang <[email protected]>
committer: savannahostrowski <[email protected]>
date: 2025-07-05T15:54:26-07:00
summary:
gh-136032: Fix `argparse.BooleanOptionalAction` doc (#136133)
files:
M Doc/library/argparse.rst
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index a03d88092dbf1f..f189f6b8fa8953 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -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
-----------------------
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]