@@ -839,23 +839,11 @@ how the command-line arguments should be handled. The supplied actions are:
839
839
>>> parser.parse_args(['--version'])
840
840
PROG 2.0
841
841
842
- Only actions that consume command-line arguments (e.g. ``'store' ``,
843
- ``'append' `` or ``'extend' ``) can be used with positional arguments.
844
-
845
- .. class :: BooleanOptionalAction
846
-
847
- You may also specify an arbitrary action by passing an :class: `Action ` subclass or
848
- other object that implements the same interface. The :class: `!BooleanOptionalAction `
849
- is available in :mod: `!argparse ` and adds support for boolean actions such as
850
- ``--foo `` and ``--no-foo ``::
851
-
852
- >>> import argparse
853
- >>> parser = argparse.ArgumentParser()
854
- >>> parser.add_argument('--foo', action=argparse.BooleanOptionalAction)
855
- >>> parser.parse_args(['--no-foo'])
856
- Namespace(foo=False)
857
-
858
- .. versionadded :: 3.9
842
+ You may also specify an arbitrary action by passing an :class: `Action ` subclass
843
+ (e.g. :class: `BooleanOptionalAction `) or other object that implements the same
844
+ interface. Only actions that consume command-line arguments (e.g. ``'store' ``,
845
+ ``'append' ``, ``'extend' ``, or custom actions with non-zero ``nargs ``) can be used
846
+ with positional arguments.
859
847
860
848
The recommended way to create a custom action is to extend :class: `Action `,
861
849
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
1429
1417
and return a string which will be used when printing the usage of the program.
1430
1418
If such method is not provided, a sensible default will be used.
1431
1419
1420
+ .. class :: BooleanOptionalAction
1421
+
1422
+ A subclass of :class: `Action ` for handling boolean flags with positive
1423
+ and negative options. Adding a single argument such as ``--foo `` automatically
1424
+ creates both ``--foo `` and ``--no-foo `` options, storing ``True `` and ``False ``
1425
+ respectively::
1426
+
1427
+ >>> import argparse
1428
+ >>> parser = argparse.ArgumentParser()
1429
+ >>> parser.add_argument('--foo', action=argparse.BooleanOptionalAction)
1430
+ >>> parser.parse_args(['--no-foo'])
1431
+ Namespace(foo=False)
1432
+
1433
+ .. versionadded :: 3.9
1434
+
1432
1435
1433
1436
The parse_args() method
1434
1437
-----------------------
0 commit comments