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:
SLASH_HONORTRACK1, SLASH_HONORTRACK2 = '/honortrack', '/ht';
--Function to handle the context of the slash command arguements
function SlashCmdList.HONORTRACK(msg, editBox)
--Capture the command and then the rest of whatever the user input then do things
local command, rest = msg:match("^(%S*)%s*(.-)$");
--Show the frame command check
if string.lower(command) == 'show' then
--Show it
Addon:Show();
--Set the visible variable to save between sessions
htvisible = true
--Let the user know it worked and how to hide it.
print("Honor Track: Showing tracker. You can hide the tracker with /honortrack hide")
--Hide the frame command check
elseif string.lower(command) == 'hide' then
--Hide it
Addon:Hide();
--set the variable
htvisible = false
--Let the user know it worked and how to show it.
print("Honor Track: Hiding tracker. You can show the tracker again with /honortrack show")
--Set the goal command check. This makes sure the user input only numbers and errors if they didn't
elseif string.lower(command) == 'goal' and string.match(rest, "%d*") ~= nil and string.match(rest, "%a") == nil then
--Grab the goal amount entered
hthonorgoal = string.match(rest, "%d*")
--Send the goal to the update function
UpdateGoal(self)
htgoalset = true
--Let the user know it worked and what we set the goal to.
print("Honor Track: Honor Goal set to " .. string.match(rest, "%d*"))
--Reset the goal
elseif string.lower(command) == 'goal' and string.lower(rest) == "reset" then
--Set the goal to 0
hthonorgoal = 0
--Send that 0 to the frame update function
UpdateGoal(self)
htgoalset = false
--Let the user know we reset the goal
print("Honor Track: Honor Goal reset")
--Reset the honor per hour tracker to zero
elseif string.lower(command) == 'hprreset' then
--set the honor per hour variable to 0
hthonorgained = 0
print("Honor Track: Your honor per hour statistic has been reset.")
--Test slash function
elseif string.lower(command) == 'test' and string.match(rest, "%d*") ~= nil and string.match(rest, "%a") == nil then
hthonorgained = string.match(rest, "%d*")
print("HonorTrack: Added honor to the functions for testing.")
--User entered something that is not a valid command
else
--Let the user know what the proper slash command syntax is
print("Honor Track: Available commands are show, hide, hprreset and goal")
print("Honor Track: To set goal, use /honortrack goal ####")
print("Honor Track: To reset goal, use /honortrack goal reset")
print("Honor Track: To reset your honor per hour statistic, use /honortrack hprreset")
end
end