Quantcast Shell cmd w/ multiple args - LoTROInterface
lotrointerface.com
Search Downloads


Go Back   LoTROInterface > LotRO > Developer Discussions > Lua Programming Help (L)

Reply
Thread Tools Display Modes
  #1  
Unread 01-24-2011, 10:47 PM
MuNkEy MuNkEy is offline
The Wary
Interface Author - Click to view interfaces
 
Join Date: Jan 2011
Posts: 2
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

Last edited by MuNkEy : 01-24-2011 at 10:50 PM.
Reply With Quote
  #2  
Unread 01-25-2011, 02:24 AM
ptweety's Avatar
ptweety ptweety is offline
The Wary
Interface Author - Click to view interfaces
 
Join Date: Dec 2010
Posts: 2
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
Reply With Quote
  #3  
Unread 01-26-2011, 11:11 PM
MuNkEy MuNkEy is offline
The Wary
Interface Author - Click to view interfaces
 
Join Date: Jan 2011
Posts: 2
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
Reply With Quote
  #4  
Unread 03-18-2011, 11:00 AM
Garan's Avatar
Garan Garan is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: Oct 2010
Posts: 270
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

Last edited by Garan : 03-23-2011 at 01:00 PM. Reason: original solution was incomplete
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
function Backpack:ItemMoved(sender, args); MrJackdaw Lua Programming Help (L) 0 11-21-2010 02:28 AM
Multiple Skindefinition.XML per UI Deewe LotRO Wish List (L) 2 09-29-2007 03:25 PM
Help with loading multiple UIs Crumby Interface Help (L) 0 07-06-2007 06:45 PM


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


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui