View Single Post
05-06-14, 07:03 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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:
  1. SLASH_MYADDON1 = "/myaddon"
  2. SlashCmdList["MYADDON"] = function(msg)
  3.     local command, args = strsplit(" ", strtrim(msg), 2) -- trim leading/trailing spaces before splitting
  4.  
  5.     if command == "show" then
  6.         MyAddonMainFrame:Show()
  7.     elseif command == "hide" then
  8.         MyAddonMainFrame:Hide()
  9.     elseif command == "toggle" then
  10.         MyAddonMainFrame:SetShown(not MyAddonMainFrame:IsShown())
  11.     else
  12.         print("|cffffc000MyAddon:|r Use /myaddon with the following commands:")
  13.         print("   - show - shows the main frame")
  14.         print("   - hide - hides the main frame")
  15.         print("   - toggle - toggles the main frame")
  16.     end
  17. 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.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote