Duugu, what you've written won't work, since (a) there is no global "split" alias for the string.split function, only "strsplit", (b) string.split requires the delimiter as the first argument, not the string to be split, and (c) string.split returns a list, not a table. Even if you enclosed it in curly brackets to create a table, which I suppose was your intention, that's rather inefficient anyway. Also, there's no need to delay creating your slash command; you can just do it in the main chunk, outside of any other function. Here's the usual way to handle commands with arguments that
doesn't create new tables all the time:
Lua Code:
SLASH_MYADDON1 = "/myaddon"
SlashCmdList["MYADDON"] = function(msg)
local command, args = strsplit(" ", strtrim(msg), 2) -- trim leading/trailing spaces before splitting
if command == "show" then
MyAddonMainFrame:Show()
elseif command == "hide" then
MyAddonMainFrame:Hide()
elseif command == "toggle" then
MyAddonMainFrame:SetShown(not MyAddonMainFrame:IsShown())
else
print("|cffffc000MyAddon:|r Use /myaddon with the following commands:")
print(" - show - shows the main frame")
print(" - hide - hides the main frame")
print(" - toggle - toggles the main frame")
end
end
Other benefits of this approach include:
- The user gets the help list no matter what they type, as long as it's not a valid command, instead of limiting them to a few pre-set help commands like "help" and "?". If you limit them, then typing an invalid command (even making a typo, like "hied" instead of "hide") will result in nothing at all happening, which is confusing.
- Each command can handle its arguments however it likes, rather than being forced to handle them one word at a time and use more string functions to join them back up if that's not what it wants.