1 """engine.SCons.Variables.EnumVariable
3 This file defines the option type for SCons allowing only specified
9 opts.Add(EnumVariable('debug', 'debug output and symbols', 'no',
10 allowed_values=('yes', 'no', 'full'),
11 map={}, ignorecase=2))
13 if env['debug'] == 'full':
18 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation
20 # Permission is hereby granted, free of charge, to any person obtaining
21 # a copy of this software and associated documentation files (the
22 # "Software"), to deal in the Software without restriction, including
23 # without limitation the rights to use, copy, modify, merge, publish,
24 # distribute, sublicense, and/or sell copies of the Software, and to
25 # permit persons to whom the Software is furnished to do so, subject to
26 # the following conditions:
28 # The above copyright notice and this permission notice shall be included
29 # in all copies or substantial portions of the Software.
31 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
32 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
33 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 __revision__ = "src/engine/SCons/Variables/EnumVariable.py 3842 2008/12/20 22:59:52 scons"
42 __all__ = ['EnumVariable',]
48 def _validator(key, val, env, vals):
50 raise SCons.Errors.UserError(
51 'Invalid value for option %s: %s' % (key, val))
54 def EnumVariable(key, help, default, allowed_values, map={}, ignorecase=0):
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
61 'key' and 'default' are the values to be passed on to Variables.Add().
63 'help' will be appended by the allowed values automatically
65 'allowed_values' is a list of strings, which are allowed as values
68 The 'map'-dictionary may be used for converting the input value
69 into canonical values (eg. for aliases).
71 'ignorecase' defines the behaviour of the validator:
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.
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
83 help = '%s (%s)' % (help, string.join(allowed_values, '|'))
86 validator = lambda key, val, env, vals=allowed_values: \
87 _validator(key, string.lower(val), env, vals)
89 validator = lambda key, val, env, vals=allowed_values: \
90 _validator(key, val, env, vals)
93 converter = lambda val, map=map: \
94 string.lower(map.get(string.lower(val), val))
96 converter = lambda val, map=map: \
97 map.get(string.lower(val), val)
99 converter = lambda val, map=map: \
101 return (key, help, default, validator, converter)