1 """engine.SCons.Variables.EnumVariable
2
3 This file defines the option type for SCons allowing only specified
4 input-values.
5
6 Usage example:
7
8 opts = Variables()
9 opts.Add(EnumVariable('debug', 'debug output and symbols', 'no',
10 allowed_values=('yes', 'no', 'full'),
11 map={}, ignorecase=2))
12 ...
13 if env['debug'] == 'full':
14 ...
15 """
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 __revision__ = "src/engine/SCons/Variables/EnumVariable.py rel_2.4.0:3365:9259ea1c13d7 2015/09/21 14:03:43 bdbaddog"
41
42 __all__ = ['EnumVariable',]
43
44
45 import SCons.Errors
46
48 if not val in vals:
49 raise SCons.Errors.UserError(
50 'Invalid value for option %s: %s. Valid values are: %s' % (key, val, vals))
51
52
53 -def EnumVariable(key, help, default, allowed_values, map={}, ignorecase=0):
54 """
55 The input parameters describe a option with only certain values
56 allowed. They are returned with an appropriate converter and
57 validator appended. The result is usable for input to
58 Variables.Add().
59
60 'key' and 'default' are the values to be passed on to Variables.Add().
61
62 'help' will be appended by the allowed values automatically
63
64 'allowed_values' is a list of strings, which are allowed as values
65 for this option.
66
67 The 'map'-dictionary may be used for converting the input value
68 into canonical values (eg. for aliases).
69
70 'ignorecase' defines the behaviour of the validator:
71
72 If ignorecase == 0, the validator/converter are case-sensitive.
73 If ignorecase == 1, the validator/converter are case-insensitive.
74 If ignorecase == 2, the validator/converter is case-insensitive and
75 the converted value will always be lower-case.
76
77 The 'validator' tests whether the value is in the list of allowed
78 values. The 'converter' converts input values according to the
79 given 'map'-dictionary (unmapped input values are returned
80 unchanged).
81 """
82 help = '%s (%s)' % (help, '|'.join(allowed_values))
83
84 if ignorecase >= 1:
85 validator = lambda key, val, env: \
86 _validator(key, val.lower(), env, allowed_values)
87 else:
88 validator = lambda key, val, env: \
89 _validator(key, val, env, allowed_values)
90
91 if ignorecase == 2:
92 converter = lambda val: map.get(val.lower(), val).lower()
93 elif ignorecase == 1:
94 converter = lambda val: map.get(val.lower(), val)
95 else:
96 converter = lambda val: map.get(val, val)
97 return (key, help, default, validator, converter)
98
99
100
101
102
103
104