summaryrefslogtreecommitdiffstats
path: root/Doc/library/argparse.rst
diff options
context:
space:
mode:
authorRémi Lapeyre <remi.lapeyre@henki.fr>2019-09-13 10:17:43 (GMT)
committerStéphane Wirtel <stephane@wirtel.be>2019-09-13 10:17:43 (GMT)
commit6a517c674907c195660fa9178a7b561de49cc721 (patch)
treee13407fbb1f8cdf1d38f3c04118de5fad01c1039 /Doc/library/argparse.rst
parent04f0bbfbedf8d2bb69b012f853de6648b1a9f27f (diff)
downloadcpython-6a517c674907c195660fa9178a7b561de49cc721.zip
cpython-6a517c674907c195660fa9178a7b561de49cc721.tar.gz
cpython-6a517c674907c195660fa9178a7b561de49cc721.tar.bz2
bpo-8538: Add support for boolean actions to argparse (GH-11478)
Co-Authored-By: remilapeyre <remi.lapeyre@henki.fr>
Diffstat (limited to 'Doc/library/argparse.rst')
-rw-r--r--Doc/library/argparse.rst19
1 files changed, 16 insertions, 3 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index 6dffd2e..a8aeca4 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -839,9 +839,19 @@ how the command-line arguments should be handled. The supplied actions are:
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
You may also specify an arbitrary action by passing an Action subclass or
-other object that implements the same interface. The recommended way to do
-this is to extend :class:`Action`, overriding the ``__call__`` method
-and optionally the ``__init__`` method.
+other object that implements the same interface. The ``BooleanOptionalAction``
+is available in ``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)
+
+The recommended way to create a custom action is to extend :class:`Action`,
+overriding the ``__call__`` method and optionally the ``__init__`` and
+``format_usage`` methods.
An example of a custom action::
@@ -1361,6 +1371,9 @@ Action instances should be callable, so subclasses must override the
The ``__call__`` method may perform arbitrary actions, but will typically set
attributes on the ``namespace`` based on ``dest`` and ``values``.
+Action subclasses can define a ``format_usage`` method that takes no argument
+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.
The parse_args() method
-----------------------