8000 gh-136032: Fix `argparse.BooleanOptionalAction` doc by mattwang44 · Pull Request #136133 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-136032: Fix argparse.BooleanOptionalAction doc #136133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
doc(argparse): move BooleanOptionalAction to Action classes section
  • Loading branch information
mattwang44 committed Jul 1, 2025
commit b708956fe2a839afe14d58749f70864668b39019
29 changes: 14 additions & 15 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -844,21 +844,6 @@ Only actions that consume command-line arguments (e.g. ``'store'``,
also specify an arbitrary action by passing an :class:`Action` subclass or
other object that implements the same interface.

.. 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 recommended way to create a custom action is to extend :class:`Action`,
overriding the :meth:`!__call__` method and optionally the :meth:`!__init__` and
:meth:`!format_usage` methods. You can also register custom actions using the
Expand Down Expand Up @@ -1431,6 +1416,20 @@ 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
-----------------------
Expand Down
Loading
0