1from __future__
import print_function
4from click
import echo, style, secho
5from .click_texttable
import Texttable
9 """A parameter that works similar to :data:`click.INT` but restricts
10 the value to fit into a range. The default behavior is to fail if the
11 value falls outside the range, but it can also be silently clamped
12 between the two edges.
14 See :ref:`ranges` for an example.
16 name =
'integer range'
18 def __init__(self, min=None, max=None, clamp=False):
25 if type(value) == str:
26 if value.startswith(
'0x'):
28 elif value.startswith(
'0o'):
30 elif value.startswith(
'0b'):
38 if self.
min is not None and rv < self.
min:
40 if self.
max is not None and rv > self.
max:
42 if self.
min is not None and rv < self.
min or \
43 self.
max is not None and rv > self.
max:
45 self.fail(
'%s is bigger than the maximum valid value '
46 '%s.' % (rv, self.
max), param, ctx)
47 elif self.
max is None:
48 self.fail(
'%s is smaller than the minimum valid value '
49 '%s.' % (rv, self.
min), param, ctx)
51 self.fail(
'%s is not in the valid range of %s to %s.'
52 % (rv, self.
min, self.
max), param, ctx)
56 return 'IntRange(%r, %r)' % (self.
min, self.
max)
64 return value.split(
',')
69 if value.startswith(
'0x'):
71 elif value.startswith(
'0o'):
73 elif value.startswith(
'0b'):
77 return int(value, base)
88 for item
in value.split(sep):
89 nums = item.split(dash)
97 click.ClickException(
'Invalid interval '+item)
98 numbers.extend(list(range(i,j+1)))
100 click.ClickException(
'Malformed option (comma separated list expected): {}'.format(value))