Permalink
Browse files

v0.2.0

  • Loading branch information...
1 parent 2ba460f commit c4d7f77d7d3e567b46fc98552db60ec30f9981bb @shinout committed Feb 24, 2012
Showing with 409 additions and 374 deletions.
  1. +46 −37 ArgParser.js
  2. +0 −73 README.ja.md
  3. +326 −56 README.md
  4. +2 −2 package.json
  5. +0 −65 scripts/README.tpl.md
  6. +0 −31 scripts/en.lang
  7. +0 −31 scripts/ja.lang
  8. +0 −18 scripts/package.tpl.json
  9. +0 −21 scripts/umecob-command.js
  10. +0 −5 scripts/updateinfo.sh
  11. +35 −35 test/test.js
View
@@ -1,9 +1,9 @@
function ArgParser() {
- this.valopts = {s: [], l: []};
- this.opts = {s: [], l: []};
- this.options = {};
- this.args = [];
- this.invalids = [];
+ this._valopts = {s: [], l: []};
+ this._opts = {s: [], l: []};
+ this._options = {};
+ this._args = [];
+ this._invalids = [];
this._defaults = {};
this.emptyValue = false;
@@ -19,56 +19,65 @@ ArgParser.create = function() {
// get options ( getOptions("s") ...
ArgParser.prototype.getOptions = function() {
if (arguments.length == 0) {
- return this.options;
+ return this._options;
}
var ret = null;
const that = this;
Array.prototype.forEach.call(arguments, function(arg) {
- ret = ret || that.options[arg];
+ ret = ret || that._options[arg];
});
return ret;
};
+// syntax sugar
+ArgParser.prototype.opt = ArgParser.prototype.getOptions;
+
// get arguments getArgs(0), getArgs(1) ...
ArgParser.prototype.getArgs = function(n) {
- return (n == null) ? this.args: this.args[n];
+ return (n == null) ? this._args: this._args[n];
};
+// syntax sugar
+ArgParser.prototype.arg = ArgParser.prototype.getArgs;
// stringify current options
ArgParser.prototype.stringifyOptions = function() {
var that = this;
- return ['opts', 'valopts'].map(function(opts) {
+ return ['_opts', '_valopts'].map(function(opts) {
return Object.keys(that[opts]).map(function(sl) {
return that[opts][sl]
.filter(function(k) {
- return (that.options[k] !== false);
+ return (that._options[k] !== false);
})
.map(function(k) {
- return (( (sl == 's') ? '-'+k : '--'+k ) + ( (opts == 'opts') ? '' : (' ' + that.options[k]))).replace(/ +$/, '');
+ return (( (sl == 's') ? '-'+k : '--'+k ) + ( (opts == '_opts') ? '' : (' ' + that._options[k]))).replace(/ +$/, '');
}).join(' ');
}).join(' ').replace(/ +$/, '');
}).join(' ').replace(/ +$/, '');
};
// stringify current options and args
ArgParser.prototype.stringify = function() {
- return this.stringifyOptions() + ' ' + this.args.join(' ');
+ return this.stringifyOptions() + ' ' + this._args.join(' ');
};
// get invalid options given
ArgParser.prototype.getInvalids = function(n) {
- return (n == null) ? this.invalids : this.invalids[n];
+ return (n == null) ? this._invalids : this._invalids[n];
};
+ArgParser.prototype.invalids = function() { return this._invalids };
+
// set options which requires a value
ArgParser.prototype.addValueOptions = function() {
var arr = (Array.isArray(arguments[0])) ? arguments[0] : Array.prototype.slice.call(arguments);
arr.forEach(function(opt) {
- this.valopts[(opt.length == 1) ? 's' : 'l'].push(opt);
+ this._valopts[(opt.length == 1) ? 's' : 'l'].push(opt);
}, this);
return this;
};
+// syntax sugar
+ArgParser.prototype.vals = ArgParser.prototype.addValueOptions;
// set args | options which requires (a file | a directory | a number)
@@ -107,17 +116,20 @@ ArgParser.prototype.defaults = function(obj, noSetNums) {
ArgParser.prototype.addOptions = function() {
var arr = (Array.isArray(arguments[0])) ? arguments[0] : Array.prototype.slice.call(arguments);
arr.forEach(function(opt) {
- this.opts[(opt.length == 1) ? 's' : 'l'].push(opt);
+ this._opts[(opt.length == 1) ? 's' : 'l'].push(opt);
}, this);
return this;
};
+// syntax sugar
+ArgParser.prototype.nonvals = ArgParser.prototype.addOptions;
+
// parse argv
ArgParser.prototype.parse = function(arr) {
/* clear past data */
- this.options = {};
- this.args = [];
- this.invalids = [];
+ this._options = {};
+ this._args = [];
+ this._invalids = [];
/* check arguments */
if (! (arr instanceof Array)) {
@@ -129,10 +141,10 @@ ArgParser.prototype.parse = function(arr) {
/* set default values */
var that = this; // for shortcut access to this
- ['opts', 'valopts'].forEach(function(opts) {
+ ['_opts', '_valopts'].forEach(function(opts) {
['s', 'l'].forEach(function(sl) {
that[opts][sl].forEach(function(opt) {
- that.options[opt] = (that._defaults[opt] != undefined) ? that._defaults[opt] : that.emptyValue;
+ that._options[opt] = (that._defaults[opt] != undefined) ? that._defaults[opt] : that.emptyValue;
});
});
});
@@ -143,49 +155,49 @@ ArgParser.prototype.parse = function(arr) {
arr.forEach(function(val) {
/* if option with value is set */
if (vopt) {
- that.options[vopt] = val;
+ that._options[vopt] = val;
vopt = null;
return;
}
/* short option parsing */
if (val.match(/^-[a-zA-Z0-9_]$/)) {
var optname = val.charAt(1);
- if (that.valopts.s.indexOf(optname) >= 0) {
+ if (that._valopts.s.indexOf(optname) >= 0) {
vopt = optname;
return;
}
- else if (that.opts.s.indexOf(optname) >= 0) {
- that.options[optname] = true;
+ else if (that._opts.s.indexOf(optname) >= 0) {
+ that._options[optname] = true;
return;
}
else { // invalid option
- that.options[optname] = true;
- that.invalids.push(optname);
+ that._options[optname] = true;
+ that._invalids.push(optname);
return;
}
}
/* long option parsing */
if (val.match(/^--[a-zA-Z0-9_-]+$/)) {
var optname = val.slice(2);
- if (that.valopts.l.indexOf(optname) >= 0) {
+ if (that._valopts.l.indexOf(optname) >= 0) {
vopt = optname;
return;
}
- else if (that.opts.l.indexOf(optname) >= 0) {
- that.options[optname] = true;
+ else if (that._opts.l.indexOf(optname) >= 0) {
+ that._options[optname] = true;
return;
}
else {
- that.options[optname] = true;
- that.invalids.push(optname);
+ that._options[optname] = true;
+ that._invalids.push(optname);
return;
}
}
/* arguments */
- that.args.push(val);
+ that._args.push(val);
});
var path = require('path'), fs = require('fs');
@@ -210,8 +222,8 @@ ArgParser.prototype.parse = function(arr) {
if (typeof v == "string" && this.getOptions(v) === this.emptyValue) return;
var num = Number( (typeof v == "number") ? this.getArgs(v) : this.getOptions(v) );
if (isNaN(num)) throw new Error('the arg ' + v +' must be a number.');
- if (typeof v == "number") this.args[v] = num;
- else this.options[v] = num;
+ if (typeof v == "number") this._args[v] = num;
+ else this._options[v] = num;
}, this);
return this;
};
@@ -237,7 +249,4 @@ Object.keys(ArgParser.prototype).forEach(function(methodName) {
};
});
-/* version */
-ArgParser.version = '0.1.3';
-
module.exports = ArgParser;
View
@@ -1,73 +0,0 @@
-argparser v0.1.3
-==================
-[Node.js] コマンドライン引数、オプション解析
-
-変更履歴
-
-----------------
-* [0.0.1]: リリース
-* [0.0.2]: getterメソッドを改良
-* [0.0.3]: メソッドチェーンに対応
-* [0.0.4]: process.argvのreferenceでなくcopyを利用するようにした
-* [0.0.5]: stringify()でコマンドの文字列に戻す作業
-* [0.0.6]: ArgParser.getOptionString(obj) でハッシュからオプション文字列を取得
-* [0.0.7]: ArgParser.getOptionString(obj)でnullとfalseの場合はオプションに含めないようにした
-* [0.0.9]: defaultの値をfalse固定からカスタマイズできるようにした
-* [0.1.0]: 各optionのdefaultの値を設定可能に | ファイルやdirectory, 数値の指定ができるようにした
-* [0.1.1]: default値が数値なら入力値の数値チェックをするようにした
-* [0.1.2]: ArgParser.create() <= new ArgParser()と同義. '-' というファイル名はチェックなしにした(stdin用)
-* [0.1.3]: ArgParser.parse()等を ArgParser.create().parse() と同義にした
-
-概要
-----------------
-### インストール方法 ###
- git clone git://github.com/shinout/argparser.git
-
- または
-
- npm install argparser
-
-### 使い方 ###
- const ArgParser = require('argparser');
-
- /* シンプルな使い方 */
- /* node hoge.js --long-var -s foo bar */
- var parser = new ArgParser().parse();
- parser.getArgs(); // [foo, var]
- parser.getOptions(); // {long-var: true, s: true}
- parser.getOptions('long-var'); // true
-
-
- /* 引数つきオプションを指定 */
- /* node hoge.js piyo foo -h --var-with-val 392 bar */
- var parser = new ArgParser();
- parser.addValueOptions(['var-with-val']);
- parser.parse();
- parser.getArgs(0); // [piyo, foo, var]
- parser.getOptions(); // {h: true, var-with-val: 392}
-
-
- /* ファイルを引数に指定する */
- /* node hoge.js -f opfile file1 file2 */
- parser.files(0, 1, "f");
- parser.parse(); // file,file1,file2のいずれかが存在しなければエラーをthrow
-
-
- var parser = new ArgParser();
- parser.addValueOptions(['encoding', 'm', 'aaa']);
- parser.parse(['-m', 110, '--encoding', 'utf-8', 'index.html']);
- parser.getArgs(); // [index.html]
- parser.getOptions(); // {encoding: utf-8, m: 100, aaa: false}
-
-
- /* 引数なしオプションを指定 */
- parser.addOptions(['-h', '-t']);
- parser.addValueOptions(['encoding', 'e', 'm']);
- parser.parse(['-h', 'hoge', '--encoding', 'utf-8', 'index.html']);
- parser.getArgs(); // [hoge, index.html]
- parser.getOptions(); // {h: true, encoding: utf-8, m: false}
- parser.getOptions('e'); // false
- parser.getOptions('encoding'); // utf-8
- parser.getOptions('encoding', 'e'); // utf-8
- parser.getOptions('e', 'encoding'); // utf-8
-
Oops, something went wrong.

0 comments on commit c4d7f77

Please sign in to comment.