WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Testing slash-command before execution? (https://www.wowinterface.com/forums/showthread.php?t=58847)

Zax 07-18-21 01:41 AM

Testing slash-command before execution?
 
Hello,

I use the following function to execute slash-commands, or commands:

Code:

-- .executeCmd("/sit", true)
-- .executeCmd("print(GetServerTime()), false)

function .executeCmd(cmdStr, isMacroBoolean)
        local cmdStr = cmdStr or ""
        local isMacroBoolean = isMacroBoolean or true
        local tbl = {strsplit("\n", cmdStr)}
        for i, strLine in ipairs(tbl) do
                strLine = strtrim(strLine)
                if (strLine ~= "") then
                        if (isMacroBoolean)) then
                                --------------------------- macro ("/" needed)
                                if (string.sub(strLine, 1, 1) ~= "/") then strLine = "/"..strLine end
                                DEFAULT_CHAT_FRAME.editBox:SetText(strLine)
                                ChatEdit_SendText(DEFAULT_CHAT_FRAME.editBox, 0)
                        else
                                --------------------------- command ("/" not valid)
                                if (string.sub(strLine, 1, 1) == "/") then strLine = string.sub(strLine, 2) end -- delete char 1
                                RunScript(strLine)
                        end
                end
        end
end

And I would like to test the passed command because if the command is not valid, it will display a lua error, and it can be confusing. I tried to play with pcall() without success.

Thank you for your help.

Fizzlemizz 07-18-21 10:15 AM

What function did you add to SlashCmdList to be called by your /xxx command? You could just use that.
Lua Code:
  1. SLASH_MYSLASH1 = "/abc"
  2. SlashCmdList.MYSLASH = function(msg)
  3.     ...
  4. end
  5. -- Testing with
  6. SlashCmdList.MYSLASH("PassedText")

Zax 07-18-21 12:41 PM

Quote:

Originally Posted by Fizzlemizz (Post 339586)
What function did you add to SlashCmdList to be called by your /xxx command?

Well, .executeCmd() function is made for general purpose and could receive different kind of commands or macros, something like:
Lua Code:
  1. .executeCmd("/sit", true) -- built-in macro
  2. .executeCmd("/anAddonSlashCmd arg1", true) -- an addon /cmd
  3. .executeCmd("print(GetServerTime()", false) -- script
So I'm not sure to understand what are you proposing.

Fizzlemizz 07-18-21 01:08 PM

The function for a slash command receives a single string parameter. If you want to interpret that into say commmands and parameters, you need to break down the string that is passed eg:
Lua Code:
  1. SlashCmdList.SLASHFUNC = function(msg)
  2.     local command, p1, p2, p3, p4 = strsplit(" ", strupper(msg))
  3.     if command == "RESET" then
  4.         FCF_ResetChatWindows()
  5.     elseif command == "LEAVE" then
  6.         if p1 == "TRADE" then
  7.             LeaveChannelByName("Trade")
  8.         else
  9.             print(command, p1, "Not Implemented!")
  10.         end
  11.     end
  12. end
  13.  
  14. -- test
  15. SlashCmdList.SLASHFUNC = function("leave trade")

or are you saying you want to test if RunScript(strLine) is going to run without error before running RunScript(strLine)?

Zax 07-19-21 09:09 AM

Quote:

Originally Posted by Fizzlemizz (Post 339589)
or are you saying you want to test if RunScript(strLine) is going to run without error before running RunScript(strLine)?

Yes :)

If I write RunScript("mmmkkhgrttt") I will have a lua error, and I would like to trap this error.
In fact, I'm now able to do this with something like:
Lua Code:
  1. local func, errorMessage = loadstring(strLine)
  2.             if (not func) then
  3.                 isError = true
  4.             else
  5.                 local success, errorMessage = pcall(func)
  6.                 if (not success) then
  7.                     isError = true
  8.                 end
  9.             end

The tricky part is to catch errors when a macro is passed to the function .executeCmd() and processed with:
Lua Code:
  1. if (isMacroBoolean)) then
  2.                 --------------------------- macro ("/" needed)
  3.                 if (string.sub(strLine, 1, 1) ~= "/") then strLine = "/"..strLine end
  4.                 DEFAULT_CHAT_FRAME.editBox:SetText(strLine)
  5.                 ChatEdit_SendText(DEFAULT_CHAT_FRAME.editBox, 0)
  6.             else
  7.                 ...
  8.             end
In case of macro passed as strLine, I don't know how to test/try strLine.

Zax 07-20-21 02:30 AM

OK, I finally solve my problem, with the following code (maybe it could be optimized):

Lua Code:
  1. function .executeCmd(cmdStr, isMacroBoolean, noPrintErrorBoolean) -- return "", or error string
  2.     local cmdStr = cmdStr or ""
  3.     local tbl = {strsplit("\n", cmdStr)}
  4.    
  5.     local function executeString(str) -- return "", or error string
  6.         local str = str or ""
  7.         local func, errorMessage = loadstring(str)
  8.         if (not func) then
  9.             return "slash command LUA error #59000."
  10.         else
  11.             local success, errorMessage = pcall(func)
  12.             if (not success) then
  13.                 return "slash command error #59001."
  14.             else
  15.                 return ""
  16.             end
  17.         end
  18.     end
  19.    
  20.     local resultStr = ""
  21.     for i, strLine in ipairs(tbl) do
  22.         if (resultStr ~= "") then break end
  23.         if (isMacroBoolean) then
  24.             --------------------------- macro ("/" needed)
  25.             if (string.sub(strLine, 1, 1) ~= "/") then strLine = "/"..strLine end
  26.             if (string.sub(strLine, 1, 5) == "/run ") then
  27.                 strLine = string.sub(strLine, 6)
  28.                 resultStr = executeString(strLine)
  29.             elseif (string.sub(strLine, 1, 8) == "/script ") then
  30.                 strLine = string.sub(strLine, 9)
  31.                 resultStr = executeString(strLine)
  32.             else
  33.                 DEFAULT_CHAT_FRAME.editBox:SetText(strLine) -- sent to Chat Box
  34.                 ChatEdit_SendText(DEFAULT_CHAT_FRAME.editBox,0)
  35.             end
  36.         else
  37.             --------------------------- command ("/" not valid)
  38.             if (string.sub(strLine, 1, 1) == "/") then strLine = string.sub(strLine, 2) end -- delete char 1
  39.             resultStr = executeString(strLine)
  40.         end
  41.     end
  42.    
  43.     if ((resultStr ~= "") and (not noPrintErrorBoolean)) then print(resultStr) end
  44.     return resultStr
  45. end


All times are GMT -6. The time now is 05:04 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI