Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sub = string.sub
- local gsub = string.gsub
- local find = string.find
- local tinsert = _G.tinsert or table.insert
- local function getParams(str)
- local len = str:len()
- local params = {}
- local i, j = find(str, "%S+")
- while (i and j) do
- if (find(str, '"', i) == i) then
- _, j = find(str, '"', i+1)
- while (true) do
- if (not j) then
- return params
- end
- if (j+1 > len) then
- break
- end
- if (j <= 1 or find(str, "\\", j-1) ~= j-1) then
- break
- end
- _, j = find(str, '"', j+1)
- end
- local unquot = i+1 >= j+1 and "" or gsub(sub(str, i+1, j-1), '\\"', '"')
- tinsert(params, unquot)
- else
- tinsert(params, sub(str, i, j))
- end
- if (j+1 > len) then
- return params
- end
- i, j = find(str, "%S+", j+1)
- end
- return params
- end
- -- Test code
- local cmd = 'something Rochet "attgasd" 1000 "Because I said \\"so\\""'
- print(cmd) -- User used command
- for k, v in pairs(getParams(cmd)) do
- print(k,v)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement