Skip to content
Myr

Merge pull request #538 from DenisCarriere/master

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

Add Float Range in Types

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents dac66dc, 3c0b985 · Browse files · 5 files+92 added−16 removed
This commit

State

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

Diff stats

5 files+92 added−16 removed

  • tests:+33 added−0 removed
  • other:+59 added−16 removed

5 files+92 added−16 removed

modified:click/__init__.py+2 added-2 removed
26 unmodified lines
27 password_option, version_option, help_option2829# Types30from .types import ParamType, File, Path, Choice, IntRange, Tuple, \31 STRING, INT, FLOAT, BOOL, UUID, UNPROCESSED31 STRING, INT, FLOAT, BOOL, UUID, UNPROCESSED, FloatRange3233# Utilities34from .utils import echo, get_binary_stream, get_text_stream, open_file, \35 format_filename, get_app_dir, get_os_args
29 unmodified lines
65 'version_option', 'help_option',6667 # Types68 'ParamType', 'File', 'Path', 'Choice', 'IntRange', 'Tuple', 'STRING',69 'INT', 'FLOAT', 'BOOL', 'UUID', 'UNPROCESSED',69 'INT', 'FLOAT', 'BOOL', 'UUID', 'UNPROCESSED', 'FloatRange'7071 # Utilities72 'echo', 'get_binary_stream', 'get_text_stream', 'open_file',73 'format_filename', 'get_app_dir', 'get_os_args',
25 unmodified lines
modified:click/types.py+53 added-14 removed
212 unmodified lines
213 def __repr__(self):214 return 'IntRange(%r, %r)' % (self.min, self.max)215216217class FloatParamType(ParamType):218 name = 'float'219220 def convert(self, value, param, ctx):221 try:222 return float(value)223 except (UnicodeError, ValueError):224 self.fail('%s is not a valid floating point value' %225 value, param, ctx)226227 def __repr__(self):228 return 'FLOAT'229230231class FloatRange(FloatParamType):232 """A parameter that works similar to :data:`click.FLOAT` but restricts233 the value to fit into a range. The default behavior is to fail if the234 value falls outside the range, but it can also be silently clamped235 between the two edges.236237 See :ref:`ranges` for an example.238 """239 name = 'float range'240241 def __init__(self, min=None, max=None, clamp=False):242 self.min = min243 self.max = max244 self.clamp = clamp245246 def convert(self, value, param, ctx):247 rv = FloatParamType.convert(self, value, param, ctx)248 if self.clamp:249 if self.min is not None and rv < self.min:250 return self.min251 if self.max is not None and rv > self.max:252 return self.max253 if self.min is not None and rv < self.min or \254 self.max is not None and rv > self.max:255 if self.min is None:256 self.fail('%s is bigger than the maximum valid value '257 '%s.' % (rv, self.max), param, ctx)258 elif self.max is None:259 self.fail('%s is smaller than the minimum valid value '260 '%s.' % (rv, self.min), param, ctx)261 else:262 self.fail('%s is not in the valid range of %s to %s.'263 % (rv, self.min, self.max), param, ctx)264 return rv265266 def __repr__(self):267 return 'FloatRange(%r, %r)' % (self.min, self.max)268269270class BoolParamType(ParamType):271 name = 'boolean'272273 def convert(self, value, param, ctx):
9 unmodified lines
283 def __repr__(self):284 return 'BOOL'285286234class FloatParamType(ParamType):235 name = 'float'236237 def convert(self, value, param, ctx):238 try:239 return float(value)240 except (UnicodeError, ValueError):241 self.fail('%s is not a valid floating point value' %242 value, param, ctx)243244 def __repr__(self):245 return 'FLOAT'246247287class UUIDParameterType(ParamType):288 name = 'uuid'289290 def convert(self, value, param, ctx):
301 unmodified lines
modified:docs/parameters.rst+3 added
65 unmodified lines
6667.. autoclass:: IntRange68 :noindex:6970.. autoclass:: FloatRange71 :noindex:7273Custom parameter types can be implemented by subclassing74:class:`click.ParamType`. For simple cases, passing a Python function that75fails with a `ValueError` is also supported, though discouraged.76
47 unmodified lines
modified:tests/test_basic.py+33 added
341 unmodified lines
342 assert not result.exception343 assert result.output == '0\n'344345346def test_float_range_option(runner):347 @click.command()348 @click.option('--x', type=click.FloatRange(0, 5))349 def cli(x):350 click.echo(x)351352 result = runner.invoke(cli, ['--x=5.0'])353 assert not result.exception354 assert result.output == '5.0\n'355356 result = runner.invoke(cli, ['--x=6.0'])357 assert result.exit_code == 2358 assert 'Invalid value for "--x": 6.0 is not in the valid range of 0 to 5.\n' \359 in result.output360361 @click.command()362 @click.option('--x', type=click.FloatRange(0, 5, clamp=True))363 def clamp(x):364 click.echo(x)365366 result = runner.invoke(clamp, ['--x=5.0'])367 assert not result.exception368 assert result.output == '5.0\n'369370 result = runner.invoke(clamp, ['--x=6.0'])371 assert not result.exception372 assert result.output == '5\n'373374 result = runner.invoke(clamp, ['--x=-1.0'])375 assert not result.exception376 assert result.output == '0\n'377378379def test_required_option(runner):380 @click.command()381 @click.option('--foo', required=True)382 def cli(foo):
94 unmodified lines
modified:CHANGES+1 added
8 unmodified lines
9(upcoming release with new features, release date to be decided)1011- The exception objects now store unicode properly.12- Added the ability to hide commands and options from help.13- Added Float Range in Types.1415Version 6.516-----------17
331 unmodified lines