You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.7 KiB

6 years ago
  1. """engine.SCons.Variables.EnumVariable
  2. This file defines the option type for SCons allowing only specified
  3. input-values.
  4. Usage example::
  5. opts = Variables()
  6. opts.Add(EnumVariable('debug', 'debug output and symbols', 'no',
  7. allowed_values=('yes', 'no', 'full'),
  8. map={}, ignorecase=2))
  9. ...
  10. if env['debug'] == 'full':
  11. ...
  12. """
  13. #
  14. # Copyright (c) 2001 - 2017 The SCons Foundation
  15. #
  16. # Permission is hereby granted, free of charge, to any person obtaining
  17. # a copy of this software and associated documentation files (the
  18. # "Software"), to deal in the Software without restriction, including
  19. # without limitation the rights to use, copy, modify, merge, publish,
  20. # distribute, sublicense, and/or sell copies of the Software, and to
  21. # permit persons to whom the Software is furnished to do so, subject to
  22. # the following conditions:
  23. #
  24. # The above copyright notice and this permission notice shall be included
  25. # in all copies or substantial portions of the Software.
  26. #
  27. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  28. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  29. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. #
  35. __revision__ = "src/engine/SCons/Variables/EnumVariable.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  36. __all__ = ['EnumVariable',]
  37. import SCons.Errors
  38. def _validator(key, val, env, vals):
  39. if not val in vals:
  40. raise SCons.Errors.UserError(
  41. 'Invalid value for option %s: %s. Valid values are: %s' % (key, val, vals))
  42. def EnumVariable(key, help, default, allowed_values, map={}, ignorecase=0):
  43. """
  44. The input parameters describe an option with only certain values
  45. allowed. They are returned with an appropriate converter and
  46. validator appended. The result is usable for input to
  47. Variables.Add().
  48. 'key' and 'default' are the values to be passed on to Variables.Add().
  49. 'help' will be appended by the allowed values automatically
  50. 'allowed_values' is a list of strings, which are allowed as values
  51. for this option.
  52. The 'map'-dictionary may be used for converting the input value
  53. into canonical values (e.g. for aliases).
  54. 'ignorecase' defines the behaviour of the validator:
  55. If ignorecase == 0, the validator/converter are case-sensitive.
  56. If ignorecase == 1, the validator/converter are case-insensitive.
  57. If ignorecase == 2, the validator/converter is case-insensitive and the converted value will always be lower-case.
  58. The 'validator' tests whether the value is in the list of allowed values. The 'converter' converts input values
  59. according to the given 'map'-dictionary (unmapped input values are returned unchanged).
  60. """
  61. help = '%s (%s)' % (help, '|'.join(allowed_values))
  62. # define validator
  63. if ignorecase >= 1:
  64. validator = lambda key, val, env: \
  65. _validator(key, val.lower(), env, allowed_values)
  66. else:
  67. validator = lambda key, val, env: \
  68. _validator(key, val, env, allowed_values)
  69. # define converter
  70. if ignorecase == 2:
  71. converter = lambda val: map.get(val.lower(), val).lower()
  72. elif ignorecase == 1:
  73. converter = lambda val: map.get(val.lower(), val)
  74. else:
  75. converter = lambda val: map.get(val, val)
  76. return (key, help, default, validator, converter)
  77. # Local Variables:
  78. # tab-width:4
  79. # indent-tabs-mode:nil
  80. # End:
  81. # vim: set expandtab tabstop=4 shiftwidth=4: