Make _OPTIONS table key lookups case insensitive

This commit is contained in:
Jason Perkins 2014-02-25 13:17:04 -05:00
parent 68d517c061
commit 51ab0947e8
4 changed files with 75 additions and 9 deletions

View File

@ -1,17 +1,45 @@
--
-- option.lua
-- Work with the list of registered options.
-- Copyright (c) 2002-2013 Jason Perkins and the Premake project
-- Copyright (c) 2002-2014 Jason Perkins and the Premake project
--
premake.option = {}
local m = premake.option
--
-- The list of registered options.
-- We can't control how people will type in the command line arguments, or how
-- project scripts will define their custom options, so case becomes an issue.
-- To mimimize issues, set up the _OPTIONS table to always use lowercase keys.
--
premake.option.list = {}
local _OPTIONS_metatable = {
__index = function(tbl, key)
if type(key) == "string" then
key = key:lower()
end
return rawget(tbl, key)
end,
__newindex = function(tbl, key, value)
if type(key) == "string" then
key = key:lower()
end
rawset(tbl, key, value)
end
}
setmetatable(_OPTIONS, _OPTIONS_metatable)
--
-- The list of registered options. Calls to newoption() will add
-- new entries here.
--
m.list = {}
--
@ -21,7 +49,7 @@
-- The new option object.
--
function premake.option.add(opt)
function m.add(opt)
-- some sanity checking
local missing
for _, field in ipairs({ "description", "trigger" }) do
@ -39,6 +67,7 @@
end
--
-- Retrieve an option by name.
--
@ -48,16 +77,17 @@
-- The requested option, or nil if the option does not exist.
--
function premake.option.get(name)
function m.get(name)
return premake.option.list[name]
end
--
-- Iterator for the list of options.
--
function premake.option.each()
function m.each()
-- sort the list by trigger
local keys = { }
for _, option in pairs(premake.option.list) do
@ -73,6 +103,7 @@
end
--
-- Validate a list of user supplied key/value pairs against the list of registered options.
--
@ -82,7 +113,7 @@
--- True if the list of pairs are valid, false and an error message otherwise.
--
function premake.option.validate(values)
function m.validate(values)
for key, value in pairs(values) do
-- does this option exist
local opt = premake.option.get(key)

View File

@ -0,0 +1,32 @@
--
-- tests/base/test_option.lua
-- Verify the handling of command line options and the _OPTIONS table.
-- Copyright (c) 2014 Jason Perkins and the Premake project
--
local suite = test.declare("base_option")
--
-- Setup and teardown.
--
function suite.setup()
_LOGGING = true
_OPTIONS["testopt"] = "testopt"
end
function suite.teardown()
_OPTIONS["testopt"] = nil
_LOGGING = false
end
--
-- Because we can't control how the user will type in options on the
-- command line, all key lookups should be case insensitive.
--
function suite.returnsCorrectOption_onMixedCase()
test.isnotnil(_OPTIONS["TestOpt"])
end

View File

@ -53,6 +53,7 @@
dofile("base/test_criteria.lua")
dofile("base/test_detoken.lua")
dofile("base/test_include.lua")
dofile("base/test_option.lua")
dofile("base/test_os.lua")
dofile("base/test_override.lua")
dofile("base/test_path.lua")

View File

@ -248,6 +248,7 @@
-- Test execution function
--
local _OS_host = _OS
local _OPTIONS_host = _OPTIONS
local function error_handler(err)
local msg = err
@ -275,10 +276,11 @@
local function test_setup(suite, fn)
_ACTION = "test"
_ARGS = { }
_OPTIONS = { }
_OS = _OS_host
_OPTIONS = {}
setmetatable(_OPTIONS, getmetatable(_OPTIONS_host))
stderr_capture = nil
premake.solution.list = { }