View Full Version : EZStartInfo
Paragon
12-21-2010, 06:59 PM
See code below
If I understand correctly, if I modify EatDrink.ash to use a startInfo record to get its (long) list of parameters, I can launch the script from the gCLI like this:
eatdrink 15 19 15 !overdrink 1000 12 6 1000 +sim
instead of doing this:
ashq import <eatdrink.ash>; eatdrink(15,19,15,false,1000,12,6,1000,true);
?
Paragon
12-22-2010, 04:01 PM
EatDrink main looks like this now:
void main (int foodMax, int drinkMax, int spleenMax, boolean overdrink, boolean sim)
{
SIM_CONSUME = sim;
eatdrink(foodMax, drinkMax, spleenMax, overdrink);
}
Swapping it with
void main(string cmd)
{
startInfo sI = getStartInfo(cmd,false); //Because EatDrink doesn't use commands
SIM_CONSUME = sI.Flags["sim"];
if (sI.Arguments contains 6) //If all 7 args are provided
{
eatdrink(to_int(sI.Arguments[0]), to_int(sI.Arguments[1]), to_int(sI.Arguments[2]), sI.Flags["overdrink"], to_int(sI.Arguments[3]), to_int(sI.Arguments[4]), to_int(sI.Arguments[5]), to_int(sI.Arguments[6]), sI.Flags["sim"]);
} else {
if (sI.Arguments contains 2) //If only 3 of the args are provided
eatdrink(to_int(sI.Arguments[0]), to_int(sI.Arguments[1]), to_int(sI.Arguments[2]), sI.Flags["overdrink"]);
else //When an incorrect number of args are provided
eatdrink (<remaining fullness>, <remaining drunkeness>, <remaining spleen>, sI.Flags["overdrink"]);
}
}
-----------------------------------------------------------------------------------------------------------------------------
Then in the gCLI:
<one time>
alias eatdrink => eatdrink.ash *
eatdrink :: Calls eatdrink to finish filling up.
eatdrink x y z :: Calls eatdrink to eat x fullness y drunkeness, and z spleen
eatdrink x y z a b c d :: calls long ver. of eatdrink
Although this wasn't really meant to update old scripts, it was meant more as a tool for new scripts that didn't want the restrictions of a specific main method signiture so rewiring old scripts to use it may not be pretty.
Also, I *think*, but forgot to check, that a flag that does not appear in the cmd is defaulted to false. I am not 100% sure about this because I didn't put any code to explicitly handle it although i will if it doesn't work that way currently. I also think that I will add another map KeyValuePairs to automatically associate any token with an = in it as a key value pair with the left hand item being the key and the right hand op as the value such that.
eatdrink food=19 drink=4 spleen=8 !overdrink silent
would create an start Info of
.Command=Default
.Arguments[0] = "silent"
.Flags["overdrink"]=false
.KVP["food"]="19"
.KVP["drink"]="4"
.KVP["spleen"]="8"
You could also set it up so that it does use the Command portion so that
eatdrink NightCap :: Could trigger eatdrink with overdrink as true
eatdrink FoodUp :: Could trigger eatdrink to eat as much food as remaining but ignore drink... etc.
I will also change it so that only the first non Kvp and non flag is used as the command so that a script can still get a command of Default even if flags or kvps are sent.
Although this wasn't really meant to update old scripts, it was meant more as a tool for new scripts that didn't want the restrictions of a specific main method signiture so rewiring old scripts to use it may not be pretty.
I just wanted to clarify what you script does by taking as example a notably argument heavy script =)
Also, I *think*, but forgot to check, that a flag that does not appear in the cmd is defaulted to false. I am not 100% sure about this because I didn't put any code to explicitly handle it although i will if it doesn't work that way currently.
False is the default value, no need to worry about that.
I also think that I will add another map KeyValuePairs to automatically associate any token with an = in it as a key value pair with the left hand item being the key and the right hand op as the value
This would be great.
Thanks for sharing this!
heeheehee
12-22-2010, 06:30 PM
By the way, you can do something like count(arr)>=7 instead of arr contains 6. The former's more useful if you use a map indexed by something other than count.
Haven't actually checked out the script yet, but it looks promising, especially considering slyz's response.
Edit:
To save you some time in the future,
startInfo KeyValuePairs(string s)
{
startInfo this;
string[int] map = split_string(s, " ");
foreach i,v in map
{
matcher m = create_matcher("^(.*?)=(.*?)$", v);
if (m.find())
this.KVP[m.group(1)] = m.group(2);
else
{
m = create_matcher("(+|\\?)(.*)", v);
if (m.find())
this.Flags[m.group(2)] = (m.group(1)=="+");
else
if (i==0)
this.Command = v;
else
this.Arguments[count(this.Arguments)] = v;
}
}
if (this.Command == "") this.Command = "Default";
return this;
}
Wasn't totally sure when you wanted this.Command to be set, so if you want to change it, just change if (i==0) to if (this.Command = "").
It looks like Paragon removed his script, but here a version I had downloaded if anyone is ever interested in using it.
Powered by vBulletin® Version 4.2.0 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.