Skip to content
Myr

Merge pull request #842 from axu2/master

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

add bool conversions for t and f

authored by Markus Unterwaditzer · committed by GitHub · parents 3ded26d, 379d593 · Browse files · 4 files+27 added−4 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−4 removed

  • tests:+22 added−0 removed
  • other:+5 added−4 removed

4 files+27 added−4 removed

modified:click/types.py+2 added-2 removed
272 unmodified lines
273 def convert(self, value, param, ctx):274 if isinstance(value, bool):275 return bool(value)276 value = value.lower()277 if value in ('true', '1', 'yes', 'y'):277 if value in ('true', 't,' '1', 'yes', 'y'):278 return True279 elif value in ('false', '0', 'no', 'n'):279 elif value in ('false', 'f', '0', 'no', 'n'):280 return False281 self.fail('%s is not a valid boolean' % value, param, ctx)282283 def __repr__(self):
308 unmodified lines
modified:docs/parameters.rst+2 added-2 removed
45 unmodified lines
46 A parameter that only accepts floating point values.4748``bool`` / :data:`click.BOOL`:49 A parameter that accepts boolean values. This is automatically used50 for boolean flags. If used with string values ``1``, ``yes``, ``y``51 and ``true`` convert to `True` and ``0``, ``no``, ``n`` and ``false``50 for boolean flags. If used with string values ``1``, ``yes``, ``y``, ``t``51 and ``true`` convert to `True` and ``0``, ``no``, ``n``, ``f`` and ``false``52 convert to `False`.5354:data:`click.UUID`:55 A parameter that accepts UUID values. This is not automatically
68 unmodified lines
modified:tests/test_basic.py+22 added
178 unmodified lines
179 assert not result.exception180 assert result.output == '%s\n' % (default)181182183def test_boolean_conversion(runner):184 for default in True, False:185 @click.command()186 @click.option('--flag', default=default, type=bool)187 def cli(flag):188 click.echo(flag)189190 for value in 'true', 't,' '1', 'yes', 'y':191 result = runner.invoke(cli, ['--flag', value])192 assert not result.exception193 assert result.output == 'True\n'194195 for value in 'false', 'f', '0', 'no', 'n':196 result = runner.invoke(cli, ['--flag', value])197 assert not result.exception198 assert result.output == 'False\n'199200 result = runner.invoke(cli, [])201 assert not result.exception202 assert result.output == '%s\n' % default203204205def test_file_option(runner):206 @click.command()207 @click.option('--file', type=click.File('w'))208 def input(file):
290 unmodified lines
modified:CHANGES+1 added
23 unmodified lines
24- `CliRunner.invoke` now may receive `args` as a string representing25 a Unix shell command. See #664.26- Fix bug that caused bashcompletion to give inproper completions on 27 chained commands. See #774.28- 't' and 'f' are now converted to True and False. 2930Version 6.831-----------32
354 unmodified lines