Merge pull request #1630 from jwodder/more-bools
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Make ``BOOL`` accept the strings "on" and "off"
authored by David Lord · committed by GitHub · parents 5eaa4c5, 9537f4c · Browse files · 4 files·+27 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+27 added−26 removed
- tests:+19 added−19 removed
- other:+8 added−7 removed
Provenance
- Commit
2815455- Tree
a301d4f- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
5eaa4c5- Parent 2
9537f4c
4 files changed+27 added−26 removed
modified:docs/parameters.rst+4 added-4 removed
45 unmodified lines46 ``float`` / :data:`click.FLOAT`:47 A parameter that only accepts floating point values.48 49 ``bool`` / :data:`click.BOOL`:50- A parameter that accepts boolean values. This is automatically used51- for boolean flags. If used with string values ``1``, ``yes``, ``y``, ``t``52- and ``true`` convert to `True` and ``0``, ``no``, ``n``, ``f`` and ``false``53- convert to `False`.50+ A parameter that accepts boolean values. This is automatically used51+ for boolean flags. The string values "1", "true", "t", "yes", "y",52+ and "on" convert to ``True``. "0", "false", "f", "no", "n", and53+ "off" convert to ``False``.54 55 :data:`click.UUID`:56 A parameter that accepts UUID values. This is not automatically57 guessed but represented as :class:`uuid.UUID`.85 unmodified linesmodified:src/click/types.py+3 added-3 removed
385 unmodified lines386 def convert(self, value, param, ctx):387 if isinstance(value, bool):388 return bool(value)389 value = value.lower()390- if value in ("true", "t", "1", "yes", "y"):390+ if value in {"1", "true", "t", "yes", "y", "on"}:391 return True392- elif value in ("false", "f", "0", "no", "n"):392+ elif value in {"0", "false", "f", "no", "n", "off"}:393 return False394- self.fail(f"{value} is not a valid boolean", param, ctx)394+ self.fail(f"{value!r} is not a valid boolean value.", param, ctx)395 396 def __repr__(self):397 return "BOOL"398 328 unmodified linesmodified:tests/test_basic.py+19 added-19 removed
1 import os2 import uuid3+from itertools import chain4 5+import pytest6+7 import click8 9 10 def test_basic_functionality(runner):189 unmodified lines200 assert not result.exception201 assert result.output == f"{default}\n"202 203 201-def test_boolean_conversion(runner):202- for default in True, False:204+@pytest.mark.parametrize(205+ ("value", "expect"),206+ chain(207+ ((x, "True") for x in ("1", "true", "t", "yes", "y", "on")),208+ ((x, "False") for x in ("0", "false", "f", "no", "n", "off")),209+ ),210+)211+def test_boolean_conversion(runner, value, expect):212+ @click.command()213+ @click.option("--flag", type=bool)214+ def cli(flag):215+ click.echo(flag, nl=False)216 204- @click.command()205- @click.option("--flag", default=default, type=bool)206- def cli(flag):207- click.echo(flag)217+ result = runner.invoke(cli, ["--flag", value])218+ assert result.output == expect219 209- for value in "true", "t", "1", "yes", "y":210- result = runner.invoke(cli, ["--flag", value])211- assert not result.exception220+ result = runner.invoke(cli, ["--flag", value.title()])221+ assert result.output == expect212- assert result.output == "True\n"222 214- for value in "false", "f", "0", "no", "n":215- result = runner.invoke(cli, ["--flag", value])216- assert not result.exception217- assert result.output == "False\n"223 219- result = runner.invoke(cli, [])220- assert not result.exception221- assert result.output == f"{default}\n"222-223-224 def test_file_option(runner):225 @click.command()226 @click.option("--file", type=click.File("w"))227 def input(file):332 unmodified linesmodified:CHANGES.rst+1 added
26 unmodified lines27 - Resolving commands returns the canonical command name instead of the28 matched name. This makes behavior such as help text and29 ``Context.invoked_subcommand`` consistent when using patterns like30 ``AliasedGroup``. :issue:`1422`31+- The ``BOOL`` type accepts the values "on" and "off". :issue:`1629`32 33 34 Version 7.1.235 -------------635 unmodified lines