3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 """Variable type for enumeration Variables.
26 Enumeration variables allow selection of one from a specified set of values.
34 help='debug output and symbols',
36 allowed_values=('yes', 'no', 'full'),
42 if env['debug'] == 'full':
46 from typing
import Tuple
, Callable
50 __all__
= ['EnumVariable',]
53 def _validator(key
, val
, env
, vals
) -> None:
55 raise SCons
.Errors
.UserError(
56 'Invalid value for option %s: %s. Valid values are: %s' % (key
, val
, vals
))
59 def EnumVariable(key
, help, default
, allowed_values
, map={}, ignorecase
: int=0) -> Tuple
[str, str, str, Callable
, Callable
]:
60 """Return a tuple describing an enumaration SCons Variable.
62 The input parameters describe an option with only certain values
63 allowed. Returns A tuple including an appropriate converter and
64 validator. The result is usable as input to :meth:`Add`.
66 *key* and *default* are passed directly on to :meth:`Add`.
68 *help* is the descriptive part of the help text,
69 and will have the allowed values automatically appended.
71 *allowed_values* is a list of strings, which are the allowed values
74 The *map*-dictionary may be used for converting the input value
75 into canonical values (e.g. for aliases).
77 The value of *ignorecase* defines the behaviour of the validator:
79 * 0: the validator/converter are case-sensitive.
80 * 1: the validator/converter are case-insensitive.
81 * 2: the validator/converter is case-insensitive and the
82 converted value will always be lower-case.
84 The *validator* tests whether the value is in the list of allowed values.
85 The *converter* converts input values according to the given
86 *map*-dictionary (unmapped input values are returned unchanged).
89 help = '%s (%s)' % (help, '|'.join(allowed_values
))
92 validator
= lambda key
, val
, env
: \
93 _validator(key
, val
.lower(), env
, allowed_values
)
95 validator
= lambda key
, val
, env
: \
96 _validator(key
, val
, env
, allowed_values
)
99 converter
= lambda val
: map.get(val
.lower(), val
).lower()
100 elif ignorecase
== 1:
101 converter
= lambda val
: map.get(val
.lower(), val
)
103 converter
= lambda val
: map.get(val
, val
)
104 return (key
, help, default
, validator
, converter
)
108 # indent-tabs-mode:nil
110 # vim: set expandtab tabstop=4 shiftwidth=4: