LoTROInterface

LoTROInterface (https://www.lotrointerface.com/forums/index.php)
-   Lua Programming Help (L) (https://www.lotrointerface.com/forums/forumdisplay.php?f=50)
-   -   Shell cmd w/ multiple args (https://www.lotrointerface.com/forums/showthread.php?t=1368)

MuNkEy 01-24-2011 10:47 PM

Shell cmd w/ multiple args
 
I'm trying to expand on to a simple LUA script I wrote (my first time working with LUA) to allow multiple arguments to be passed to it. The problem I'm running into is it seems that the way I'm doing it gives one argument regardless of what I type (I ran into a similar problem when first writing it where the argument wouldn't be nil when nothing was passed to it).

Here's my script:
Code:

import "Turbine"

AhCmd = Turbine.ShellCommand();

function AhCmd:Execute(cmd, arg1, arg2)
        if ( type(tonumber(arg1)) == "number"  and  arg2 == nil ) then
                AhCmd:AhCalc(arg1);
        elseif ( type(tonumber(arg1)) == "number" and type(tonumber(arg2)) == "number" ) then
                AhCmd:AhCalc(arg1, arg2);
        else
                AhCmd:GetHelp();
        end
end

function AhCmd:AhCalc(buyout, bid)
        if (bid == nil) then
                Turbine.Shell.WriteLine( "<rgb=#00FF00>Set Buyout to</rgb> <rgb=#FFFF00>" .. math.floor( ( buyout/0.95)*(10^2) )/(10^2) .. "s</rgb> <rgb=#00FF00>to get</rgb> <rgb=#80FF00>" .. buyout .. "s</rgb>" );
        else
                Turbine.Shell.WriteLine( "<rgb=#00FF00>Set Starting Bid to</rgb> <rgb=#FFBF00>" .. math.floor( ( bid/0.95)*(10^2) )/(10^2) .. "s</rgb><rgb=#00FF00>, Buyout to</rgb> <rgb=#FFFF00>" .. math.floor( ( buyout/0.95)*(10^2) )/(10^2) .. "s</rgb> <rgb=#00FF00>to get between</rgb> <rgb=#80FF00>" .. bid .. "s</rgb> <rgb=#00FF00>and</rgb> <rgb=#80FF00>" .. buyout .. "s</rgb>" );
        end
end

function AhCmd:GetHelp()
        Turbine.Shell.WriteLine( "Syntax: /ah <price>" );
end

Turbine.Shell.AddCommand( "ah", AhCmd );

The script accurately acknowledges 1 argument, but will just return the help text if 2 are is sent.
I know could just add a 2nd command specifically written to accept 2 args, but I really want just 1 command to work for everything

ptweety 01-25-2011 02:24 AM

Hi MuNkEy,

if I recall correctly, than there is only one argument passed to Execute:

Code:

Execute = function(sender, cmd, args)
I.e. args is one big string holding all the parameters. Therefore you may want to parse args in a loop and find all your parameters.

Regards

MuNkEy 01-26-2011 11:11 PM

Just an update in case it helps anyone in the future...

I solved this by adding the split function
Code:

function split(pString, pPattern)
        local Table = {} -- NOTE: use {n = 0} in Lua-5.0
        local fpat = "(.-)" .. pPattern
        local last_end = 1
        local s, e, cap = pString:find(fpat, 1)
        while s do
                if s ~= 1 or cap ~= "" then
                        table.insert(Table,cap)
                end
                last_end = e+1
                s, e, cap = pString:find(fpat, last_end)
        end
        if last_end <= #pString then
                cap = pString:sub(last_end)
                table.insert(Table, cap)
        end
        return Table
end

once you have that function, you can split the string to an array with
Code:

function Execute(cmd, args)
        args = split(args, " ");
end


Garan 03-18-2011 11:00 AM

you can also easily implement string.split using string.gmatch:
Code:

string.split=function(str,separator)
local tmpArray={};
local tmpElem;
 if string.find("^$()%.[]*+-?",separator,1,true)~=nil then separator="%"..separator end
 for tmpElem in string.gmatch(str,"[^"..separator.."]+") do
  table.insert(tmpArray,tmpElem);
 end
 return tmpArray;
end

then use the same as your original split function:
Code:

args = string.split(args, " ");
This version also allows using control characters, including "%" as separators.

EDIT: After playing with it a bit, I realized that the simple solution I originally posted doesn't account for "empty" values between separator characters and neither the original nor the simple version accounted for empty values at the end of the string. So, a more complete (but slightly uglier) version is:
Code:

string.split=function(str,separator)
 local tmpArray={};
 local tmpElem;
 str=tostring(str);
 if string.find("^$()%.[]*+-?",separator,1,true)~=nil then separator="%"..separator end
 for tmpElem in string.gmatch(str,"[^"..separator.."]*") do
  table.insert(tmpArray,tmpElem);
 end
 local tmpIndex=1;
 while tmpIndex<=#tmpArray do
  if tmpIndex==#tmpArray and tmpArray[tmpIndex]=="" then
  table.remove(tmpArray,tmpIndex);
  else
  if tmpArray[tmpIndex]=="" and tmpArray[tmpIndex+1]~="" then
    table.remove(tmpArray,tmpIndex);
  else
    tmpIndex=tmpIndex+1;
  end
  end
 end
 return tmpArray;
end



All times are GMT -5. The time now is 07:12 AM.

vBulletin® - Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© MMOUI