Class of CommandLine

前回の続き。
前回はhtmlからvbscriptを分離してjavascriptに書き換えるところまでを行った。
今回はコマンドの実行辺りが煩雑なので、これをクラス化してしまう。

//Class コマンドライン

//constructor
var CCommandLine = function(command){
	//instance variables
	this.Str = command;
	this.Separater = "-";
	
	this.oEXEC = null;
};

//class variables
CCommandLine.oWSS = new ActiveXObject( "WScript.Shell" );

//instance methods
CCommandLine.prototype = {
	setSeparater : function(separater){
		this.Separater = separater;
	},
	addArg : function(argument){
		this.Str = this.Str + " " + argument;
		return this;
	},
	addOpt : function(option){
		this.addArg(this.Separater + option);
		return this;
	},
	execute : function(){
		this.oEXEC = CCommandLine.oWSS.Exec(this.Str);
		return this;
	},
	resultALL : function(){
		return this.oEXEC.StdOut.ReadAll();
	}
};

WScript.Shellオブジェクトは1つあれば十分なのでクラス変数に保持する。
コマンドライン文字列とオプション区切り文字、EXECオブジェクトはインスタンス変数に。
コマンドはオブジェクト生成時に指定して、あとはコマンドラインの生成、実行、出力のメソッドを追加する。

var ping = new CCommandLine("ping");

form1.kekka.value = ping.addArg("127.0.0.1").addOpt("n").addArg("3").execute().resultALL();

コマンドラインの生成と実行で自分自身を返しているので上のようなメソッド・チェーンで記述できる。
今日はここまで。