1 """engine.SCons.Options
3 This file defines the Options class that is used to add user-friendly customizable
4 variables to a scons build.
8 # Copyright (c) 2001, 2002, 2003 Steven Knight
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
18 # The above copyright notice and this permission notice shall be included
19 # in all copies or substantial portions of the Software.
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
22 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
23 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
38 Holds all the options, updates the environment with the variables,
39 and renders the help text.
41 def __init__(self, file=None):
43 file - [optional] the name of the customizable file.
49 def Add(self, key, help="", default=None, validater=None, converter=None):
53 key - the name of the variable
54 help - optional help text for the options
55 default - optional default value
56 validater - optional function that is called to validate the option's value
57 converter - optional function that is called to convert the option's value before
58 putting it in the environment.
67 option.default = default
68 option.validater = validater
69 option.converter = converter
71 self.options.append(option)
73 def Update(self, env, args):
75 Update an environment with the option variables.
77 env - the environment to update.
78 args - the dictionary to get the command line arguments from.
83 # first set the defaults:
84 for option in self.options:
85 if not option.default is None:
86 values[option.key] = option.default
88 # next set the value specified in the options file
89 if self.file and os.path.exists(self.file):
90 execfile(self.file, values)
92 # finally set the values specified on the command line
95 # put the variables in the environment:
96 # (don't copy over variables that are not declared
98 for option in self.options:
100 env[option.key] = values[option.key]
104 # Call the convert functions:
105 for option in self.options:
107 value = env.subst('${%s}'%option.key)
109 env[option.key] = option.converter(value)
110 except ValueError, x:
111 raise SCons.Errors.UserError, 'Error converting option: %s\n%s'%(option.key, x)
114 # Finally validate the values:
115 for option in self.options:
117 option.validater(option.key, env.subst('${%s}'%option.key))
120 def GenerateHelpText(self, env):
122 Generate the help text for the options.
124 env - an environment that is used to get the current values of the options.
129 for option in self.options:
130 help_text = help_text + '\n%s: %s\n default: %s\n'%(option.key, option.help, option.default)
131 if env.has_key(option.key):
132 help_text = help_text + ' actual: %s\n'%env.subst('${%s}'%option.key)
134 help_text = help_text + ' actual: None\n'