aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
authorJussi Pakkanen <jpakkane@gmail.com>2017-12-02 19:54:27 +0200
committerGitHub <noreply@github.com>2017-12-02 19:54:27 +0200
commit793fc002fa3c414cee20b3b3a4397eeb3ce3d35e (patch)
tree6d299980e2bdb44823e3dd109037241a5f8498df /mesonbuild
parente1bdc098ca48990322b058e2a2a9fce16c3e7674 (diff)
parentc9351ce30c03d107279090da7825096951a705d3 (diff)
downloadmeson-793fc002fa3c414cee20b3b3a4397eeb3ce3d35e.zip
meson-793fc002fa3c414cee20b3b3a4397eeb3ce3d35e.tar.gz
meson-793fc002fa3c414cee20b3b3a4397eeb3ce3d35e.tar.bz2
Merge pull request #2390 from dcbaker/submit/options-list
Add an array type to user options
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/coredata.py33
-rw-r--r--mesonbuild/optinterpreter.py16
2 files changed, 39 insertions, 10 deletions
diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py
index 68b1abf..7fbf18a 100644
--- a/mesonbuild/coredata.py
+++ b/mesonbuild/coredata.py
@@ -128,24 +128,37 @@ class UserComboOption(UserOption):
class UserStringArrayOption(UserOption):
def __init__(self, name, description, value, **kwargs):
super().__init__(name, description, kwargs.get('choices', []))
- self.set_value(value)
-
- def validate(self, value):
- if isinstance(value, str):
- if not value.startswith('['):
- raise MesonException('Valuestring does not define an array: ' + value)
- newvalue = ast.literal_eval(value)
+ self.set_value(value, user_input=False)
+
+ def validate(self, value, user_input):
+ # User input is for options defined on the command line (via -D
+ # options). Users should put their input in as a comma separated
+ # string, but for defining options in meson_options.txt the format
+ # should match that of a combo
+ if not user_input:
+ if isinstance(value, str):
+ if not value.startswith('['):
+ raise MesonException('Valuestring does not define an array: ' + value)
+ newvalue = ast.literal_eval(value)
+ else:
+ newvalue = value
else:
- newvalue = value
+ assert isinstance(value, str)
+ newvalue = [v.strip() for v in value.split(',')]
if not isinstance(newvalue, list):
raise MesonException('"{0}" should be a string array, but it is not'.format(str(newvalue)))
for i in newvalue:
if not isinstance(i, str):
raise MesonException('String array element "{0}" is not a string.'.format(str(newvalue)))
+ if self.choices:
+ bad = [x for x in newvalue if x not in self.choices]
+ if bad:
+ raise MesonException('Options "{}" are not in allowed choices: "{}"'.format(
+ ', '.join(bad), ', '.join(self.choices)))
return newvalue
- def set_value(self, newvalue):
- self.value = self.validate(newvalue)
+ def set_value(self, newvalue, user_input=True):
+ self.value = self.validate(newvalue, user_input)
def validate_value(self, value):
self.validate(value)
diff --git a/mesonbuild/optinterpreter.py b/mesonbuild/optinterpreter.py
index 01dd036..22a263e 100644
--- a/mesonbuild/optinterpreter.py
+++ b/mesonbuild/optinterpreter.py
@@ -84,9 +84,25 @@ def ComboParser(name, description, kwargs):
raise OptionException('Combo choice elements must be strings.')
return coredata.UserComboOption(name, description, choices, kwargs.get('value', choices[0]))
+@permitted_kwargs({'value', 'choices'})
+def string_array_parser(name, description, kwargs):
+ if 'choices' not in kwargs:
+ raise OptionException('Array option missing "choices" keyword.')
+ choices = kwargs['choices']
+ if not isinstance(choices, list):
+ raise OptionException('Array choices must be an array.')
+ for i in choices:
+ if not isinstance(i, str):
+ raise OptionException('Array choice elements must be strings.')
+ value = kwargs.get('value', choices)
+ if not isinstance(value, list):
+ raise OptionException('Array choices must be passed as an array.')
+ return coredata.UserStringArrayOption(name, description, value, choices=choices)
+
option_types = {'string': StringParser,
'boolean': BooleanParser,
'combo': ComboParser,
+ 'array': string_array_parser,
}
class OptionInterpreter: