2010-02-14 15:16:33 -05:00
|
|
|
(function(){
|
2010-02-25 06:15:58 -05:00
|
|
|
var LONG_FLAG, OPTIONAL, SHORT_FLAG, build_rule, build_rules, op;
|
|
|
|
// Create an OptionParser with a list of valid options, in the form:
|
|
|
|
// [short-flag (optional), long-flag, description]
|
|
|
|
// And an optional banner for the usage help.
|
2010-02-24 17:57:58 -05:00
|
|
|
op = (exports.OptionParser = function OptionParser(rules, banner) {
|
|
|
|
this.banner = banner || 'Usage: [Options]';
|
2010-02-14 15:16:33 -05:00
|
|
|
this.rules = build_rules(rules);
|
|
|
|
return this;
|
|
|
|
});
|
2010-02-25 06:15:58 -05:00
|
|
|
// Parse the argument array, populating an options object with all of the
|
|
|
|
// specified options, and returning it. options.arguments will be an array
|
|
|
|
// containing the remaning non-option arguments.
|
2010-02-14 15:16:33 -05:00
|
|
|
op.prototype.parse = function parse(args) {
|
2010-02-24 17:57:58 -05:00
|
|
|
var _a, _b, arg, is_option, options, rule;
|
|
|
|
arguments = Array.prototype.slice.call(arguments, 0);
|
|
|
|
options = {
|
|
|
|
arguments: []
|
|
|
|
};
|
2010-02-25 06:15:58 -05:00
|
|
|
args = args.slice(0);
|
|
|
|
while (arg = args.shift()) {
|
2010-02-14 15:16:33 -05:00
|
|
|
is_option = false;
|
2010-02-16 18:00:40 -05:00
|
|
|
_a = this.rules;
|
|
|
|
for (_b = 0; _b < _a.length; _b++) {
|
|
|
|
rule = _a[_b];
|
2010-02-14 15:16:33 -05:00
|
|
|
if (rule.letter === arg || rule.flag === arg) {
|
2010-02-25 06:15:58 -05:00
|
|
|
options[rule.name] = rule.has_argument ? args.shift() : true;
|
2010-02-14 15:16:33 -05:00
|
|
|
is_option = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(is_option)) {
|
2010-02-24 17:57:58 -05:00
|
|
|
options.arguments.push(arg);
|
2010-02-14 15:16:33 -05:00
|
|
|
}
|
|
|
|
}
|
2010-02-24 17:57:58 -05:00
|
|
|
return options;
|
2010-02-14 15:16:33 -05:00
|
|
|
};
|
|
|
|
// Return the help text for this OptionParser, for --help and such.
|
|
|
|
op.prototype.help = function help() {
|
2010-02-25 06:15:58 -05:00
|
|
|
var _a, _b, _c, _d, _e, _f, _g, i, let_part, lines, rule, spaces;
|
|
|
|
lines = [this.banner, '', 'Available options:'];
|
2010-02-16 18:00:40 -05:00
|
|
|
_a = this.rules;
|
|
|
|
for (_b = 0; _b < _a.length; _b++) {
|
|
|
|
rule = _a[_b];
|
2010-02-25 06:15:58 -05:00
|
|
|
spaces = 15 - rule.flag.length;
|
|
|
|
spaces = spaces > 0 ? (function() {
|
|
|
|
_c = []; _f = 0; _g = spaces;
|
|
|
|
for (_e = 0, i=_f; (_f <= _g ? i <= _g : i >= _g); (_f <= _g ? i += 1 : i -= 1), _e++) {
|
|
|
|
_c.push(' ');
|
|
|
|
}
|
|
|
|
return _c;
|
|
|
|
}).call(this).join('') : '';
|
|
|
|
let_part = rule.letter ? rule.letter + ', ' : ' ';
|
|
|
|
lines.push(' ' + let_part + rule.flag + spaces + rule.description);
|
2010-02-14 15:16:33 -05:00
|
|
|
}
|
|
|
|
return lines.join('\n');
|
|
|
|
};
|
|
|
|
// Regex matchers for option flags.
|
2010-02-21 13:48:38 -05:00
|
|
|
LONG_FLAG = /^(--[\w\-]+)/;
|
2010-02-14 15:16:33 -05:00
|
|
|
SHORT_FLAG = /^(-\w+)/;
|
|
|
|
OPTIONAL = /\[(.+)\]/;
|
|
|
|
// Build rules from a list of valid switch tuples in the form:
|
|
|
|
// [letter-flag, long-flag, help], or [long-flag, help].
|
|
|
|
build_rules = function build_rules(rules) {
|
2010-02-16 18:00:40 -05:00
|
|
|
var _a, _b, _c, tuple;
|
|
|
|
_a = []; _b = rules;
|
|
|
|
for (_c = 0; _c < _b.length; _c++) {
|
|
|
|
tuple = _b[_c];
|
|
|
|
_a.push((function() {
|
2010-02-14 15:16:33 -05:00
|
|
|
if (tuple.length < 3) {
|
|
|
|
tuple.unshift(null);
|
|
|
|
}
|
|
|
|
return build_rule.apply(this, tuple);
|
|
|
|
}).call(this));
|
|
|
|
}
|
2010-02-16 18:00:40 -05:00
|
|
|
return _a;
|
2010-02-14 15:16:33 -05:00
|
|
|
};
|
|
|
|
// Build a rule from a short-letter-flag, long-form-flag, and help text.
|
|
|
|
build_rule = function build_rule(letter, flag, description) {
|
|
|
|
var match;
|
|
|
|
match = flag.match(OPTIONAL);
|
2010-02-14 20:50:45 -05:00
|
|
|
flag = flag.match(LONG_FLAG)[1];
|
2010-02-14 15:16:33 -05:00
|
|
|
return {
|
2010-02-14 20:50:45 -05:00
|
|
|
name: flag.substr(2),
|
2010-02-14 15:16:33 -05:00
|
|
|
letter: letter,
|
|
|
|
flag: flag,
|
|
|
|
description: description,
|
2010-02-25 06:15:58 -05:00
|
|
|
has_argument: !!(match && match[1])
|
2010-02-14 15:16:33 -05:00
|
|
|
};
|
|
|
|
};
|
2010-02-24 18:56:32 -05:00
|
|
|
})();
|