Bastion aims to serve as a highly performant, simplisitic, and expandable World of Warcraft data visualization framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Bastion/src/Command/Command.lua

61 lines
1.1 KiB

2 years ago
-- Create a wow command handler class
---@class Command
2 years ago
local Command = {}
Command.__index = Command
function Command:__tostring()
return "Command(" .. self.command .. ")"
end
function Command:New(prefix)
local self = setmetatable({}, Command)
self.prefix = prefix
self.commands = {}
_G['SLASH_' .. prefix:upper() .. '1'] = "/" .. prefix
SlashCmdList[prefix:upper()] = function(msg, editbox)
self:OnCommand(msg)
end
return self
end
function Command:Register(command, helpmsg, cb)
self.commands[command] = {
helpmsg = helpmsg,
cb = cb
}
end
function Command:Parse(msg)
local args = {}
for arg in msg:gmatch("%S+") do
table.insert(args, arg)
end
return args
end
function Command:OnCommand(msg)
local args = self:Parse(msg)
if #args == 0 then
self:PrintHelp()
return
end
local runner = self.commands[args[1]]
if runner then
runner.cb(args)
end
end
function Command:PrintHelp()
for k, v in pairs(self.commands) do
print('/' .. self.prefix .. ' ' .. k .. " - " .. v.helpmsg)
end
end
return Command