-
Notifications
You must be signed in to change notification settings - Fork 1
/
args.lua
54 lines (47 loc) · 1.11 KB
/
args.lua
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
local args = love.arg.parseGameArguments(arg)
local insert = table.insert
local processed, last, cache = {}, nil, nil
for i=1, #args do
local arg = args[i]
local firstChar, lastChar = arg:sub(1,1), arg:sub(-1)
local isArgument = firstChar == "-" and not cache
if not cache and (firstChar == "\"" or firstChar == "'") then
cache = arg:sub(2)
goto continue
end
if cache then
local chars = #arg
if lastChar == "\"" or lastChar == "'" then
arg = cache.." "..arg:sub(1, chars-1)
cache = nil
else
cache = cache.." "..arg:sub(1, chars)
goto continue
end
end
if not isArgument then
if last then
if type(processed[last]) ~= "table" then
processed[last] = {}
end
insert(processed[last], arg)
else
insert(processed, arg)
end
else
processed[arg] = processed[arg] or true
last = arg
end
::continue::
end
if cache then
if last then
if type(processed[last]) ~= "table" then
processed[last] = {}
end
insert(processed[last], cache)
else
insert(processed, cache)
end
end
return processed