summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--.pre-commit-config.yaml8
-rw-r--r--CHANGES.rst9
-rw-r--r--docs/.DS_Storebin8196 -> 0 bytes
-rw-r--r--docs/options.rst21
-rw-r--r--examples/README2
-rw-r--r--requirements/dev.txt33
-rw-r--r--requirements/docs.txt16
-rw-r--r--requirements/tests.txt2
-rw-r--r--requirements/typing.txt6
-rw-r--r--setup.cfg3
-rw-r--r--src/click/__init__.py2
-rw-r--r--src/click/core.py33
-rw-r--r--tests/test_formatting.py3
-rw-r--r--tests/test_options.py31
15 files changed, 117 insertions, 53 deletions
diff --git a/.gitignore b/.gitignore
index fc5a65f..c1a1723 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
/.idea/
/.vscode/
+.DS_Store/
/env/
/venv/
__pycache__/
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index ddc2ffb..7b2b746 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -2,17 +2,17 @@ ci:
autoupdate_schedule: monthly
repos:
- repo: https://github.com/asottile/pyupgrade
- rev: v2.15.0
+ rev: v2.25.0
hooks:
- id: pyupgrade
args: ["--py36-plus"]
- repo: https://github.com/asottile/reorder_python_imports
- rev: v2.5.0
+ rev: v2.6.0
hooks:
- id: reorder-python-imports
args: ["--application-directories", "src"]
- repo: https://github.com/psf/black
- rev: 21.5b1
+ rev: 21.8b0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
@@ -23,7 +23,7 @@ repos:
- flake8-bugbear
- flake8-implicit-str-concat
- repo: https://github.com/pre-commit/pre-commit-hooks
- rev: v3.4.0
+ rev: v4.0.1
hooks:
- id: fix-byte-order-marker
- id: trailing-whitespace
diff --git a/CHANGES.rst b/CHANGES.rst
index 38ec534..fa8063a 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,5 +1,14 @@
.. currentmodule:: click
+Version 8.1.0
+-------------
+
+Unreleased
+
+- Single options boolean flags with ``show_default=True`` only show
+ the default if it is ``True``. :issue:`1971`:
+
+
Version 8.0.2
-------------
diff --git a/docs/.DS_Store b/docs/.DS_Store
deleted file mode 100644
index 5a91862..0000000
--- a/docs/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/docs/options.rst b/docs/options.rst
index 277a98b..b54fbba 100644
--- a/docs/options.rst
+++ b/docs/options.rst
@@ -109,6 +109,27 @@ To show the default values when showing command help, use ``show_default=True``
invoke(dots, args=['--help'])
+For single option boolean flags, the default remains hidden if the default
+value is False.
+
+.. click:example::
+
+ @click.command()
+ @click.option('--n', default=1, show_default=True)
+ @click.option("--gr", is_flag=True, show_default=True, default=False, help="Greet the world.")
+ @click.option("--br", is_flag=True, show_default=True, default=True, help="Add a thematic break")
+ def dots(n, gr, br):
+ if gr:
+ click.echo('Hello world!')
+ click.echo('.' * n)
+ if br:
+ click.echo('-' * n)
+
+.. click:run::
+
+ invoke(dots, args=['--help'])
+
+
Multi Value Options
-------------------
diff --git a/examples/README b/examples/README
index 6be3296..566153f 100644
--- a/examples/README
+++ b/examples/README
@@ -9,4 +9,4 @@ Click Examples
through the wrong interpreter.
For more information about this see the documentation:
- https://click.palletsprojects.com/en/7.x/setuptools/
+ https://click.palletsprojects.com/setuptools/
diff --git a/requirements/dev.txt b/requirements/dev.txt
index fcc2526..3b95a8d 100644
--- a/requirements/dev.txt
+++ b/requirements/dev.txt
@@ -42,10 +42,10 @@ jinja2==3.0.1
# via sphinx
markupsafe==2.0.1
# via jinja2
+mypy==0.910
+ # via -r requirements/typing.in
mypy-extensions==0.4.3
# via mypy
-mypy==0.812
- # via -r requirements/typing.in
nodeenv==1.6.0
# via pre-commit
packaging==20.9
@@ -58,13 +58,13 @@ pallets-sphinx-themes==2.0.1
# via -r requirements/docs.in
pep517==0.10.0
# via pip-tools
-pip-tools==6.1.0
+pip-tools==6.2.0
# via -r requirements/dev.in
pluggy==0.13.1
# via
# pytest
# tox
-pre-commit==2.12.1
+pre-commit==2.14.1
# via -r requirements/dev.in
py==1.10.0
# via
@@ -76,7 +76,7 @@ pygments==2.9.0
# sphinx-tabs
pyparsing==2.4.7
# via packaging
-pytest==6.2.4
+pytest==6.2.5
# via -r requirements/tests.in
pytz==2021.1
# via babel
@@ -90,22 +90,22 @@ six==1.16.0
# virtualenv
snowballstemmer==2.1.0
# via sphinx
-sphinx-issues==1.2.0
- # via -r requirements/docs.in
-sphinx-tabs==3.0.0
- # via -r requirements/docs.in
-sphinx==4.0.2
+sphinx==4.1.2
# via
# -r requirements/docs.in
# pallets-sphinx-themes
# sphinx-issues
# sphinx-tabs
# sphinxcontrib-log-cabinet
+sphinx-issues==1.2.0
+ # via -r requirements/docs.in
+sphinx-tabs==3.2.0
+ # via -r requirements/docs.in
sphinxcontrib-applehelp==1.0.2
# via sphinx
sphinxcontrib-devhelp==1.0.2
# via sphinx
-sphinxcontrib-htmlhelp==1.0.3
+sphinxcontrib-htmlhelp==2.0.0
# via sphinx
sphinxcontrib-jsmath==1.0.1
# via sphinx
@@ -113,26 +113,27 @@ sphinxcontrib-log-cabinet==1.0.1
# via -r requirements/docs.in
sphinxcontrib-qthelp==1.0.3
# via sphinx
-sphinxcontrib-serializinghtml==1.1.4
+sphinxcontrib-serializinghtml==1.1.5
# via sphinx
toml==0.10.2
# via
+ # mypy
# pep517
# pre-commit
# pytest
# tox
-tox==3.23.1
+tox==3.24.3
# via -r requirements/dev.in
-typed-ast==1.4.3
- # via mypy
typing-extensions==3.10.0.0
# via mypy
-urllib3==1.26.4
+urllib3==1.26.5
# via requests
virtualenv==20.4.6
# via
# pre-commit
# tox
+wheel==0.36.2
+ # via pip-tools
# The following packages are considered to be unsafe in a requirements file:
# pip
diff --git a/requirements/docs.txt b/requirements/docs.txt
index 245f04b..a6b1a53 100644
--- a/requirements/docs.txt
+++ b/requirements/docs.txt
@@ -42,22 +42,22 @@ requests==2.25.1
# via sphinx
snowballstemmer==2.1.0
# via sphinx
-sphinx-issues==1.2.0
- # via -r requirements/docs.in
-sphinx-tabs==3.0.0
- # via -r requirements/docs.in
-sphinx==4.0.2
+sphinx==4.1.2
# via
# -r requirements/docs.in
# pallets-sphinx-themes
# sphinx-issues
# sphinx-tabs
# sphinxcontrib-log-cabinet
+sphinx-issues==1.2.0
+ # via -r requirements/docs.in
+sphinx-tabs==3.2.0
+ # via -r requirements/docs.in
sphinxcontrib-applehelp==1.0.2
# via sphinx
sphinxcontrib-devhelp==1.0.2
# via sphinx
-sphinxcontrib-htmlhelp==1.0.3
+sphinxcontrib-htmlhelp==2.0.0
# via sphinx
sphinxcontrib-jsmath==1.0.1
# via sphinx
@@ -65,9 +65,9 @@ sphinxcontrib-log-cabinet==1.0.1
# via -r requirements/docs.in
sphinxcontrib-qthelp==1.0.3
# via sphinx
-sphinxcontrib-serializinghtml==1.1.4
+sphinxcontrib-serializinghtml==1.1.5
# via sphinx
-urllib3==1.26.4
+urllib3==1.26.5
# via requests
# The following packages are considered to be unsafe in a requirements file:
diff --git a/requirements/tests.txt b/requirements/tests.txt
index 4ff31e3..89031f8 100644
--- a/requirements/tests.txt
+++ b/requirements/tests.txt
@@ -16,7 +16,7 @@ py==1.10.0
# via pytest
pyparsing==2.4.7
# via packaging
-pytest==6.2.4
+pytest==6.2.5
# via -r requirements/tests.in
toml==0.10.2
# via pytest
diff --git a/requirements/typing.txt b/requirements/typing.txt
index 0e342aa..fa04c8a 100644
--- a/requirements/typing.txt
+++ b/requirements/typing.txt
@@ -4,11 +4,11 @@
#
# pip-compile requirements/typing.in
#
+mypy==0.910
+ # via -r requirements/typing.in
mypy-extensions==0.4.3
# via mypy
-mypy==0.812
- # via -r requirements/typing.in
-typed-ast==1.4.3
+toml==0.10.2
# via mypy
typing-extensions==3.10.0.0
# via mypy
diff --git a/setup.cfg b/setup.cfg
index 45c6b89..7faca3a 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -69,6 +69,9 @@ ignore =
E722
# bin op line break, invalid
W503
+ # `raise exception from exc` required in except block
+ B904
+
# up to 88 allowed by bugbear B950
max-line-length = 80
per-file-ignores =
diff --git a/src/click/__init__.py b/src/click/__init__.py
index 969f4c4..8790877 100644
--- a/src/click/__init__.py
+++ b/src/click/__init__.py
@@ -72,4 +72,4 @@ from .utils import get_os_args as get_os_args
from .utils import get_text_stream as get_text_stream
from .utils import open_file as open_file
-__version__ = "8.0.2.dev0"
+__version__ = "8.1.0.dev0"
diff --git a/src/click/core.py b/src/click/core.py
index 5bbc90c..eb66e2a 100644
--- a/src/click/core.py
+++ b/src/click/core.py
@@ -2405,25 +2405,26 @@ class Option(Parameter):
All other parameters are passed onwards to the parameter constructor.
- :param show_default: controls if the default value should be shown on the
- help page. Normally, defaults are not shown. If this
- value is a string, it shows the string instead of the
- value. This is particularly useful for dynamic options.
- :param show_envvar: controls if an environment variable should be shown on
- the help page. Normally, environment variables
- are not shown.
- :param prompt: if set to `True` or a non empty string then the user will be
- prompted for input. If set to `True` the prompt will be the
- option name capitalized.
+ :param show_default: Controls if the default value should be shown
+ on the help page. Normally, defaults are not shown. If this
+ value is a string, it shows that string in parentheses instead
+ of the actual value. This is particularly useful for dynamic
+ options. For single option boolean flags, the default remains
+ hidden if its value is ``False``.
+ :param show_envvar: Controls if an environment variable should be
+ shown on the help page. Normally, environment ariables are not
+ shown.
+ :param prompt: If set to ``True`` or a non empty string then the
+ user will be prompted for input. If set to ``True`` the prompt
+ will be the option name capitalized.
:param confirmation_prompt: Prompt a second time to confirm the
value if it was prompted for. Can be set to a string instead of
``True`` to customize the message.
:param prompt_required: If set to ``False``, the user will be
prompted for input only when the option was specified as a flag
without a value.
- :param hide_input: if this is `True` then the input on the prompt will be
- hidden from the user. This is useful for password
- input.
+ :param hide_input: If this is ``True`` then the input on the prompt
+ will be hidden from the user. This is useful for password input.
:param is_flag: forces this option to act as a flag. The default is
auto detection.
:param flag_value: which value should be used for this flag if it's
@@ -2441,6 +2442,10 @@ class Option(Parameter):
:param help: the help string.
:param hidden: hide this option from help outputs.
+ .. versionchanged:: 8.1.0
+ The default of a single option boolean flag is not shown if the
+ default value is ``False``.
+
.. versionchanged:: 8.0.1
``type`` is detected from ``flag_value`` if given.
"""
@@ -2734,6 +2739,8 @@ class Option(Parameter):
default_string = split_opt(
(self.opts if self.default else self.secondary_opts)[0]
)[1]
+ elif self.is_bool_flag and not self.secondary_opts and not default_value:
+ default_string = ""
else:
default_string = str(default_value)
diff --git a/tests/test_formatting.py b/tests/test_formatting.py
index f957e01..1cbf32b 100644
--- a/tests/test_formatting.py
+++ b/tests/test_formatting.py
@@ -322,12 +322,13 @@ def test_global_show_default(runner):
pass
result = runner.invoke(cli, ["--help"])
+ # the default to "--help" is not shown because it is False
assert result.output.splitlines() == [
"Usage: cli [OPTIONS]",
"",
"Options:",
" -f TEXT Output file name [default: out.txt]",
- " --help Show this message and exit. [default: False]",
+ " --help Show this message and exit.",
]
diff --git a/tests/test_options.py b/tests/test_options.py
index 53179e8..efdda48 100644
--- a/tests/test_options.py
+++ b/tests/test_options.py
@@ -700,16 +700,37 @@ def test_show_default_boolean_flag_name(runner, default, expect):
assert f"[default: {expect}]" in message
-def test_show_default_boolean_flag_value(runner):
- """When a boolean flag only has one opt, it will show the default
- value, not the opt name.
+def test_show_true_default_boolean_flag_value(runner):
+ """When a boolean flag only has one opt and its default is True,
+ it will show the default value, not the opt name.
"""
opt = click.Option(
- ("--cache",), is_flag=True, show_default=True, help="Enable the cache."
+ ("--cache",),
+ is_flag=True,
+ show_default=True,
+ default=True,
+ help="Enable the cache.",
+ )
+ ctx = click.Context(click.Command("test"))
+ message = opt.get_help_record(ctx)[1]
+ assert "[default: True]" in message
+
+
+@pytest.mark.parametrize("default", [False, None])
+def test_hide_false_default_boolean_flag_value(runner, default):
+ """When a boolean flag only has one opt and its default is False or
+ None, it will not show the default
+ """
+ opt = click.Option(
+ ("--cache",),
+ is_flag=True,
+ show_default=True,
+ default=default,
+ help="Enable the cache.",
)
ctx = click.Context(click.Command("test"))
message = opt.get_help_record(ctx)[1]
- assert "[default: False]" in message
+ assert "[default: " not in message
def test_show_default_string(runner):