node-getopt
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -8,3 +8,3 @@ // examples/help.js | ||
['' , 'long' , 'long option.'], | ||
['S' , 'short-with-arg=ARG' , 'option with argument'], | ||
['S' , 'short-with-arg=ARG' , 'option with argument', 'S'], | ||
['L' , 'long-with-arg=ARG' , 'long option with argument'], | ||
@@ -11,0 +11,0 @@ ['' , 'color[=COLOR]' , 'COLOR is optional'], |
@@ -5,3 +5,3 @@ // node-getopt oneline example. | ||
['' , 'long' , 'long option.'], | ||
['S' , 'short-with-arg=ARG' , 'option with argument'], | ||
['S' , 'short-with-arg=ARG' , 'option with argument', 'S'], | ||
['L' , 'long-with-arg=ARG' , 'long option with argument'], | ||
@@ -8,0 +8,0 @@ ['' , 'color[=COLOR]' , 'COLOR is optional'], |
// Generated by ToffeeScript 1.6.3-4 | ||
(function() { | ||
var Getopt, ParsedOption, __matches, | ||
__hasProp = {}.hasOwnProperty; | ||
var Getopt, __matches; | ||
ParsedOption = (function() { | ||
function ParsedOption(argv, options) { | ||
this.argv = argv; | ||
this.options = options; | ||
} | ||
ParsedOption.prototype.empty = function() { | ||
var k, v, _ref; | ||
if (this.argv.length) { | ||
return false; | ||
} | ||
_ref = this.options; | ||
for (k in _ref) { | ||
if (!__hasProp.call(_ref, k)) continue; | ||
v = _ref[k]; | ||
return false; | ||
} | ||
return true; | ||
}; | ||
return ParsedOption; | ||
})(); | ||
Getopt = (function() { | ||
@@ -39,6 +14,6 @@ Getopt.HAS_ARGUMENT = true; | ||
Getopt.VERSION = '0.2.3'; | ||
Getopt.VERSION = '0.3.1'; | ||
function Getopt(optionsPattern) { | ||
var comment, definition, has_argument, long_name, multi_supported, name, option, optional, short_name, _i, _len, _ref; | ||
var comment, def, definition, fixed_long_name, has_argument, long_name, multi_supported, name, names, option, optional, short_name, _i, _len, _ref; | ||
this.optionsPattern = optionsPattern; | ||
@@ -49,3 +24,2 @@ this.short_options = {}; | ||
this.events = {}; | ||
this.event_names = []; | ||
this.argv = []; | ||
@@ -62,6 +36,7 @@ this.options = {}; | ||
} | ||
names = {}; | ||
_ref = this.optionsPattern; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
option = _ref[_i]; | ||
short_name = option[0], definition = option[1], comment = option[2]; | ||
short_name = option[0], definition = option[1], comment = option[2], def = option[3]; | ||
if (comment == null) { | ||
@@ -86,5 +61,4 @@ comment = ''; | ||
} | ||
if (!long_name) { | ||
long_name = short_name; | ||
} | ||
long_name || (long_name = short_name); | ||
fixed_long_name = 'opt_' + long_name; | ||
name = long_name; | ||
@@ -94,3 +68,3 @@ if (long_name === '') { | ||
} | ||
if (this.long_options[long_name] == null) { | ||
if (names[fixed_long_name] == null) { | ||
this.long_names.push(long_name); | ||
@@ -105,6 +79,8 @@ this.long_options[long_name] = { | ||
optional: optional, | ||
definition: definition | ||
definition: definition, | ||
def: def | ||
}; | ||
names[fixed_long_name] = true; | ||
} else { | ||
throw new Error("option " + long_name + " redefined."); | ||
throw new Error("option " + long_name + " is redefined."); | ||
} | ||
@@ -121,31 +97,38 @@ if (short_name !== '') { | ||
Getopt.prototype.on = function(name, cb) { | ||
this.events[name] = cb; | ||
this.event_names.push(name); | ||
return this; | ||
Getopt.prototype.getOptionByName = function(name) { | ||
var _ref; | ||
return (_ref = this.long_options[name]) != null ? _ref : this.short_options[name]; | ||
}; | ||
Getopt.prototype.emit = function(name, cb) { | ||
if (this.events[name]) { | ||
return this.events[name].call(this, this.parsedOption.argv, this.parsedOption.options); | ||
} else { | ||
throw new Error("Getopt trigger '" + name + "' is not found"); | ||
} | ||
Getopt.prototype.getOptionName = function(name) { | ||
var _ref; | ||
return (_ref = this.getOptionByName(name)) != null ? _ref.name : void 0; | ||
}; | ||
Getopt.prototype.trigger_events_ = function() { | ||
var name, options, _i, _len, _ref; | ||
options = this.parsedOption.options; | ||
_ref = this.event_names; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
name = _ref[_i]; | ||
if (options[name] != null) { | ||
this.emit(name); | ||
Getopt.prototype.on = function(name, cb) { | ||
var iname; | ||
if (name) { | ||
iname = this.getOptionName(name); | ||
if (!iname) { | ||
throw new Error("unknown option " + name); | ||
} | ||
} else { | ||
iname = name; | ||
} | ||
this.events[iname] = cb; | ||
return this; | ||
}; | ||
Getopt.prototype.emit = function(name, value) { | ||
var event; | ||
event = this.events[this.getOptionName(name)]; | ||
if (event) { | ||
return event.call(this, value); | ||
} else { | ||
throw new Error("Getopt event on '" + name + "' is not found"); | ||
} | ||
}; | ||
Getopt.prototype.save_option_ = function(options, option, argv) { | ||
var name, names, value, _i, _len; | ||
var name, value, _ref; | ||
if (option.has_argument) { | ||
@@ -159,14 +142,14 @@ if (argv.length === 0) { | ||
} | ||
names = [option.name]; | ||
for (_i = 0, _len = names.length; _i < _len; _i++) { | ||
name = names[_i]; | ||
if (option.multi_supported) { | ||
if (options[name] == null) { | ||
options[name] = []; | ||
} | ||
options[name].push(value); | ||
} else { | ||
options[name] = value; | ||
name = option.name; | ||
if (option.multi_supported) { | ||
if (options[name] == null) { | ||
options[name] = []; | ||
} | ||
options[name].push(value); | ||
} else { | ||
options[name] = value; | ||
} | ||
if ((_ref = this.events[name]) != null) { | ||
_ref.call(this, value); | ||
} | ||
return this; | ||
@@ -176,3 +159,3 @@ }; | ||
Getopt.prototype.parse = function(argv) { | ||
var arg, e, i, long_name, option, rt_argv, rt_options, short_name, short_names, value, _i, _len; | ||
var arg, e, i, long_name, name, option, rt_argv, rt_options, short_name, short_names, sname, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _ref3; | ||
try { | ||
@@ -182,6 +165,14 @@ argv = argv.slice(0); | ||
rt_argv = []; | ||
_ref = this.long_names; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
long_name = _ref[_i]; | ||
option = this.long_options[long_name]; | ||
if ((option.def != null) || (rt_options[option.long_name] != null)) { | ||
rt_options[option.long_name] = option.def; | ||
} | ||
} | ||
while ((arg = argv.shift()) != null) { | ||
if (__matches = arg.match(/^-(\w[\w\-]*)/)) { | ||
short_names = __matches[1]; | ||
for (i = _i = 0, _len = short_names.length; _i < _len; i = ++_i) { | ||
for (i = _j = 0, _len1 = short_names.length; _j < _len1; i = ++_j) { | ||
short_name = short_names[i]; | ||
@@ -218,7 +209,24 @@ option = this.short_options[short_name]; | ||
rt_argv = rt_argv.concat(argv); | ||
for (_k = 0, _len2 = argv.length; _k < _len2; _k++) { | ||
arg = argv[_k]; | ||
if ((_ref1 = this.events['']) != null) { | ||
_ref1.call(this, arg); | ||
} | ||
} | ||
break; | ||
} else { | ||
rt_argv.push(arg); | ||
if ((_ref2 = this.events['']) != null) { | ||
_ref2.call(this, arg); | ||
} | ||
} | ||
} | ||
_ref3 = Object.keys(rt_options); | ||
for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) { | ||
name = _ref3[_l]; | ||
sname = this.long_options[name].short_name; | ||
if (sname !== '') { | ||
rt_options[sname] = rt_options[name]; | ||
} | ||
} | ||
} catch (_error) { | ||
@@ -228,6 +236,4 @@ e = _error; | ||
} | ||
this.parsedOption = new ParsedOption(rt_argv, rt_options); | ||
this.argv = rt_argv; | ||
this.options = rt_options; | ||
this.trigger_events_(); | ||
return this; | ||
@@ -237,7 +243,7 @@ }; | ||
Getopt.prototype.parse_system = function() { | ||
return this.parse(process.argv.slice(2)); | ||
return this.parseSystem(); | ||
}; | ||
Getopt.prototype.parseSystem = function() { | ||
return this.parse_system(); | ||
return this.parse(process.argv.slice(2)); | ||
}; | ||
@@ -250,35 +256,57 @@ | ||
Getopt.prototype.sort = function() { | ||
return this.long_names.sort(function(a, b) { | ||
return a > b && 1 || a < b && -1 || 0; | ||
}); | ||
}; | ||
Getopt.prototype.getHelp = function() { | ||
var comment, definition, long_name, opt, option, options, short_name, strs, ws, _i, _len, _ref; | ||
var comment, def, definition, i, line, lines, long_name, n, option, options, short_name, table, td, tr, ws, _i, _j, _k, _len, _len1, _len2, _ref; | ||
ws = []; | ||
options = []; | ||
table = []; | ||
_ref = this.long_names; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
long_name = _ref[_i]; | ||
tr = []; | ||
option = this.long_options[long_name]; | ||
short_name = option.short_name, long_name = option.long_name, comment = option.comment, definition = option.definition; | ||
if (short_name && short_name === long_name) { | ||
opt = "-" + short_name; | ||
} else if (short_name) { | ||
opt = "-" + short_name + ", --" + definition; | ||
} else { | ||
opt = " --" + definition; | ||
short_name = option.short_name, long_name = option.long_name, comment = option.comment, definition = option.definition, def = option.def; | ||
tr.push(short_name ? short_name === long_name ? " -" + short_name : " -" + short_name + ", --" + definition : " --" + definition); | ||
tr.push(" " + comment); | ||
if (def) { | ||
tr.push(" (default: " + def + ")"); | ||
} | ||
ws[0] = Math.max(ws[0] >> 0, opt.length); | ||
options.push([opt, comment]); | ||
table.push(tr); | ||
} | ||
strs = (function() { | ||
var _j, _len1, _results; | ||
for (_j = 0, _len1 = table.length; _j < _len1; _j++) { | ||
tr = table[_j]; | ||
for (i = _k = 0, _len2 = tr.length; _k < _len2; i = ++_k) { | ||
td = tr[i]; | ||
if (ws[i] == null) { | ||
ws[i] = 0; | ||
} | ||
ws[i] = Math.max(ws[i], td.length); | ||
} | ||
} | ||
lines = (function() { | ||
var _l, _len3, _len4, _m, _results; | ||
_results = []; | ||
for (_j = 0, _len1 = options.length; _j < _len1; _j++) { | ||
option = options[_j]; | ||
opt = option[0], comment = option[1]; | ||
while (opt.length < ws[0]) { | ||
opt += ' '; | ||
for (_l = 0, _len3 = table.length; _l < _len3; _l++) { | ||
tr = table[_l]; | ||
line = ''; | ||
for (i = _m = 0, _len4 = tr.length; _m < _len4; i = ++_m) { | ||
td = tr[i]; | ||
if (i) { | ||
n = ws[i - 1] - tr[i - 1].length; | ||
while (n--) { | ||
line += ' '; | ||
} | ||
} | ||
line += td; | ||
} | ||
_results.push(" " + opt + " " + comment); | ||
_results.push(line.trimRight()); | ||
} | ||
return _results; | ||
})(); | ||
return this.help.replace('[[OPTIONS]]', strs.join("\n")); | ||
return this.help.replace('[[OPTIONS]]', lines.join("\n")); | ||
}; | ||
@@ -285,0 +313,0 @@ |
{ | ||
"name": "node-getopt", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"author" : "Jiang Miao <jiangfriend@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "featured command line args parser", |
@@ -92,3 +92,3 @@ node-getopt | ||
['' , 'long' , 'long option.'], | ||
['S' , 'short-with-arg=ARG' , 'option with argument'], | ||
['S' , 'short-with-arg=ARG' , 'option with argument', 'S'], | ||
['L' , 'long-with-arg=ARG' , 'long option with argument'], | ||
@@ -122,3 +122,3 @@ ['' , 'color[=COLOR]' , 'COLOR is optional'], | ||
--long long option. | ||
-S, --short-with-arg=ARG option with argument | ||
-S, --short-with-arg=ARG option with argument (default: S) | ||
-L, --long-with-arg=ARG long option with argument | ||
@@ -205,4 +205,4 @@ --color[=COLOR] COLOR is optional | ||
constructor(Array options) | ||
options is a set of option. each option contains 3 fields. | ||
[short_name, long_name_with_definition, comment] | ||
options is a set of option. each option contains 4 fields. | ||
[short_name, long_name_with_definition, comment, default] | ||
Definition: | ||
@@ -242,4 +242,4 @@ * '=ARG': has argument | ||
Getpot on(String optionName, Function<Array argv, Object options> action) | ||
after parsing, trigger the action if optionName is found. | ||
Getpot on(String optionName, Function<Value> action) | ||
trigger the action when optionName is found. | ||
the 'this' in action will be the instance of Getopt. | ||
@@ -250,2 +250,3 @@ | ||
Getopt Static Methods: | ||
@@ -252,0 +253,0 @@ |
// Generated by ToffeeScript 1.6.3-4 | ||
(function() { | ||
var Getopt, assert, e, eq, getopt, throws, | ||
var Getopt, assert, e, eq, getopt, opt, res, throws, | ||
_this = this; | ||
@@ -20,3 +20,4 @@ | ||
options: { | ||
'has-argument': 'a-value' | ||
'has-argument': 'a-value', | ||
'a': 'a-value' | ||
} | ||
@@ -27,3 +28,4 @@ }, 'has-argument'); | ||
options: { | ||
'has-argument': 'a-value' | ||
'has-argument': 'a-value', | ||
'a': 'a-value' | ||
} | ||
@@ -34,3 +36,4 @@ }, 'has-argument'); | ||
options: { | ||
'has-argument': "one\ntwo" | ||
'has-argument': "one\ntwo", | ||
'a': "one\ntwo" | ||
} | ||
@@ -42,5 +45,6 @@ }, 'has-argument'); | ||
options: { | ||
'has-argument': 'one two three' | ||
'has-argument': 'one two three', | ||
'a': 'one two three' | ||
} | ||
}, 'has-argument'); | ||
}); | ||
eq(getopt.parse(['-baone two three']), { | ||
@@ -50,3 +54,5 @@ argv: [], | ||
'has-argument': 'one two three', | ||
'no-argument': true | ||
'no-argument': true, | ||
'a': 'one two three', | ||
'b': true | ||
} | ||
@@ -58,3 +64,5 @@ }, 'has-argument'); | ||
'has-argument': '<a >', | ||
'no-argument': true | ||
'no-argument': true, | ||
'a': '<a >', | ||
'b': true | ||
} | ||
@@ -93,3 +101,4 @@ }, 'has-argument'); | ||
options: { | ||
'help': true | ||
'help': true, | ||
'h': true | ||
} | ||
@@ -108,5 +117,50 @@ }, 'long option'); | ||
'multi': ['a', 'b', 'c'], | ||
'short': true | ||
'short': true, | ||
'h': true, | ||
'm': ['a', 'b', 'c'], | ||
's': true | ||
} | ||
}); | ||
opt = new Getopt([['c', 'constructor', 'constructor']]).parse(['-c']); | ||
eq(opt, { | ||
argv: [], | ||
options: { | ||
'constructor': true, | ||
c: true | ||
} | ||
}); | ||
opt = new Getopt([['c', 'constructor', 'constructor'], ['', 'toString']]).parse([]); | ||
eq(opt, { | ||
argv: [], | ||
options: { | ||
'constructor': void 0, | ||
c: void 0, | ||
toString: void 0 | ||
} | ||
}); | ||
getopt = new Getopt([['A', '=+'], ['R', '=+'], ['c', 'constructor'], ['', 'long=']]); | ||
res = []; | ||
getopt.on('A', function(arg) { | ||
return res.push(arg); | ||
}).on('R', function(arg) { | ||
return res.push(arg); | ||
}).on('c', function(arg) { | ||
return res.push(arg); | ||
}).on('long', function(arg) { | ||
return res.push(arg); | ||
}).on('', function(arg) { | ||
return res.push(arg); | ||
}); | ||
getopt.parse(['n1', '-A', 'a1', '-R', 'r1', '-A', 'a2', '--constructor', '--long=l1', '--', 'n2']); | ||
assert.deepEqual(res, ['n1', 'a1', 'r1', 'a2', true, 'l1', 'n2']); | ||
opt = new Getopt([['a', '=', '', 'a'], ['M', '=+', '', ['m1', 'm2']]]).parse([]); | ||
eq(opt, { | ||
argv: [], | ||
options: { | ||
'a': 'a', | ||
'M': ['m1', 'm2'] | ||
} | ||
}); | ||
opt = new Getopt([['s', ''], ['', 'long'], ['b', 'both']]); | ||
assert.deepEqual(opt.long_names, ['s', 'long', 'both']); | ||
console.info("\x1b[32mTest passed.\x1b[0m"); | ||
@@ -113,0 +167,0 @@ } catch (_error) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37649
14
555
262