aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ClientHamr/GuiLua/skang.lua
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-04-03 09:16:33 +1000
committerDavid Walter Seikel2014-04-03 09:16:33 +1000
commit2085a462249a4fd87d737508976b28321aa97a59 (patch)
tree77dbc9ce57227f1d78d41ffb206c2013ffcb36d7 /ClientHamr/GuiLua/skang.lua
parentDetect and print LuaJIT version. (diff)
downloadSledjHamr-2085a462249a4fd87d737508976b28321aa97a59.zip
SledjHamr-2085a462249a4fd87d737508976b28321aa97a59.tar.gz
SledjHamr-2085a462249a4fd87d737508976b28321aa97a59.tar.bz2
SledjHamr-2085a462249a4fd87d737508976b28321aa97a59.tar.xz
Parse the command line arguments.
Diffstat (limited to 'ClientHamr/GuiLua/skang.lua')
-rw-r--r--ClientHamr/GuiLua/skang.lua126
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
148end 148end
149 149
150-- Restore the environment. 150
151--[[ Parse command line parameters.
152
153This is done in two parts. Skang will do an initial scan and tokenise,
154then each module gets a chance to pull it's own stuff from the result.
155
156Make the command line parameter getting MUCH more intelligent, try to support the common
157command line interfaces -
158
159arg value
160a value
161/arg value
162/a value
163--arg value
164--a value
165-a value
166-ab ('a' and 'b' are both shortcuts.)
167arg=value
168a=value
169arg1=value1&arg2=value2
170arg1=value1|arg2=value2
171a=value1&a=value2
172+arg/-arg (Can't support this generically.)
173
174Ignore /,-,--,& except as arg introducers. Use = as value introducer. Expect
175arg or a. If type is String, expect a value. If type is integer, and next token is
176not an integer, increment current value, otherwise expect integer value. If type is
177boolean, value beginning with T, t, F, f, etc is true, otherwise value is false, unless
178next token starts with an introducer, then value is true.
179
180TODO - Finish supporting all of the above.
181]]
182
183ARGS = {}
184lua = ''
185command = ''
186
187-- Do an initial scan and tokenise of the command line arguments.
188scanArguments = 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
204end
205
206
207-- Restore the environment, and grab paramateres from standard places.
151moduleEnd = function (module) 208moduleEnd = 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
161end 277end
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.
164local _M = moduleBegin('skang', 'David Seikel', 'Copyright 2014 David Seikel', '0.1', '2014-03-27 02:57:00') 281local _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.
166if type(jit) == 'table' then 284if 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)
170end 288end
171 289
290scanArguments(arg)
291
172 292
173function printTableStart(table, space, name) 293function printTableStart(table, space, name)
174 print(space .. name .. ": ") 294 print(space .. name .. ": ")