Add Float Range in Types
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Denis Carriere · committed by Denis Carriere · parent 57c6f09 · Browse files · 4 files·+91 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
4 files+91 added−16 removed
- tests:+33 added−0 removed
- other:+58 added−16 removed
Provenance
- Commit
2be5923- Tree
3e1c848- Author
Denis Carriere <carriere.denis@gmail.com>- Committer
Denis Carriere <carriere.denis@gmail.com>- Parent
57c6f09
4 files changed+91 added−16 removed
modified:click/__init__.py+2 added-2 removed
26 unmodified lines27 password_option, version_option, help_option28 29 # Types30 from .types import ParamType, File, Path, Choice, IntRange, Tuple, \31- STRING, INT, FLOAT, BOOL, UUID, UNPROCESSED31+ STRING, INT, FLOAT, BOOL, UUID, UNPROCESSED, FloatRange32 33 # Utilities34 from .utils import echo, get_binary_stream, get_text_stream, open_file, \35 format_filename, get_app_dir, get_os_args29 unmodified lines65 'version_option', 'help_option',66 67 # Types68 'ParamType', 'File', 'Path', 'Choice', 'IntRange', 'Tuple', 'STRING',69- 'INT', 'FLOAT', 'BOOL', 'UUID', 'UNPROCESSED',69+ 'INT', 'FLOAT', 'BOOL', 'UUID', 'UNPROCESSED', 'FloatRange'70 71 # Utilities72 'echo', 'get_binary_stream', 'get_text_stream', 'open_file',73 'format_filename', 'get_app_dir', 'get_os_args',25 unmodified linesmodified:click/types.py+53 added-14 removed
212 unmodified lines213 def __repr__(self):214 return 'IntRange(%r, %r)' % (self.min, self.max)215 216 217+class FloatParamType(ParamType):218+ name = 'float'219+220+ 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)226+227+ def __repr__(self):228+ return 'FLOAT'229+230+231+class 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.236+237+ See :ref:`ranges` for an example.238+ """239+ name = 'float range'240+241+ def __init__(self, min=None, max=None, clamp=False):242+ self.min = min243+ self.max = max244+ self.clamp = clamp245+246+ 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 rv265+266+ def __repr__(self):267+ return 'FloatRange(%r, %r)' % (self.min, self.max)268+269+270 class BoolParamType(ParamType):271 name = 'boolean'272 273 def convert(self, value, param, ctx):9 unmodified lines283 def __repr__(self):284 return 'BOOL'285 286 234-class FloatParamType(ParamType):235- name = 'float'236-237- 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)243-244- def __repr__(self):245- return 'FLOAT'246-247-287 class UUIDParameterType(ParamType):288 name = 'uuid'289 290 def convert(self, value, param, ctx):299 unmodified linesmodified:docs/parameters.rst+3 added
65 unmodified lines66 67 .. autoclass:: IntRange68 :noindex:69 70+.. autoclass:: FloatRange71+ :noindex:72+73 Custom parameter types can be implemented by subclassing74 :class:`click.ParamType`. For simple cases, passing a Python function that75 fails with a `ValueError` is also supported, though discouraged.76 47 unmodified linesmodified:tests/test_basic.py+33 added
341 unmodified lines342 assert not result.exception343 assert result.output == '0\n'344 345 346+def 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)351+352+ result = runner.invoke(cli, ['--x=5.0'])353+ assert not result.exception354+ assert result.output == '5.0\n'355+356+ 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.output360+361+ @click.command()362+ @click.option('--x', type=click.FloatRange(0, 5, clamp=True))363+ def clamp(x):364+ click.echo(x)365+366+ result = runner.invoke(clamp, ['--x=5.0'])367+ assert not result.exception368+ assert result.output == '5.0\n'369+370+ result = runner.invoke(clamp, ['--x=6.0'])371+ assert not result.exception372+ assert result.output == '5\n'373+374+ result = runner.invoke(clamp, ['--x=-1.0'])375+ assert not result.exception376+ assert result.output == '0\n'377+378+379 def test_required_option(runner):380 @click.command()381 @click.option('--foo', required=True)382 def cli(foo):94 unmodified lines