diff options
author | David Walter Seikel | 2014-04-03 09:16:33 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-04-03 09:16:33 +1000 |
commit | 2085a462249a4fd87d737508976b28321aa97a59 (patch) | |
tree | 77dbc9ce57227f1d78d41ffb206c2013ffcb36d7 /ClientHamr/GuiLua | |
parent | Detect and print LuaJIT version. (diff) | |
download | SledjHamr-2085a462249a4fd87d737508976b28321aa97a59.zip SledjHamr-2085a462249a4fd87d737508976b28321aa97a59.tar.gz SledjHamr-2085a462249a4fd87d737508976b28321aa97a59.tar.bz2 SledjHamr-2085a462249a4fd87d737508976b28321aa97a59.tar.xz |
Parse the command line arguments.
Diffstat (limited to '')
-rw-r--r-- | ClientHamr/GuiLua/skang.lua | 126 |
1 files changed, 123 insertions, 3 deletions
diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index 20eaae9..1fbe7cc 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua | |||
@@ -147,7 +147,64 @@ moduleBegin = function (name, author, copyright, version, timestamp, skin, isLua | |||
147 | return _M | 147 | return _M |
148 | end | 148 | end |
149 | 149 | ||
150 | -- Restore the environment. | 150 | |
151 | --[[ Parse command line parameters. | ||
152 | |||
153 | This is done in two parts. Skang will do an initial scan and tokenise, | ||
154 | then each module gets a chance to pull it's own stuff from the result. | ||
155 | |||
156 | Make the command line parameter getting MUCH more intelligent, try to support the common | ||
157 | command line interfaces - | ||
158 | |||
159 | arg value | ||
160 | a value | ||
161 | /arg value | ||
162 | /a value | ||
163 | --arg value | ||
164 | --a value | ||
165 | -a value | ||
166 | -ab ('a' and 'b' are both shortcuts.) | ||
167 | arg=value | ||
168 | a=value | ||
169 | arg1=value1&arg2=value2 | ||
170 | arg1=value1|arg2=value2 | ||
171 | a=value1&a=value2 | ||
172 | +arg/-arg (Can't support this generically.) | ||
173 | |||
174 | Ignore /,-,--,& except as arg introducers. Use = as value introducer. Expect | ||
175 | arg or a. If type is String, expect a value. If type is integer, and next token is | ||
176 | not an integer, increment current value, otherwise expect integer value. If type is | ||
177 | boolean, value beginning with T, t, F, f, etc is true, otherwise value is false, unless | ||
178 | next token starts with an introducer, then value is true. | ||
179 | |||
180 | TODO - Finish supporting all of the above. | ||
181 | ]] | ||
182 | |||
183 | ARGS = {} | ||
184 | lua = '' | ||
185 | command = '' | ||
186 | |||
187 | -- Do an initial scan and tokenise of the command line arguments. | ||
188 | scanArguments = function (args) | ||
189 | if args then | ||
190 | lua = args[-1] | ||
191 | command = args[0] | ||
192 | for i, v in ipairs(args) do | ||
193 | local pre = '' | ||
194 | if '--' == string.sub(v, 1, 2) then pre = '--'; v = string.sub(v, 3, -1) end | ||
195 | if '-' == string.sub(v, 1, 1) then pre = '-'; v = string.sub(v, 2, -1) end | ||
196 | if '+' == string.sub(v, 1, 1) then pre = '+'; v = string.sub(v, 2, -1) end | ||
197 | if '/' == string.sub(v, 1, 1) then pre = '/'; v = string.sub(v, 2, -1) end | ||
198 | if '=' == string.sub(v, 1, 1) then pre = '='; v = string.sub(v, 2, -1) end | ||
199 | if '&' == string.sub(v, 1, 1) then pre = '&'; v = string.sub(v, 2, -1) end | ||
200 | if '|' == string.sub(v, 1, 1) then pre = '|'; v = string.sub(v, 2, -1) end | ||
201 | ARGS[i] = {pre, v} | ||
202 | end | ||
203 | end | ||
204 | end | ||
205 | |||
206 | |||
207 | -- Restore the environment, and grab paramateres from standard places. | ||
151 | moduleEnd = function (module) | 208 | moduleEnd = function (module) |
152 | -- See if there is a properties file, and run it in the modules environment. | 209 | -- See if there is a properties file, and run it in the modules environment. |
153 | local properties = loadfile(module._NAME .. '.properties') | 210 | local properties = loadfile(module._NAME .. '.properties') |
@@ -155,13 +212,74 @@ moduleEnd = function (module) | |||
155 | setfenv(properties, getfenv(2)) | 212 | setfenv(properties, getfenv(2)) |
156 | properties() | 213 | properties() |
157 | end | 214 | end |
158 | -- TODO - Parse command line parameters at some point. | 215 | |
159 | -- http://stackoverflow.com/questions/3745047/help-locate-c-sample-code-to-read-lua-command-line-arguments | 216 | -- Look for our command line arguments. |
217 | local metaMum = getmetatable(module) | ||
218 | if metaMum and metaMum.__self then | ||
219 | for i, v in ipairs(ARGS) do | ||
220 | if v[2] then | ||
221 | local thingy = metaMum.__self.stuff[v[2] ] | ||
222 | -- Did we find one of ours? | ||
223 | if thingy then | ||
224 | local value = ARGS[i + 1] | ||
225 | |||
226 | if 'string' == thingy.types[1] then | ||
227 | if value then | ||
228 | module[v[2] ] = value[2] | ||
229 | value[2] = nil -- Mark it as used. | ||
230 | else | ||
231 | print('ERROR - Expected a string value for ' .. thingy.names[1]) | ||
232 | end | ||
233 | end | ||
234 | |||
235 | if 'number' == thingy.types[1] then | ||
236 | if value then | ||
237 | -- If the introducer is '-', then this should be a negative number. | ||
238 | if '-' == value[1] then value[1] = ''; value[2] = '-' .. value[2] end | ||
239 | -- Only parse the next value as a number if it doesn't have an introducer. | ||
240 | if ('' == value[1]) or ('=' == value[1]) then | ||
241 | value[2] = tonumber(value[2]) | ||
242 | if value[2] then | ||
243 | module[v[2] ] = value[2] | ||
244 | value[2] = nil -- Mark it as used. | ||
245 | else | ||
246 | print('ERROR - Expected a number value for ' .. thingy.names[1]) | ||
247 | end | ||
248 | else | ||
249 | module[v[2] ] = module[v[2] ] + 1 | ||
250 | end | ||
251 | else | ||
252 | print('ERROR - Expected a number value for ' .. thingy.names[1]) | ||
253 | end | ||
254 | end | ||
255 | |||
256 | if 'boolean' == thingy.types[1] then | ||
257 | if value then | ||
258 | -- Only parse the next value as a boolean if it doesn't have an introducer. | ||
259 | if ('' == value[1]) or ('=' == value[1]) then | ||
260 | module[v[2] ] = isBoolean(value[2]) | ||
261 | value[2] = nil -- Mark it as used. | ||
262 | else | ||
263 | module[v[2] ] = true | ||
264 | end | ||
265 | else | ||
266 | print('ERROR - Expected a boolean value for ' .. thingy.names[1]) | ||
267 | end | ||
268 | end | ||
269 | |||
270 | v[2] = nil -- Mark it as used. | ||
271 | end | ||
272 | end | ||
273 | end | ||
274 | end | ||
275 | |||
160 | if module.isLua then setfenv(2, module.savedEnvironment) end | 276 | if module.isLua then setfenv(2, module.savedEnvironment) end |
161 | end | 277 | end |
162 | 278 | ||
279 | |||
163 | -- Call this now so that from now on, this is like any other module. | 280 | -- Call this now so that from now on, this is like any other module. |
164 | local _M = moduleBegin('skang', 'David Seikel', 'Copyright 2014 David Seikel', '0.1', '2014-03-27 02:57:00') | 281 | local _M = moduleBegin('skang', 'David Seikel', 'Copyright 2014 David Seikel', '0.1', '2014-03-27 02:57:00') |
282 | |||
165 | -- This works coz LuaJIT automatically loads the jit module. | 283 | -- This works coz LuaJIT automatically loads the jit module. |
166 | if type(jit) == 'table' then | 284 | if type(jit) == 'table' then |
167 | print('Skang is being run by ' .. jit.version .. ' under ' .. jit.os .. ' on a ' .. jit.arch) | 285 | print('Skang is being run by ' .. jit.version .. ' under ' .. jit.os .. ' on a ' .. jit.arch) |
@@ -169,6 +287,8 @@ else | |||
169 | print('Skang is being run by Lua version ' .. _VERSION) | 287 | print('Skang is being run by Lua version ' .. _VERSION) |
170 | end | 288 | end |
171 | 289 | ||
290 | scanArguments(arg) | ||
291 | |||
172 | 292 | ||
173 | function printTableStart(table, space, name) | 293 | function printTableStart(table, space, name) |
174 | print(space .. name .. ": ") | 294 | print(space .. name .. ": ") |