Package SCons :: Package Variables :: Module BoolVariable'
[hide private]
[frames] | no frames]

Source Code for Module SCons.Variables.BoolVariable'

 1  """engine.SCons.Variables.BoolVariable 
 2   
 3  This file defines the option type for SCons implementing true/false values. 
 4   
 5  Usage example: 
 6   
 7    opts = Variables() 
 8    opts.Add(BoolVariable('embedded', 'build for an embedded system', 0)) 
 9    ... 
10    if env['embedded'] == 1: 
11      ... 
12  """ 
13   
14  # 
15  # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation 
16  # 
17  # Permission is hereby granted, free of charge, to any person obtaining 
18  # a copy of this software and associated documentation files (the 
19  # "Software"), to deal in the Software without restriction, including 
20  # without limitation the rights to use, copy, modify, merge, publish, 
21  # distribute, sublicense, and/or sell copies of the Software, and to 
22  # permit persons to whom the Software is furnished to do so, subject to 
23  # the following conditions: 
24  # 
25  # The above copyright notice and this permission notice shall be included 
26  # in all copies or substantial portions of the Software. 
27  # 
28  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 
29  # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
30  # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
31  # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
32  # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
33  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
34  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
35  # 
36   
37  __revision__ = "src/engine/SCons/Variables/BoolVariable.py 3363 2008/09/06 07:34:10 scons" 
38   
39  __all__ = ['BoolVariable',] 
40   
41  import string 
42   
43  import SCons.compat 
44  import SCons.Errors 
45   
46  __true_strings  = ('y', 'yes', 'true', 't', '1', 'on' , 'all' ) 
47  __false_strings = ('n', 'no', 'false', 'f', '0', 'off', 'none') 
48   
49   
50 -def _text2bool(val):
51 """ 52 Converts strings to True/False depending on the 'truth' expressed by 53 the string. If the string can't be converted, the original value 54 will be returned. 55 56 See '__true_strings' and '__false_strings' for values considered 57 'true' or 'false respectivly. 58 59 This is usable as 'converter' for SCons' Variables. 60 """ 61 lval = string.lower(val) 62 if lval in __true_strings: return True 63 if lval in __false_strings: return False 64 raise ValueError("Invalid value for boolean option: %s" % val)
65 66
67 -def _validator(key, val, env):
68 """ 69 Validates the given value to be either '0' or '1'. 70 71 This is usable as 'validator' for SCons' Variables. 72 """ 73 if not env[key] in (True, False): 74 raise SCons.Errors.UserError( 75 'Invalid value for boolean option %s: %s' % (key, env[key]))
76 77
78 -def BoolVariable(key, help, default):
79 """ 80 The input parameters describe a boolen option, thus they are 81 returned with the correct converter and validator appended. The 82 'help' text will by appended by '(yes|no) to show the valid 83 valued. The result is usable for input to opts.Add(). 84 """ 85 return (key, '%s (yes|no)' % help, default, 86 _validator, _text2bool)
87