add quoted arguments

This commit is contained in:
Er2 2022-02-12 21:46:36 +03:00
parent ea4b8679c9
commit e8a1270932

View file

@ -35,9 +35,40 @@ local function receiveUpdate(self, update)
if (msg.chat.type == 'group' or msg.chat.type == 'supergroup')
and not to then return end
local args = {}
-- remove needn't text
msg.text = msg.text:sub(#cmd + #(to or '') + 3)
for s in msg.text:gmatch '%S+' do table.insert(args, s) end
-- "quoted" and not quoted args support
-- inside quotes, buffer and final args
local iq, buf, args = false, '', {}
local qt, esc = nil, false -- quote type and escape, addition
-- helper function, add args and clean buffer
local function psh()
if #buf > 0 then table.insert(args, buf) end
buf = ''
qt = nil
end
for i = 1, #msg.text do
local v = msg.text:sub(i, i)
print(v, buf, iq)
-- if space and not inside quotes
if (v == ' ' or v == '\n' or v == '\t')
and not iq then psh()
elseif esc then esc, buf = false, buf.. v
elseif v == '\\' then esc = true
elseif (not qt or qt == v)
and (v == '"' or v == "'") then
psh()
qt, iq = v, not iq -- quote type, inside quote
else buf = buf.. v -- append buffer
end
end
psh() -- if has something
msg.cmd = cmd
msg.args = args