Bug 1275419 - Add support for the --option=+a,-b kind of options in python configure. r=chmanchester

This commit is contained in:
Mike Hommey 2016-05-25 15:55:03 +09:00
parent 4466444e54
commit fa78b01aa0
2 changed files with 57 additions and 0 deletions

View File

@ -343,12 +343,31 @@ class Option(object):
))
if len(values) and self.choices:
relative_result = None
for val in values:
if self.nargs in ('+', '*'):
if val.startswith(('+', '-')):
if relative_result is None:
relative_result = list(self.default)
sign = val[0]
val = val[1:]
if sign == '+':
if val not in relative_result:
relative_result.append(val)
else:
try:
relative_result.remove(val)
except ValueError:
pass
if val not in self.choices:
raise InvalidOptionError(
"'%s' is not one of %s"
% (val, ', '.join("'%s'" % c for c in self.choices)))
if relative_result is not None:
values = PositiveOptionValue(relative_result, origin=origin)
return values

View File

@ -238,6 +238,44 @@ class TestOption(unittest.TestCase):
option = Option('--with-option', choices=('a', 'b'))
self.assertEqual(option.nargs, 1)
# Test "relative" values
option = Option('--with-option', nargs='*', default=('b', 'c'),
choices=('a', 'b', 'c', 'd'))
value = option.get_value('--with-option=+d')
self.assertEquals(PositiveOptionValue(('b', 'c', 'd')), value)
value = option.get_value('--with-option=-b')
self.assertEquals(PositiveOptionValue(('c',)), value)
value = option.get_value('--with-option=-b,+d')
self.assertEquals(PositiveOptionValue(('c','d')), value)
# Adding something that is in the default is fine
value = option.get_value('--with-option=+b')
self.assertEquals(PositiveOptionValue(('b', 'c')), value)
# Removing something that is not in the default is fine, as long as it
# is one of the choices
value = option.get_value('--with-option=-a')
self.assertEquals(PositiveOptionValue(('b', 'c')), value)
with self.assertRaises(InvalidOptionError) as e:
option.get_value('--with-option=-e')
self.assertEquals(e.exception.message,
"'e' is not one of 'a', 'b', 'c', 'd'")
# Other "not a choice" errors.
with self.assertRaises(InvalidOptionError) as e:
option.get_value('--with-option=+e')
self.assertEquals(e.exception.message,
"'e' is not one of 'a', 'b', 'c', 'd'")
with self.assertRaises(InvalidOptionError) as e:
option.get_value('--with-option=e')
self.assertEquals(e.exception.message,
"'e' is not one of 'a', 'b', 'c', 'd'")
def test_option_value_format(self):
val = PositiveOptionValue()
self.assertEquals('--with-value', val.format('--with-value'))