Archive
Posts Tagged ‘javascript’
javascript code to use WScript parameters
27 August 2008
3 comments
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");
Categories: Uncategorized
arguments, command line, javascript, wscript