WoWInterface - View Single Post - Slash command functions help
View Single Post
06-18-21, 07:07 PM   #4
A Warpwood Thunder Caller
briskman3000's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 98
If you want to be able to use a sub-command or a command with an argument, you can use a string grab to separate the stuff.

Here is an example of how I do it, without using any libraries.

This is a straight snippet from one of my working addons, so some of the variables wont make any sense in just this snippet, but you should be able to get an idea of how it works.

Lua Code:
  1. SLASH_HONORTRACK1, SLASH_HONORTRACK2 = '/honortrack', '/ht';
  2. --Function to handle the context of the slash command arguements
  3. function SlashCmdList.HONORTRACK(msg, editBox)
  4.     --Capture the command and then the rest of whatever the user input then do things
  5.     local command, rest = msg:match("^(%S*)%s*(.-)$");
  6.     --Show the frame command check
  7.     if string.lower(command) == 'show' then
  8.         --Show it
  9.         Addon:Show();
  10.         --Set the visible variable to save between sessions
  11.         htvisible = true
  12.         --Let the user know it worked and how to hide it.
  13.         print("Honor Track: Showing tracker. You can hide the tracker with /honortrack hide")
  14.     --Hide the frame command check
  15.     elseif string.lower(command) == 'hide' then
  16.         --Hide it
  17.         Addon:Hide();
  18.         --set the variable
  19.         htvisible = false
  20.         --Let the user know it worked and how to show it.
  21.         print("Honor Track: Hiding tracker. You can show the tracker again with /honortrack show")
  22.     --Set the goal command check. This makes sure the user input only numbers and errors if they didn't
  23.     elseif string.lower(command) == 'goal' and string.match(rest, "%d*") ~= nil and string.match(rest, "%a") == nil then
  24.         --Grab the goal amount entered
  25.         hthonorgoal = string.match(rest, "%d*")
  26.         --Send the goal to the update function
  27.         UpdateGoal(self)
  28.         htgoalset = true
  29.         --Let the user know it worked and what we set the goal to.
  30.         print("Honor Track: Honor Goal set to " .. string.match(rest, "%d*"))
  31.     --Reset the goal
  32.     elseif string.lower(command) == 'goal' and string.lower(rest) == "reset" then
  33.         --Set the goal to 0
  34.         hthonorgoal = 0
  35.         --Send that 0 to the frame update function
  36.         UpdateGoal(self)
  37.         htgoalset = false
  38.         --Let the user know we reset the goal
  39.         print("Honor Track: Honor Goal reset")
  40.     --Reset the honor per hour tracker to zero
  41.     elseif string.lower(command) == 'hprreset' then
  42.         --set the honor per hour variable to 0
  43.         hthonorgained = 0
  44.         print("Honor Track: Your honor per hour statistic has been reset.")
  45.     --Test slash function
  46.     elseif string.lower(command) == 'test' and string.match(rest, "%d*") ~= nil and string.match(rest, "%a") == nil then
  47.         hthonorgained = string.match(rest, "%d*")
  48.         print("HonorTrack: Added honor to the functions for testing.")
  49.     --User entered something that is not a valid command
  50.     else
  51.         --Let the user know what the proper slash command syntax is
  52.         print("Honor Track: Available commands are show, hide, hprreset and goal")
  53.         print("Honor Track: To set goal, use /honortrack goal ####")
  54.         print("Honor Track: To reset goal, use /honortrack goal reset")
  55.         print("Honor Track: To reset your honor per hour statistic, use /honortrack hprreset")
  56.     end
  57. end
My Addons: Convert Ratings Honor Track

Last edited by briskman3000 : 06-18-21 at 07:09 PM.
  Reply With Quote