2010-02-14 15:16:33 -05:00
|
|
|
(function(){
|
2010-02-27 19:46:45 -05:00
|
|
|
var LONG_FLAG, MULTI_FLAG, OPTIONAL, OptionParser, SHORT_FLAG, build_rule, build_rules, normalize_arguments;
|
2010-03-07 13:41:15 -05:00
|
|
|
// A simple **OptionParser** class to parse option flags from the command-line.
|
|
|
|
// Use it like so:
|
|
|
|
// parser: new OptionParser switches, help_banner
|
|
|
|
// options: parser.parse process.argv
|
2010-02-27 19:46:45 -05:00
|
|
|
exports.OptionParser = (function() {
|
|
|
|
OptionParser = function OptionParser(rules, banner) {
|
|
|
|
this.banner = banner;
|
|
|
|
this.rules = build_rules(rules);
|
|
|
|
return this;
|
2010-02-24 17:57:58 -05:00
|
|
|
};
|
2010-03-07 13:41:15 -05:00
|
|
|
// Initialize with a list of valid options, in the form:
|
|
|
|
// [short-flag, long-flag, description]
|
|
|
|
// Along with an an optional banner for the usage help.
|
|
|
|
// Parse the list of arguments, 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. This is a simpler API than
|
|
|
|
// many option parsers that allow you to attach callback actions for every
|
|
|
|
// flag. Instead, you're responsible for interpreting the options object.
|
2010-02-27 19:46:45 -05:00
|
|
|
OptionParser.prototype.parse = function parse(args) {
|
|
|
|
var _a, _b, _c, arg, is_option, matched_rule, options, rule;
|
|
|
|
options = {
|
|
|
|
arguments: []
|
|
|
|
};
|
|
|
|
args = normalize_arguments(args);
|
|
|
|
while (arg = args.shift()) {
|
|
|
|
is_option = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
|
|
|
matched_rule = false;
|
2010-03-27 16:04:47 -04:00
|
|
|
_b = this.rules;
|
|
|
|
for (_a = 0, _c = _b.length; _a < _c; _a++) {
|
|
|
|
rule = _b[_a];
|
2010-03-07 13:41:15 -05:00
|
|
|
if (rule.short_flag === arg || rule.long_flag === arg) {
|
2010-02-27 19:46:45 -05:00
|
|
|
options[rule.name] = rule.has_argument ? args.shift() : true;
|
|
|
|
matched_rule = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_option && !matched_rule) {
|
|
|
|
throw new Error("unrecognized option: " + arg);
|
|
|
|
}
|
|
|
|
if (!(is_option)) {
|
|
|
|
options.arguments.push(arg);
|
2010-02-14 15:16:33 -05:00
|
|
|
}
|
|
|
|
}
|
2010-02-27 19:46:45 -05:00
|
|
|
return options;
|
|
|
|
};
|
2010-03-07 13:41:15 -05:00
|
|
|
// Return the help text for this **OptionParser**, listing and describing all
|
|
|
|
// of the valid options, for `--help` and such.
|
2010-02-27 19:46:45 -05:00
|
|
|
OptionParser.prototype.help = function help() {
|
2010-03-27 16:04:47 -04:00
|
|
|
var _a, _b, _c, _d, _e, _f, _g, i, let_part, lines, rule, spaces;
|
2010-02-27 19:46:45 -05:00
|
|
|
lines = ['Available options:'];
|
|
|
|
if (this.banner) {
|
2010-03-23 00:18:50 -04:00
|
|
|
lines.unshift("" + this.banner + "\n");
|
2010-02-25 18:54:08 -05:00
|
|
|
}
|
2010-03-27 16:04:47 -04:00
|
|
|
_b = this.rules;
|
|
|
|
for (_a = 0, _c = _b.length; _a < _c; _a++) {
|
|
|
|
rule = _b[_a];
|
2010-03-07 13:41:15 -05:00
|
|
|
spaces = 15 - rule.long_flag.length;
|
2010-02-27 19:46:45 -05:00
|
|
|
spaces = spaces > 0 ? (function() {
|
2010-03-27 16:04:47 -04:00
|
|
|
_d = []; _f = 0; _g = spaces;
|
|
|
|
for (_e = 0, i = _f; (_f <= _g ? i <= _g : i >= _g); (_f <= _g ? i += 1 : i -= 1), _e++) {
|
2010-02-27 19:46:45 -05:00
|
|
|
_d.push(' ');
|
|
|
|
}
|
|
|
|
return _d;
|
|
|
|
}).call(this).join('') : '';
|
2010-03-07 13:41:15 -05:00
|
|
|
let_part = rule.short_flag ? rule.short_flag + ', ' : ' ';
|
|
|
|
lines.push(" " + let_part + (rule.long_flag) + spaces + (rule.description));
|
2010-02-14 15:16:33 -05:00
|
|
|
}
|
2010-03-06 13:59:11 -05:00
|
|
|
return "\n" + (lines.join('\n')) + "\n";
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
|
|
|
return OptionParser;
|
|
|
|
}).call(this);
|
2010-03-07 13:41:15 -05:00
|
|
|
// Helpers
|
|
|
|
// -------
|
2010-02-14 15:16:33 -05:00
|
|
|
// Regex matchers for option flags.
|
2010-02-25 18:36:43 -05:00
|
|
|
LONG_FLAG = /^(--\w[\w\-]+)/;
|
2010-02-25 19:06:08 -05:00
|
|
|
SHORT_FLAG = /^(-\w)/;
|
|
|
|
MULTI_FLAG = /^-(\w{2,})/;
|
2010-02-14 15:16:33 -05:00
|
|
|
OPTIONAL = /\[(.+)\]/;
|
2010-03-07 13:41:15 -05:00
|
|
|
// Build and return the list of option rules. If the optional *short-flag* is
|
|
|
|
// unspecified, leave it out by padding with `null`.
|
2010-02-14 15:16:33 -05:00
|
|
|
build_rules = function build_rules(rules) {
|
2010-02-25 23:39:14 -05:00
|
|
|
var _a, _b, _c, _d, tuple;
|
2010-03-27 16:04:47 -04:00
|
|
|
_a = []; _c = rules;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
tuple = _c[_b];
|
2010-02-16 18:00:40 -05:00
|
|
|
_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
|
|
|
};
|
2010-03-07 13:41:15 -05:00
|
|
|
// Build a rule from a `-o` short flag, a `--output [DIR]` long flag, and the
|
|
|
|
// description of what the option does.
|
|
|
|
build_rule = function build_rule(short_flag, long_flag, description) {
|
2010-02-14 15:16:33 -05:00
|
|
|
var match;
|
2010-03-07 13:41:15 -05:00
|
|
|
match = long_flag.match(OPTIONAL);
|
|
|
|
long_flag = long_flag.match(LONG_FLAG)[1];
|
2010-02-14 15:16:33 -05:00
|
|
|
return {
|
2010-03-07 13:41:15 -05:00
|
|
|
name: long_flag.substr(2),
|
|
|
|
short_flag: short_flag,
|
|
|
|
long_flag: long_flag,
|
2010-02-14 15:16:33 -05:00
|
|
|
description: description,
|
2010-02-25 06:15:58 -05:00
|
|
|
has_argument: !!(match && match[1])
|
2010-02-14 15:16:33 -05:00
|
|
|
};
|
|
|
|
};
|
2010-03-07 13:41:15 -05:00
|
|
|
// Normalize arguments by expanding merged flags into multiple flags. This allows
|
|
|
|
// you to have `-wl` be the same as `--watch --lint`.
|
2010-02-25 19:06:08 -05:00
|
|
|
normalize_arguments = function normalize_arguments(args) {
|
2010-02-25 23:39:14 -05:00
|
|
|
var _a, _b, _c, _d, _e, _f, arg, l, match, result;
|
2010-02-25 19:06:08 -05:00
|
|
|
args = args.slice(0);
|
|
|
|
result = [];
|
2010-03-27 16:04:47 -04:00
|
|
|
_b = args;
|
|
|
|
for (_a = 0, _c = _b.length; _a < _c; _a++) {
|
|
|
|
arg = _b[_a];
|
2010-02-25 19:06:08 -05:00
|
|
|
if ((match = arg.match(MULTI_FLAG))) {
|
2010-03-27 16:04:47 -04:00
|
|
|
_e = match[1].split('');
|
|
|
|
for (_d = 0, _f = _e.length; _d < _f; _d++) {
|
|
|
|
l = _e[_d];
|
2010-02-25 19:06:08 -05:00
|
|
|
result.push('-' + l);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2010-02-24 18:56:32 -05:00
|
|
|
})();
|