Expose min and max to IntOpt
The IntOpt class utilizes the types.Integer class which has parameters for setting the minimum and maximum value. These min and max values are not exposed through IntOpt and should be ideally. In the docstring help for cfg.py, it gives an example of creating a PortType instance of types.Integer to create a new type with a range. Rather than projects having to define this new instance, seems better to just expose the min and max to IntOpt. Another advantage of adding min and max to IntOpt is the ability to generate a useful sample config file that displays the range of valid integer values. Change-Id: Icce7b6799061711ea512d60facc57bf7d6f6c9cc
This commit is contained in:
parent
ac7c55dee3
commit
366ca45ef2
@ -1029,8 +1029,9 @@ class IntOpt(Opt):
|
||||
`Kept for backward-compatibility with options not using Opt directly`.
|
||||
"""
|
||||
|
||||
def __init__(self, name, **kwargs):
|
||||
super(IntOpt, self).__init__(name, type=types.Integer(), **kwargs)
|
||||
def __init__(self, name, min=None, max=None, **kwargs):
|
||||
super(IntOpt, self).__init__(name, type=types.Integer(min, max),
|
||||
**kwargs)
|
||||
|
||||
|
||||
class FloatOpt(Opt):
|
||||
|
@ -120,6 +120,12 @@ class _OptFormatter(object):
|
||||
help_text = u'(%s)' % opt_type
|
||||
lines = self._format_help(help_text)
|
||||
|
||||
if getattr(opt.type, 'min', None):
|
||||
lines.append('# Minimum value: %d\n' % opt.type.min)
|
||||
|
||||
if getattr(opt.type, 'max', None):
|
||||
lines.append('# Maximum value: %d\n' % opt.type.max)
|
||||
|
||||
if getattr(opt.type, 'choices', None):
|
||||
choices_text = ', '.join([self._get_choice_text(choice)
|
||||
for choice in opt.type.choices])
|
||||
|
@ -977,6 +977,19 @@ class ConfigFileOptsTestCase(BaseTestCase):
|
||||
self.assertTrue(hasattr(self.conf, 'foo'))
|
||||
self.assertEqual(self.conf.foo, 666)
|
||||
|
||||
def test_conf_file_int_min_max(self):
|
||||
self.conf.register_opt(cfg.IntOpt('foo', min=1, max=5))
|
||||
|
||||
paths = self.create_tempfiles([('test',
|
||||
'[DEFAULT]\n'
|
||||
'foo = 10\n')])
|
||||
|
||||
self.conf(['--config-file', paths[0]])
|
||||
self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo')
|
||||
|
||||
def test_conf_file_int_min_greater_max(self):
|
||||
self.assertRaises(ValueError, cfg.IntOpt, 'foo', min=5, max=1)
|
||||
|
||||
def test_conf_file_int_use_dname(self):
|
||||
self._do_dname_test_use(cfg.IntOpt, '66', 66)
|
||||
|
||||
|
@ -79,6 +79,8 @@ class GeneratorTestCase(base.BaseTestCase):
|
||||
help='a boolean'),
|
||||
'int_opt': cfg.IntOpt('int_opt',
|
||||
default=10,
|
||||
min=1,
|
||||
max=20,
|
||||
help='an integer'),
|
||||
'float_opt': cfg.FloatOpt('float_opt',
|
||||
default=0.1,
|
||||
@ -400,6 +402,8 @@ class GeneratorTestCase(base.BaseTestCase):
|
||||
#
|
||||
|
||||
# an integer (integer value)
|
||||
# Minimum value: 1
|
||||
# Maximum value: 20
|
||||
#int_opt = 10
|
||||
''')),
|
||||
('float_opt',
|
||||
|
@ -982,6 +982,19 @@ class ConfigFileOptsTestCase(BaseTestCase):
|
||||
self.assertTrue(hasattr(self.conf, 'foo'))
|
||||
self.assertEqual(self.conf.foo, 666)
|
||||
|
||||
def test_conf_file_int_min_max(self):
|
||||
self.conf.register_opt(cfg.IntOpt('foo', min=1, max=5))
|
||||
|
||||
paths = self.create_tempfiles([('test',
|
||||
'[DEFAULT]\n'
|
||||
'foo = 10\n')])
|
||||
|
||||
self.conf(['--config-file', paths[0]])
|
||||
self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo')
|
||||
|
||||
def test_conf_file_int_min_greater_max(self):
|
||||
self.assertRaises(ValueError, cfg.IntOpt, 'foo', min=5, max=1)
|
||||
|
||||
def test_conf_file_int_use_dname(self):
|
||||
self._do_dname_test_use(cfg.IntOpt, '66', 66)
|
||||
|
||||
|
@ -81,6 +81,8 @@ class GeneratorTestCase(base.BaseTestCase):
|
||||
help='a boolean'),
|
||||
'int_opt': cfg.IntOpt('int_opt',
|
||||
default=10,
|
||||
min=1,
|
||||
max=20,
|
||||
help='an integer'),
|
||||
'float_opt': cfg.FloatOpt('float_opt',
|
||||
default=0.1,
|
||||
@ -413,6 +415,8 @@ class GeneratorTestCase(base.BaseTestCase):
|
||||
#
|
||||
|
||||
# an integer (integer value)
|
||||
# Minimum value: 1
|
||||
# Maximum value: 20
|
||||
#int_opt = 10
|
||||
''')),
|
||||
('float_opt',
|
||||
|
Loading…
x
Reference in New Issue
Block a user