Skip to content
Myr

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

4 files+27 added−26 removed

modified:docs/parameters.rst+4 added-4 removed
45 unmodified lines
46``float`` / :data:`click.FLOAT`:47 A parameter that only accepts floating point values.4849``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``.5455:data:`click.UUID`:56 A parameter that accepts UUID values. This is not automatically57 guessed but represented as :class:`uuid.UUID`.
85 unmodified lines
modified:src/click/types.py+3 added-3 removed
385 unmodified lines
386 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)395396 def __repr__(self):397 return "BOOL"398
328 unmodified lines
modified:tests/test_basic.py+19 added-19 removed
1import os2import uuid3from itertools import chain45import pytest67import click8910def test_basic_functionality(runner):
189 unmodified lines
200 assert not result.exception201 assert result.output == f"{default}\n"202203201def 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)211def 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)216204 @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 == expect219209 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"222214 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"223219 result = runner.invoke(cli, [])220 assert not result.exception221 assert result.output == f"{default}\n"222223224def test_file_option(runner):225 @click.command()226 @click.option("--file", type=click.File("w"))227 def input(file):
332 unmodified lines
modified:CHANGES.rst+1 added
26 unmodified lines
27- 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`323334Version 7.1.235-------------
635 unmodified lines