Creating a slash command

Slash commands allow the player to interact with addons by typing commands into the chat window. This tutorial describes the necessary steps to add the ability to handle slash commands to your addon.

Summary: assign a handler function to, and create  , ...,   global constants containing the desired command names, where key is some unique command-specific string.

Background
Slash command handlers are typically invoked by Blizzard's FrameXML code handling the player's chat input; therefore, an addon offering a slash command must register the command by placing certain values in a location where the chat input handling code looks for them.

An addon must create a function that would handle the slash command (possibly parsing the argument string passed to it), and place it into the SlashCmdList table. Additionally, an addon must declare which slash commands it wishes to bind to its handler function.

Strategy

 * 1) Pick a unique identifier for the slash command: e.g. MYFANCYADDON
 * A common convention is ADDONNAME if you only have one slash command, or ADDONNAME_COMMANDNAME if you have multiple (where ADDONNAME is the name of your addon).
 * 1) Decide which slash commands you want to respond to: i.e. /myadd.
 * You can select multiple alternatives for the same command -- allowing shorter or longer command names to be used in case of conflicts.
 * 1) Assign your selected slash commands to SLASH_MYFANCYADDON1, SLASH_MYFANCYADDON2, etc global variables.
 * 2) Write a slash command handler function (taking (msg, editbox) as arguments), and place assign it to SlashCmdList["MYFANCYADDON"].

Here is a simple example that can be placed anywhere in a Lua file: -- 1. Pick HELLOWORLD as the unique identifier. -- 2. Pick /hiw and /hellow as slash commands (/hi and /hello are actual emotes) SLASH_HELLOWORLD1, SLASH_HELLOWORLD2 = '/hiw', '/hellow'; -- 3. function SlashCmdList.HELLOWORLD(msg, editbox) -- 4. print("Hello, World!"); end

Parsing Arguments
If the user types /yourcmd someargs into chat, the function handling /yourcmd is passed "someargs" as the first argument (the second being the edit box widget into which the slash command was typed). Your handler function can then choose to act on that argument string. For simple slash commands, comparisons to pre-determined strings may be sufficient: suppose we wanted to modify the "Hello, World!" example above to display a different string if the command was passed an argument (say /hiw bye):

SLASH_HELLOWORLD1, SLASH_HELLOWORLD2 = '/hiw', '/hellow'; local function handler(msg, editbox) if msg == 'bye' then print('Goodbye, World!'); else print("Hello, World!"); end end SlashCmdList["HELLOWORLD"] = handler; -- Also a valid assignment strategy

More complex slash commands may wish to use Pattern matching to parse the arguments string: local function handler(msg, editbox) local command, rest = msg:match("^(%S*)%s*(.-)$"); -- Any leading non-whitespace is captured into command; -- the rest (minus leading whitespace) is captured into rest. if command == "add" and rest ~= "" then -- Handle adding of the contents of rest... to something. elseif command == "remove" and rest ~= "" then -- Handle removing of the contents of rest... to something. else -- If not handled above, display some sort of help message print("Syntax: /yourcmd (add|remove) someIdentifier"); end end