Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Argument Parser API
- By RodrickLord
- Code Example:
- Shell: "<program> --config1 --config2 true --config3 diamond"
- args = api.parse(...)
- args = {
- config1 = true,
- config2 = true,
- config3 = "diamond",
- }
- ]]
- l = string.lower
- local function strTable(s)
- local t = {}
- for i in string.gmatch(s, "%s+") do
- table.insert(t, i)
- end
- return t
- end
- function parse(t,start)
- local start = start or "--"
- local r = {}
- if type(t) == "string" then
- t = strTable(t)
- end
- if type(t) == "table" then
- for i = 1,#t do
- if t[i]:sub(1,#start) == start then
- if l(t[i+1]) == "true" or l(t[i+1]) == "t" or l(t[i+1]) == "yes" or l(t[i+1]) == "y" or t[i+1]:sub(1,#start) == start then
- r[t[i]] = true
- elseif l(t[i+1]) == "false" or l(t[i+1]) == "t" or l(t[i+1]) == "no" or l(t[i+1]) == "n" then
- r[t[i]] = false
- else
- r[t[i]] = t[i+1]
- end
- end
- end
- return r
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement