2010-02-14 20:16:33 +00:00
|
|
|
(function(){
|
2010-02-26 00:06:08 +00:00
|
|
|
var LONG_FLAG, MULTI_FLAG, OPTIONAL, SHORT_FLAG, build_rule, build_rules, normalize_arguments, op;
|
2010-02-25 11:15:58 +00:00
|
|
|
// 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 22:57:58 +00:00
|
|
|
op = (exports.OptionParser = function OptionParser(rules, banner) {
|
2010-02-26 02:43:42 +00:00
|
|
|
this.banner = banner;
|
2010-02-14 20:16:33 +00:00
|
|
|
this.rules = build_rules(rules);
|
|
|
|
return this;
|
|
|
|
});
|
2010-02-25 11:15:58 +00: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 20:16:33 +00:00
|
|
|
op.prototype.parse = function parse(args) {
|
2010-02-26 04:39:14 +00:00
|
|
|
var _a, _b, _c, arg, is_option, matched_rule, options, rule;
|
2010-02-24 22:57:58 +00:00
|
|
|
arguments = Array.prototype.slice.call(arguments, 0);
|
|
|
|
options = {
|
|
|
|
arguments: []
|
|
|
|
};
|
2010-02-26 00:06:08 +00:00
|
|
|
args = normalize_arguments(args);
|
2010-02-25 11:15:58 +00:00
|
|
|
while (arg = args.shift()) {
|
2010-02-25 23:36:43 +00:00
|
|
|
is_option = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
2010-02-25 23:54:08 +00:00
|
|
|
matched_rule = false;
|
2010-02-16 23:00:40 +00:00
|
|
|
_a = this.rules;
|
2010-02-26 04:39:14 +00:00
|
|
|
for (_b = 0, _c = _a.length; _b < _c; _b++) {
|
2010-02-16 23:00:40 +00:00
|
|
|
rule = _a[_b];
|
2010-02-14 20:16:33 +00:00
|
|
|
if (rule.letter === arg || rule.flag === arg) {
|
2010-02-25 11:15:58 +00:00
|
|
|
options[rule.name] = rule.has_argument ? args.shift() : true;
|
2010-02-25 23:54:08 +00:00
|
|
|
matched_rule = true;
|
2010-02-14 20:16:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-02-25 23:54:08 +00:00
|
|
|
if (is_option && !matched_rule) {
|
|
|
|
throw new Error("unrecognized option: " + arg);
|
|
|
|
}
|
2010-02-14 20:16:33 +00:00
|
|
|
if (!(is_option)) {
|
2010-02-24 22:57:58 +00:00
|
|
|
options.arguments.push(arg);
|
2010-02-14 20:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-24 22:57:58 +00:00
|
|
|
return options;
|
2010-02-14 20:16:33 +00:00
|
|
|
};
|
|
|
|
// Return the help text for this OptionParser, for --help and such.
|
|
|
|
op.prototype.help = function help() {
|
2010-02-26 04:39:14 +00:00
|
|
|
var _a, _b, _c, _d, _e, _f, _g, _h, i, let_part, lines, rule, spaces;
|
2010-02-26 02:43:42 +00:00
|
|
|
lines = ['Available options:'];
|
|
|
|
if (this.banner) {
|
|
|
|
lines.unshift(this.banner + '\n');
|
|
|
|
}
|
2010-02-16 23:00:40 +00:00
|
|
|
_a = this.rules;
|
2010-02-26 04:39:14 +00:00
|
|
|
for (_b = 0, _c = _a.length; _b < _c; _b++) {
|
2010-02-16 23:00:40 +00:00
|
|
|
rule = _a[_b];
|
2010-02-25 11:15:58 +00:00
|
|
|
spaces = 15 - rule.flag.length;
|
|
|
|
spaces = spaces > 0 ? (function() {
|
2010-02-26 04:39:14 +00:00
|
|
|
_d = []; _g = 0; _h = spaces;
|
|
|
|
for (_f = 0, i = _g; (_g <= _h ? i <= _h : i >= _h); (_g <= _h ? i += 1 : i -= 1), _f++) {
|
|
|
|
_d.push(' ');
|
2010-02-25 11:15:58 +00:00
|
|
|
}
|
2010-02-26 04:39:14 +00:00
|
|
|
return _d;
|
2010-02-25 11:15:58 +00:00
|
|
|
}).call(this).join('') : '';
|
|
|
|
let_part = rule.letter ? rule.letter + ', ' : ' ';
|
|
|
|
lines.push(' ' + let_part + rule.flag + spaces + rule.description);
|
2010-02-14 20:16:33 +00:00
|
|
|
}
|
|
|
|
return lines.join('\n');
|
|
|
|
};
|
|
|
|
// Regex matchers for option flags.
|
2010-02-25 23:36:43 +00:00
|
|
|
LONG_FLAG = /^(--\w[\w\-]+)/;
|
2010-02-26 00:06:08 +00:00
|
|
|
SHORT_FLAG = /^(-\w)/;
|
|
|
|
MULTI_FLAG = /^-(\w{2,})/;
|
2010-02-14 20:16:33 +00:00
|
|
|
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-26 04:39:14 +00:00
|
|
|
var _a, _b, _c, _d, tuple;
|
2010-02-16 23:00:40 +00:00
|
|
|
_a = []; _b = rules;
|
2010-02-26 04:39:14 +00:00
|
|
|
for (_c = 0, _d = _b.length; _c < _d; _c++) {
|
2010-02-16 23:00:40 +00:00
|
|
|
tuple = _b[_c];
|
|
|
|
_a.push((function() {
|
2010-02-14 20:16:33 +00:00
|
|
|
if (tuple.length < 3) {
|
|
|
|
tuple.unshift(null);
|
|
|
|
}
|
|
|
|
return build_rule.apply(this, tuple);
|
|
|
|
}).call(this));
|
|
|
|
}
|
2010-02-16 23:00:40 +00:00
|
|
|
return _a;
|
2010-02-14 20:16:33 +00: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-15 01:50:45 +00:00
|
|
|
flag = flag.match(LONG_FLAG)[1];
|
2010-02-14 20:16:33 +00:00
|
|
|
return {
|
2010-02-15 01:50:45 +00:00
|
|
|
name: flag.substr(2),
|
2010-02-14 20:16:33 +00:00
|
|
|
letter: letter,
|
|
|
|
flag: flag,
|
|
|
|
description: description,
|
2010-02-25 11:15:58 +00:00
|
|
|
has_argument: !!(match && match[1])
|
2010-02-14 20:16:33 +00:00
|
|
|
};
|
|
|
|
};
|
2010-02-26 00:06:08 +00:00
|
|
|
// Normalize arguments by expanding merged flags into multiple flags.
|
|
|
|
normalize_arguments = function normalize_arguments(args) {
|
2010-02-26 04:39:14 +00:00
|
|
|
var _a, _b, _c, _d, _e, _f, arg, l, match, result;
|
2010-02-26 00:06:08 +00:00
|
|
|
args = args.slice(0);
|
|
|
|
result = [];
|
|
|
|
_a = args;
|
2010-02-26 04:39:14 +00:00
|
|
|
for (_b = 0, _c = _a.length; _b < _c; _b++) {
|
2010-02-26 00:06:08 +00:00
|
|
|
arg = _a[_b];
|
|
|
|
if ((match = arg.match(MULTI_FLAG))) {
|
2010-02-26 04:39:14 +00:00
|
|
|
_d = match[1].split('');
|
|
|
|
for (_e = 0, _f = _d.length; _e < _f; _e++) {
|
|
|
|
l = _d[_e];
|
2010-02-26 00:06:08 +00:00
|
|
|
result.push('-' + l);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2010-02-24 23:56:32 +00:00
|
|
|
})();
|