Command line: pass string arguments

Posted By: AndrewAMD

Command line: pass string arguments - 05/08/20 18:22

jcl,

I'd like the ability to pass up to four strings to zorro from the command line, much like the -i argument.

Example usage, where three strings are supplied:
zorro.exe -s "alpha beta" -s "gamma,delta" -s "epsilon_zeta"

Script side, there would be a global variable CommandStr[0] .. CommandStr[3], which would point to the same strings.
Posted By: AndrewAMD

Re: Command line: pass string arguments - 05/10/20 17:21

Thanks, it looks like this feature was just added!

Here's some sample code showing it at work. Here, I extract the string "alpha beta" from the command line:
Code
// Note: this only works if arbitrary input string is in double quotations.
// example commandline entry:
// zorro -u "alpha beta" -run commandline_string.c
// output: 'alpha beta'

// returns a temporary string with -u output, if it's in double quotations.
string get_u_output(){
	int len = strlen(report(3));
	string str = zalloc(len);
	strcpy(str,report(3));
	char* p = strstr(str,"-u \"");
	if(!p) return NULL;
	if(!strtok(p,"\"")) return NULL;
	return strtok(0,"\"");
}

int main(void){
	printf("\n\'%s\'",get_u_output());
}
Posted By: jcl

Re: Command line: pass string arguments - 05/11/20 08:59

Yes, this was a coincidence, although it is not exactly the same feature that you wanted.
Posted By: AndrewAMD

Re: Command line: pass string arguments - 05/11/20 23:08

It's okay, because either way, I'll need to parse it!
Posted By: AndreasTrader

Re: Command line: pass string arguments - 01/09/22 16:16

Thanks to @AndrewAMD for the code snippet. Very helpful!

In testing it, I noticed that the report(3) function is limited to 256 bytes. To increase the number of variables that can be passed and thus the reliability a 1K limit would be great like it is used for the TEMPstrings. Thanks.
Posted By: AndrewAMD

Re: Command line: pass string arguments - 01/10/22 11:29

You can always use the Windows API directly. Try this:
Code
#include<default.c>
#include<windows.h>

LPSTR GetCommandLineA();
API(GetCommandLineA,Kernel32)

// returns a temporary string with -u output, if it's in double quotations.
string get_u_output(){
	int len = strlen(GetCommandLineA());
	string str = zalloc(len);
	strcpy(str,report(3));
	char* p = strstr(str,"-u \"");
	if(!p) return NULL;
	if(!strtok(p,"\"")) return NULL;
	return strtok(0,"\"");
}

void main(){
	printf("\n\"%s\"",GetCommandLineA());
	printf("\n\"%s\"",get_u_output());
	
	printf("\nDone.");
}
Posted By: jcl

Re: Command line: pass string arguments - 01/10/22 12:55

There is indeed a command line limit, but AFAIK it's 8k, not 256 bytes. It does not matter if you call report(3) or GetCommandLine.
Posted By: AndrewAMD

Re: Command line: pass string arguments - 01/10/22 13:47

If you really did run out of room at the command line (8k?), you can always make use of environment variables via the windows API.
https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentvariablea
© 2024 lite-C Forums