Passing Arguments from CLI to ASH

Veracity

Developer
Staff member
OK, now you can do:

call xxx.ash( 1, toast, "  this is my string!  ", true )

The CLI's argument parsing is very simple minded:

- Take the text between the first ( and the last ) and split it at commas (so no embedded commas in strings)
- "trim" each string (removes leading and trailing spaces).
- If a string both starts and ends with a ", remove the quotes (so you CAN have leading or trailing spaces in strings)
- pass an array of strings to ASH along with the script filename

ASH looks up the main() function and parses each string in order as the appropriate datatype. If any parameter parse fails, it aborts execution. If the user supplied too many arguments, the script aborts. If the user supplied too few arguments, ASH will prompt for the missing ones.

I also souped up ASH's prompting for main() parameters, in general. If the user hits the Cancel button for any prompt, the script aborts. If the user gives an unparsable value (bad item name, whatever), ASH prints an error and reprompts.
 

Nightmist

Member
Re: ASH language reference

[quote author=Veracity link=topic=355.msg2085#msg2085 date=1156899473]
OK, now you can do:[/quote]
Yay no more setting values and then using get property to somehow get values from CLI to ASH =D.
 

holatuwol

Developer
Re: ASH language reference

[quote author=Veracity link=topic=355.msg2085#msg2085 date=1156899473]
OK, now you can do: call xxx.ash( 1, toast, "  this is my string!  ", true )[/quote]

Thanks to Veracity's work, I've been able to further augment this idea to match how I originally envisioned the ASH.  To implement this, I've added the "using" command to KoLmafia's CLI.  When you use this command, KoLmafia will add the supplied ASH script parameter to an internal list of declared script files which will stick with the program until you either kill the data files or reset the "commandLineNamespace" property.  From then on, you can call ANY function from that script (and any others in the list) via the CLI.

Suppose you've declared a function called clovertest() in an ASH script called library.ash.  After typing "using library" or "using library.ash" during any session, whenever you type "clovertest" into the CLI during that session or any future session, KoLmafia's CLI will ask the ASH to invoke the clovertest() function.

Now suppose you declare a function called eatloop(int i) in an ASH script called routine.ash, then after typing "using routine" or "using routine.ash" during any session, whenever you type in "eatloop(4)" into the CLI during that session or any future session, KoLmafia's CLI will ask the ASH to invoke eatloop and bind the 4 parameter.  And, you can still call the clovertest() that's in the library.ash module even after the second "using" invocation, because it's still maintained in the internal list.

I have also made the parsing a little more ambiguous, so people can opt to either call ASH scripts with Veracity's new method or call ASH functions with my new method in a way that makes it look like it's a built in CLI command (IE: using spaces between parameters, removing the parentheses), which is only recommended for scripts with only a few parameters.  Also, in case it wasn't known before, you CAN call scripts without using "call" and without using the file extensions (".txt" and ".ash"), so invoking Veracity's example was previously possible with:

Code:
xxx( 1, toast, "  this is my string!  ", true )

Limitations:  You cannot call functions that have the same name as an existing CLI command directly; you will need to use the "call" command explicitly.  Also, you cannot call functions that have the same name as an existing script (be it CLI or ASH) ever, as they have higher precedence.  Finally, using "call" with extensionless filenames will prefer .txt files over .ash files.  Still, this functionality, while not super-powerful, allows for you to test things a lot more easily, encourages you to write more atomic functions, and is an all-around fun thing for fast typists.
 

Tirian

Member
Re: ASH language reference

Oh, that's so fabulous. I've always wanted that, but figured that it was too exotic to ask for.

Yay, now I can put on my pajamas from the gCLI!!! :D
 

holatuwol

Developer
Re: ASH language reference

And I'm moving this into a more public area so that when I release v8.8 sometime soon, I won't have to do any extra documentation. "Added new features to the ASH" should suffice given this discussion. ^_~
 

macman104

Member
Wow, old thread. Just got around to adding a namespace or two for myself. My file, Namespaces.ash right now is very simple
Code:
void fam(string famString)
{
	if(famString == "cpm")
	{
		cli_execute("familiar cymbal-playing monkey");
	}
	else
	{
		cli_execute("familiar " + famString);
	}
	
}
However, while I was writing this, I found that if I tried to change the script, save the file, and try again, it uses the old script. I have to close mafia and reopen it for the change to take effect. That seems weird since it appears mafia saves simply the file to use, not the actual functions. Any chance for a fix?
 

holatuwol

Developer
I'm trying to come up with better execution speed for scripts that get repeatedly executed (namespaces, consult scripts, and between battle scripts), with the assumption that these weren't likely to be modified a lot. Turns out I was wrong.

I've put in a partial fix for this (ie: it only refreshes when one of the base scripts is modified, though no refresh yet if it involves something those base scripts import), but I'll be tweaking this when I get more free time to hopefully get a more complete fix.
Update: And this is all fixed and scripts which are called often won't need to constantly be reparsed if they've never been modified (and their imports haven't been modified), so things like consult scripts will feel a little faster, too.
 

macman104

Member
Awesome, yea, it's not really for often changing, but it's nice if I'm debugging or tweaking not to be forced to reload mafia each time. 'Preciate the fix!
 

holatuwol

Developer
Necro!

For convenience, added two new commands to the CLI (to the release of v11.0).  I just forgot to document the commands here, I think.  And in the CLI manual, which I haven't updated in awhile.

The first is a change in functionality to namespace.  Now, if you type namespace filter, it will show you all functions which are available in the existing namespace, excluding built in functions, which match against the given filter.  You can exclude the filter to get a list of all functions in your namespace.

The second is a new command ashref which does something similar.  If you type ashref filter, it will show you all built-in functions which match against the given filter.  You can exclude the filter to get a list of all built-in ASH functions.
 

holatuwol

Developer
I've gone ahead and modified namespace handling. KoLmafia will stop parsing parameters, and since namespacing is SUPPOSED to give you CLI commands, KoLmafia will choke if you do something like add parens and make CLI look like ASH or something.

If you provide no parameters, KoLmafia searches for a function that has no parameters. If you type anything extra, KoLmafia will search for a function which accepts a single parameter and pass the whole string to it. If there's more than one function, it will choose the one which uses a string; if there are none of those, but more than one which accepts a single parameter, it will choke.

If you need more than one parameter, you will have to decide how to split things in the single-parameter version of your function (KoLmafia will assume there's one parameter), because KoLmafia will no longer split it for you.
 
Top