Skip to content
Myr

Make ``BOOL`` accept the strings "on" and "off". Closes #1629.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by John T. Wodder II · committed by David Lord · parent 5eaa4c5 · Browse files · 4 files+8 added−7 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+8 added−7 removed

  • tests:+2 added−2 removed
  • other:+6 added−5 removed

4 files+8 added−7 removed

modified:docs/parameters.rst+3 added-3 removed
46 unmodified lines
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`.51 for boolean flags. If used with string values, ``1``, ``yes``, ``y``,52 ``t``, ``true``, and ``on`` convert to `True`, and ``0``, ``no``, ``n``,53 ``f``, ``false``, and ``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+2 added-2 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 ("true", "t", "1", "yes", "y", "on"):391 return True392 elif value in ("false", "f", "0", "no", "n"):392 elif value in ("false", "f", "0", "no", "n", "off"):393 return False394 self.fail(f"{value} is not a valid boolean", param, ctx)395396 def __repr__(self):
330 unmodified lines
modified:tests/test_basic.py+2 added-2 removed
204 unmodified lines
205 @click.option("--flag", default=default, type=bool)206 def cli(flag):207 click.echo(flag)208209 for value in "true", "t", "1", "yes", "y":209 for value in "true", "t", "1", "yes", "y", "on":210 result = runner.invoke(cli, ["--flag", value])211 assert not result.exception212 assert result.output == "True\n"213214 for value in "false", "f", "0", "no", "n":214 for value in "false", "f", "0", "no", "n", "off":215 result = runner.invoke(cli, ["--flag", value])216 assert not result.exception217 assert result.output == "False\n"218
341 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 now accepts the strings "on" and "off". :issue:`1629`323334Version 7.1.235-------------
635 unmodified lines