forked from premake/premake-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.lua
More file actions
174 lines (140 loc) · 3.3 KB
/
option.lua
File metadata and controls
174 lines (140 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
--
-- option.lua
-- Work with the list of registered options.
-- Copyright (c) 2002-2014 Jason Perkins and the Premake project
--
local p = premake
p.option = {}
local m = p.option
--
-- 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.
--
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
}
_OPTIONS = {}
setmetatable(_OPTIONS, _OPTIONS_metatable)
--
-- Process the raw command line arguments from _ARGV to populate
-- the _OPTIONS table
--
for i, arg in ipairs(_ARGV) do
local key, value
local i = arg:find("=", 1, true)
if i then
key = arg:sub(1, i - 1)
value = arg:sub(i + 1)
else
key = arg
value = ""
end
if key:startswith("/") then
_OPTIONS[key:sub(2)] = value
elseif key:startswith("--") then
_OPTIONS[key:sub(3)] = value
end
end
--
-- The list of registered options. Calls to newoption() will add
-- new entries here.
--
m.list = {}
--
-- Register a new option.
--
-- @param opt
-- The new option object.
--
function m.add(opt)
-- some sanity checking
local missing
for _, field in ipairs({ "description", "trigger" }) do
if (not opt[field]) then
missing = field
end
end
if (missing) then
error("option needs a " .. missing, 3)
end
-- add it to the master list
p.option.list[opt.trigger:lower()] = opt
-- if it has a default value, set it.
if opt.default and not _OPTIONS[opt.trigger] then
_OPTIONS[opt.trigger] = opt.default
end
end
--
-- Retrieve an option by name.
--
-- @param name
-- The name of the option to retrieve.
-- @returns
-- The requested option, or nil if the option does not exist.
--
function m.get(name)
return p.option.list[name]
end
--
-- Iterator for the list of options.
--
function m.each()
-- sort the list by trigger
local keys = { }
for _, option in pairs(p.option.list) do
table.insert(keys, option.trigger)
end
table.sort(keys)
local i = 0
return function()
i = i + 1
return p.option.list[keys[i]]
end
end
--
-- Validate a list of user supplied key/value pairs against the list of registered options.
--
-- @param values
-- The list of user supplied key/value pairs.
-- @returns
--- True if the list of pairs are valid, false and an error message otherwise.
--
function m.validate(values)
for key, value in pairs(values) do
-- does this option exist
local opt = p.option.get(key)
if (not opt) then
return false, "invalid option '" .. key .. "'"
end
-- does it need a value?
if (opt.value and value == "") then
return false, "no value specified for option '" .. key .. "'"
end
-- is the value allowed?
if opt.allowed then
local found = false
for _, match in ipairs(opt.allowed) do
if match[1] == value then
found = true
break
end
end
if not found then
return false, string.format("invalid value '%s' for option '%s'", value, key)
end
end
end
return true
end