object. To create an Options object, call the Options() function:
.TP
-.RI Options([ files ])
+.RI Options([ files "], [" args ])
This creates an Options object that will read construction variables from
-the file or list of filenames given in the
+the file or list of filenames specified in
+.IR files .
+If no files are specified,
+or the
.I files
-argument. If no files are given, then no files will be read. Example:
+argument is
+.BR None ,
+then no files will be read.
+The optional argument
+.I args
+is a dictionary of
+values that will override anything read from the specified files;
+it is primarily intended to be passed the
+.B ARGUMENTS
+dictionary that holds variables
+specified on the command line.
+Example:
.ES
opts = Options('custom.py')
+opts = Options('overrides.py', ARGUMENTS)
+opts = Options(None, {FOO:'expansion', BAR:7})
.EE
Options objects have the following methods:
.EE
.TP
-.RI Update( env )
+.RI Update( env ", [" args ])
This updates a construction environment
.I env
with the customized construction variables. Normally this method is not
Holds all the options, updates the environment with the variables,
and renders the help text.
"""
- def __init__(self, files=None):
+ def __init__(self, files=None, args={}):
"""
files - [optional] List of option configuration files to load
(backward compatibility) If a single string is passed it is
"""
self.options = []
+ self.args = args
self.files = None
if SCons.Util.is_String(files):
self.files = [ files ]
self.options.append(option)
- def Update(self, env, args):
+ def Update(self, env, args=None):
"""
Update an environment with the option variables.
env - the environment to update.
- args - the dictionary to get the command line arguments from.
"""
values = {}
execfile(filename, values)
# finally set the values specified on the command line
+ if args is None:
+ args = self.args
values.update(args)
- # Update the should save state
- # This will mark options that have either been set on command line
- # or in a loaded option file
- # KeyError occurs when an option has default of None and has not been set
+ # Update should save state.
+ # This will mark options that have either been set on
+ # the command line or in a loaded option file.
+ # KeyError occurs when an option has default of None
+ # and has not been set.
for option in self.options:
try:
if values[option.key] != option.default:
"""
Generate the help text for the options.
- env - an environment that is used to get the current values of the options.
+ env - an environment that is used to get the current values
+ of the options.
"""
help_text = ""
help_text = help_text + ' actual: None\n'
return help_text
-
return self.dict.has_key(key)
-def check(key,value, env):
- assert value == 6 * 9,key
+def check(key, value, env):
+ assert value == 6 * 9, "key %s = %s" % (key, value)
# Check saved option file by executing and comparing against
# the expected dictionary
class OptionsTestCase(unittest.TestCase):
def test_Add(self):
+ """Test adding to an Options object"""
opts = SCons.Options.Options()
opts.Add('VAR')
test_it('foo.bar')
def test_Update(self):
+ """Test updating an Environment"""
+ # Test that a default value is validated correctly.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
opts = SCons.Options.Options(file)
check,
lambda x: int(x) + 12)
+ env = Environment()
+ opts.Update(env)
+ assert env['ANSWER'] == 54
+
env = Environment()
opts.Update(env, {})
assert env['ANSWER'] == 54
+ # Test that a bad value from the file is used and
+ # validation fails correctly.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
test.write('custom.py', 'ANSWER=54')
lambda x: int(x) + 12)
env = Environment()
+ exc_caught = None
+ try:
+ opts.Update(env)
+ except AssertionError:
+ exc_caught = 1
+ assert exc_caught, "did not catch expected assertion"
+
+ env = Environment()
+ exc_caught = None
try:
opts.Update(env, {})
except AssertionError:
- pass
+ exc_caught = 1
+ assert exc_caught, "did not catch expected assertion"
+ # Test that a good value from the file is used and validated.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
test.write('custom.py', 'ANSWER=42')
opts.Add('ANSWER',
'THE answer to THE question',
- "54",
+ "10",
check,
lambda x: int(x) + 12)
+ env = Environment()
+ opts.Update(env)
+ assert env['ANSWER'] == 54
+
env = Environment()
opts.Update(env, {})
assert env['ANSWER'] == 54
+ # Test that a bad value from an args dictionary passed to
+ # Update() is used and validation fails correctly.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
- test.write('custom.py', 'ANSWER=54')
+ test.write('custom.py', 'ANSWER=10')
opts = SCons.Options.Options(file)
opts.Add('ANSWER',
'THE answer to THE question',
- "54",
+ "12",
+ check,
+ lambda x: int(x) + 12)
+
+ env = Environment()
+ exc_caught = None
+ try:
+ opts.Update(env, {'ANSWER':'54'})
+ except AssertionError:
+ exc_caught = 1
+ assert exc_caught, "did not catch expected assertion"
+
+ # Test that a good value from an args dictionary
+ # passed to Update() is used and validated.
+ test = TestSCons.TestSCons()
+ file = test.workpath('custom.py')
+ test.write('custom.py', 'ANSWER=10')
+ opts = SCons.Options.Options(file)
+
+ opts.Add('ANSWER',
+ 'THE answer to THE question',
+ "12",
check,
lambda x: int(x) + 12)
opts.Update(env, {'ANSWER':'42'})
assert env['ANSWER'] == 54
+ def test_args(self):
+ """Test updating an Environment with arguments overridden"""
+
+ # Test that a bad (command-line) argument is used
+ # and the validation fails correctly.
+ test = TestSCons.TestSCons()
+ file = test.workpath('custom.py')
+ test.write('custom.py', 'ANSWER=42')
+ opts = SCons.Options.Options(file, {'ANSWER':54})
+
+ opts.Add('ANSWER',
+ 'THE answer to THE question',
+ "42",
+ check,
+ lambda x: int(x) + 12)
+
+ env = Environment()
+ exc_caught = None
+ try:
+ opts.Update(env)
+ except AssertionError:
+ exc_caught = 1
+ assert exc_caught, "did not catch expected assertion"
+
+ # Test that a good (command-line) argument is used and validated.
+ test = TestSCons.TestSCons()
+ file = test.workpath('custom.py')
+ test.write('custom.py', 'ANSWER=54')
+ opts = SCons.Options.Options(file, {'ANSWER':42})
+
+ opts.Add('ANSWER',
+ 'THE answer to THE question',
+ "54",
+ check,
+ lambda x: int(x) + 12)
+
+ env = Environment()
+ opts.Update(env)
+ assert env['ANSWER'] == 54
+
+ # Test that a (command-line) argument is overridden by a dictionary
+ # supplied to Update() and the dictionary value is validated correctly.
+ test = TestSCons.TestSCons()
+ file = test.workpath('custom.py')
+ test.write('custom.py', 'ANSWER=54')
+ opts = SCons.Options.Options(file, {'ANSWER':54})
+
+ opts.Add('ANSWER',
+ 'THE answer to THE question',
+ "54",
+ check,
+ lambda x: int(x) + 12)
+
+ env = Environment()
+ opts.Update(env, {'ANSWER':42})
+ assert env['ANSWER'] == 54
def test_Save(self):
+ """Testing saving Options"""
test = TestSCons.TestSCons()
cache_file = test.workpath('cached.options')