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 5110 2010/07/25 16:14:38 bdeegan"
41
42 __all__ = ['EnumVariable',]
43
44 import string
45
46 import SCons.Errors
47
49 if not val in vals:
50 raise SCons.Errors.UserError(
51 'Invalid value for option %s: %s' % (key, val))
52
53
54 -def EnumVariable(key, help, default, allowed_values, map={}, ignorecase=0):
55 """
56 The input parameters describe a option with only certain values
57 allowed. They are returned with an appropriate converter and
58 validator appended. The result is usable for input to
59 Variables.Add().
60
61 'key' and 'default' are the values to be passed on to Variables.Add().
62
63 'help' will be appended by the allowed values automatically
64
65 'allowed_values' is a list of strings, which are allowed as values
66 for this option.
67
68 The 'map'-dictionary may be used for converting the input value
69 into canonical values (eg. for aliases).
70
71 'ignorecase' defines the behaviour of the validator:
72
73 If ignorecase == 0, the validator/converter are case-sensitive.
74 If ignorecase == 1, the validator/converter are case-insensitive.
75 If ignorecase == 2, the validator/converter is case-insensitive and
76 the converted value will always be lower-case.
77
78 The 'validator' tests whether the value is in the list of allowed
79 values. The 'converter' converts input values according to the
80 given 'map'-dictionary (unmapped input values are returned
81 unchanged).
82 """
83 help = '%s (%s)' % (help, string.join(allowed_values, '|'))
84
85 if ignorecase >= 1:
86 validator = lambda key, val, env, vals=allowed_values: \
87 _validator(key, string.lower(val), env, vals)
88 else:
89 validator = lambda key, val, env, vals=allowed_values: \
90 _validator(key, val, env, vals)
91
92 if ignorecase == 2:
93 converter = lambda val, map=map: \
94 string.lower(map.get(string.lower(val), val))
95 elif ignorecase == 1:
96 converter = lambda val, map=map: \
97 map.get(string.lower(val), val)
98 else:
99 converter = lambda val, map=map: \
100 map.get(val, val)
101 return (key, help, default, validator, converter)
102
103
104
105
106
107
108