-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSlashCommand.lua
110 lines (101 loc) · 2.65 KB
/
SlashCommand.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
local ADDON_NAME, T = ...
local L = T.L
--- @type SimpleAddonManager
local SAM = T.AddonFrame
local CommandsModule = {}
local Commands = {
open = {
usage = "open",
args = {},
func = function()
SAM:Show()
end,
},
profile = {
usage = 'profile "Name" [Reload option]',
args = {
{ desc = 'Name: Profile name', required = true },
{
desc = [==[Reload option (optional):
- ask: Show confirmation popup (default)
- reload: Loads the profile and reloads the UI
- ignore: Only loads the profile]==],
required = false
}
},
func = function(profile, reloadType)
local db = SAM:GetDb()
if (not db.sets[profile]) then
CommandsModule:Print(L("Profile '${profile}' not found!", { profile = profile }))
else
reloadType = reloadType or "ask"
if (reloadType == "ask") then
SAM:GetModule("Profile"):ShowLoadProfileAndReloadUIDialog(profile)
else
SAM:GetModule("Profile"):LoadAddonsFromProfile(profile)
if (reloadType == "reload") then
ReloadUI()
end
end
end
end,
}
}
LibStub("AceConsole-3.0"):Embed(CommandsModule)
setmetatable(CommandsModule, {
__tostring = function()
return (SAM:GetAddOnMetadata(ADDON_NAME, "Title"))
end
})
CommandsModule:RegisterChatCommand("sam", "HandleCMD")
CommandsModule:RegisterChatCommand("simpleaddonmanager", "HandleCMD")
local function HasMissingParam(list, max, reg)
for i = 1, max do
if (list[i] == nil and reg.args[i].required) then
return true
end
end
end
function CommandsModule:UsageMessage(cmd)
local registry = Commands[cmd]
local args = {}
for _, v in ipairs(registry.args) do
table.insert(args, v.desc .. "\n")
end
return "\nUsage: /sam", registry.usage, "\n", unpack(args)
end
local function JointToString(...)
local text = ""
for _, v in ipairs({ ... }) do
text = text .. " " .. v
end
return text
end
function CommandsModule:PrintUsage(cmd)
if (not cmd) then
local text = "\nCommands:"
for i, registry in pairs(Commands) do
text = text .. "\n" .. "/sam " .. registry.usage
end
self:Print(text)
else
self:Print(self:UsageMessage(cmd))
end
end
function CommandsModule:HandleCMD(msg)
local command, nextPos = self:GetArgs(msg, 1)
local commandRegistry = Commands[command]
if (commandRegistry) then
local numArgs = #commandRegistry.args or 0
local args = { self:GetArgs(msg, numArgs, nextPos) }
if (HasMissingParam(args, numArgs, commandRegistry)) then
self:PrintUsage(command)
else
commandRegistry.func(unpack(args))
end
elseif (command == nil) then
Commands.open.func()
else
self:PrintUsage()
end
end