summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2020-08-05 07:35:50 -0700
committerDavid Lord <davidism@gmail.com>2020-08-05 07:35:50 -0700
commit9537f4c17a1ba00184d7517b835c221715114e63 (patch)
treea301d4f6bf44311a1230e4ffdbc2b92895343e72
parentce498eb559e37852e6c69651a5176b4b6fcf28e8 (diff)
downloadclick-9537f4c17a1ba00184d7517b835c221715114e63.tar.gz
use sets for bool string check
update doc, changelog, test
-rw-r--r--CHANGES.rst2
-rw-r--r--docs/parameters.rst8
-rw-r--r--src/click/types.py6
-rw-r--r--tests/test_basic.py38
4 files changed, 27 insertions, 27 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 667f715..0fce1bc 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -28,7 +28,7 @@ Unreleased
matched name. This makes behavior such as help text and
``Context.invoked_subcommand`` consistent when using patterns like
``AliasedGroup``. :issue:`1422`
-- The ``BOOL`` type now accepts the strings "on" and "off". :issue:`1629`
+- The ``BOOL`` type accepts the values "on" and "off". :issue:`1629`
Version 7.1.2
diff --git a/docs/parameters.rst b/docs/parameters.rst
index 9ac721d..27c84ea 100644
--- a/docs/parameters.rst
+++ b/docs/parameters.rst
@@ -47,10 +47,10 @@ different behavior and some are supported out of the box:
A parameter that only accepts floating point values.
``bool`` / :data:`click.BOOL`:
- A parameter that accepts boolean values. This is automatically used
- for boolean flags. If used with string values, ``1``, ``yes``, ``y``,
- ``t``, ``true``, and ``on`` convert to `True`, and ``0``, ``no``, ``n``,
- ``f``, ``false``, and ``off`` convert to `False`.
+ A parameter that accepts boolean values. This is automatically used
+ for boolean flags. The string values "1", "true", "t", "yes", "y",
+ and "on" convert to ``True``. "0", "false", "f", "no", "n", and
+ "off" convert to ``False``.
:data:`click.UUID`:
A parameter that accepts UUID values. This is not automatically
diff --git a/src/click/types.py b/src/click/types.py
index 58bbc9c..d50786a 100644
--- a/src/click/types.py
+++ b/src/click/types.py
@@ -387,11 +387,11 @@ class BoolParamType(ParamType):
if isinstance(value, bool):
return bool(value)
value = value.lower()
- if value in ("true", "t", "1", "yes", "y", "on"):
+ if value in {"1", "true", "t", "yes", "y", "on"}:
return True
- elif value in ("false", "f", "0", "no", "n", "off"):
+ elif value in {"0", "false", "f", "no", "n", "off"}:
return False
- self.fail(f"{value} is not a valid boolean", param, ctx)
+ self.fail(f"{value!r} is not a valid boolean value.", param, ctx)
def __repr__(self):
return "BOOL"
diff --git a/tests/test_basic.py b/tests/test_basic.py
index 92e7963..9daccc2 100644
--- a/tests/test_basic.py
+++ b/tests/test_basic.py
@@ -1,5 +1,8 @@
import os
import uuid
+from itertools import chain
+
+import pytest
import click
@@ -198,27 +201,24 @@ def test_boolean_option(runner):
assert result.output == f"{default}\n"
-def test_boolean_conversion(runner):
- for default in True, False:
-
- @click.command()
- @click.option("--flag", default=default, type=bool)
- def cli(flag):
- click.echo(flag)
-
- for value in "true", "t", "1", "yes", "y", "on":
- result = runner.invoke(cli, ["--flag", value])
- assert not result.exception
- assert result.output == "True\n"
+@pytest.mark.parametrize(
+ ("value", "expect"),
+ chain(
+ ((x, "True") for x in ("1", "true", "t", "yes", "y", "on")),
+ ((x, "False") for x in ("0", "false", "f", "no", "n", "off")),
+ ),
+)
+def test_boolean_conversion(runner, value, expect):
+ @click.command()
+ @click.option("--flag", type=bool)
+ def cli(flag):
+ click.echo(flag, nl=False)
- for value in "false", "f", "0", "no", "n", "off":
- result = runner.invoke(cli, ["--flag", value])
- assert not result.exception
- assert result.output == "False\n"
+ result = runner.invoke(cli, ["--flag", value])
+ assert result.output == expect
- result = runner.invoke(cli, [])
- assert not result.exception
- assert result.output == f"{default}\n"
+ result = runner.invoke(cli, ["--flag", value.title()])
+ assert result.output == expect
def test_file_option(runner):