summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.rst2
-rw-r--r--src/click/types.py10
-rw-r--r--tests/test_options.py11
3 files changed, 20 insertions, 3 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 548ac13..c27b103 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -111,6 +111,8 @@ Unreleased
completion system. :issue:`942`
- Include ``--help`` option in completion. :pr:`1504`
- ``ParameterSource`` is an ``enum.Enum`` subclass. :issue:`1530`
+- Boolean type strips surrounding space before converting.
+ :issue:`1605`
Version 7.1.2
diff --git a/src/click/types.py b/src/click/types.py
index b7161d9..721c556 100644
--- a/src/click/types.py
+++ b/src/click/types.py
@@ -497,11 +497,15 @@ class BoolParamType(ParamType):
def convert(self, value, param, ctx):
if isinstance(value, bool):
return bool(value)
- value = value.lower()
- if value in {"1", "true", "t", "yes", "y", "on"}:
+
+ norm = value.strip().lower()
+
+ if norm in {"1", "true", "t", "yes", "y", "on"}:
return True
- elif value in {"0", "false", "f", "no", "n", "off"}:
+
+ if norm in {"0", "false", "f", "no", "n", "off"}:
return False
+
self.fail(f"{value!r} is not a valid boolean value.", param, ctx)
def __repr__(self):
diff --git a/tests/test_options.py b/tests/test_options.py
index c57525c..e86a82a 100644
--- a/tests/test_options.py
+++ b/tests/test_options.py
@@ -164,6 +164,17 @@ def test_multiple_envvar(runner):
assert result.output == "foo|bar\n"
+def test_trailing_blanks_boolean_envvar(runner):
+ @click.command()
+ @click.option("--shout/--no-shout", envvar="SHOUT")
+ def cli(shout):
+ click.echo(f"shout: {shout!r}")
+
+ result = runner.invoke(cli, [], env={"SHOUT": " true "})
+ assert result.exit_code == 0
+ assert result.output == "shout: True\n"
+
+
def test_multiple_default_help(runner):
@click.command()
@click.option("--arg1", multiple=True, default=("foo", "bar"), show_default=True)