Home > Uncategorized > javascript code to use WScript parameters

javascript code to use WScript parameters

The following code makes use of the WScript.Arguments.Named and Unnamed properties to retrieve and use the WshNamed and WshUnnamed objects.

You can find something on MSDN about this topic, but this is a useful and till-now-missing example (IMHO).

For example, call it with “cscript /Nologo libparam.js /debug bla=bla key=value /verbose”.

var argsNamed = WScript.Arguments.Named;
var argsUnnamed = WScript.Arguments.Unnamed;

function Parameters (argumentsNamed, argumentsUnnamed, splitCharacter) {
    this.splitChar = splitCharacter;
	this.named = argumentsNamed;
    this.unnamed = argumentsUnnamed;
	this.unnamedOptions = new Array()
	
	this.decomposeUnnamedOptions = function ()
	{
		if (typeof(this.unnamed) == 'undefined')
			return
		//WScript.Echo("this.unnamed.length: " + this.unnamed.length);
		for (var j = 0; j  2)
				continue;
			if(typeof(parts[1]) == 'undefined')
				this.unnamedOptions[parts[0]] = 'undefined';
			this.unnamedOptions[parts[0]] = parts[1];
		}
	}

	this.debug = function () {
		WScript.Echo("Parameters class inited.");
		WScript.Echo("There are " + this.named.length + " named arguments.");
		WScript.Echo("There are " + this.unnamed.length + " unnamed arguments.");
		WScript.Echo("The split character is '" + this.splitChar + "'");
		WScript.Echo("The named args are "+this.named.length+" :");
		for (var j in this.named) {
			WScript.Echo("named(" + j+"):"+this.named.Item(j));
		}
		WScript.Echo("The unnamed args are "+this.unnamed.length+" :");
		for (var j = 0; j < this.unnamed.length; j = j + 1) {
			WScript.Echo("unnamed(" + j+"):"+this.unnamed.Item(j));
		}
	}

}


params = new Parameters(argsNamed, argsUnnamed, "=");

params.decomposeUnnamedOptions();

if (params.named.Exists("debug"))
	params.debug();

if (params.named.Exists("verbose"))
	WScript.Echo("---begin processing unnamed args");

if (!params.named.Exists("noresults")) {
	for (var i in params.unnamedOptions) {
		WScript.Echo('key is: ' + i + ', value is: ' + params.unnamedOptions[i]);
	}
}

if (params.named.Exists("verbose"))
	WScript.Echo("---end processing unnamed args");
  1. Ivo
    27 August 2008 at 15:01

    perdincibaccoliona! wordpress re-formats the preformatted….

  2. psycle
    11 August 2009 at 15:02

    Thank you for the example! Just what I was looking for. I’m a little lost though. There is an error in the for loop of the decomposeUnnamedOptions method. Looks like WordPress ate some characters. Can you please clarify that part?

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: