mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
converting the remainder of the CoffeeScript compiler (Rewriter, Scope, Optparse) to use classes
This commit is contained in:
parent
9f46c306e5
commit
e02ab76edf
6 changed files with 786 additions and 775 deletions
113
lib/optparse.js
113
lib/optparse.js
|
@ -1,67 +1,70 @@
|
|||
(function(){
|
||||
var LONG_FLAG, MULTI_FLAG, OPTIONAL, SHORT_FLAG, build_rule, build_rules, normalize_arguments, op;
|
||||
var LONG_FLAG, MULTI_FLAG, OPTIONAL, OptionParser, SHORT_FLAG, build_rule, build_rules, normalize_arguments;
|
||||
// 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.
|
||||
op = (exports.OptionParser = function OptionParser(rules, banner) {
|
||||
this.banner = banner;
|
||||
this.rules = build_rules(rules);
|
||||
return this;
|
||||
});
|
||||
// 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.
|
||||
op.prototype.parse = function parse(args) {
|
||||
var _a, _b, _c, arg, is_option, matched_rule, options, rule;
|
||||
arguments = Array.prototype.slice.call(arguments, 0);
|
||||
options = {
|
||||
arguments: []
|
||||
exports.OptionParser = (function() {
|
||||
OptionParser = function OptionParser(rules, banner) {
|
||||
this.banner = banner;
|
||||
this.rules = build_rules(rules);
|
||||
return this;
|
||||
};
|
||||
args = normalize_arguments(args);
|
||||
while (arg = args.shift()) {
|
||||
is_option = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
||||
matched_rule = false;
|
||||
// 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.
|
||||
OptionParser.prototype.parse = function parse(args) {
|
||||
var _a, _b, _c, arg, is_option, matched_rule, options, rule;
|
||||
arguments = Array.prototype.slice.call(arguments, 0);
|
||||
options = {
|
||||
arguments: []
|
||||
};
|
||||
args = normalize_arguments(args);
|
||||
while (arg = args.shift()) {
|
||||
is_option = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
||||
matched_rule = false;
|
||||
_a = this.rules;
|
||||
for (_b = 0, _c = _a.length; _b < _c; _b++) {
|
||||
rule = _a[_b];
|
||||
if (rule.letter === arg || rule.flag === arg) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
return options;
|
||||
};
|
||||
// Return the help text for this OptionParser, for --help and such.
|
||||
OptionParser.prototype.help = function help() {
|
||||
var _a, _b, _c, _d, _e, _f, _g, _h, i, let_part, lines, rule, spaces;
|
||||
lines = ['Available options:'];
|
||||
if (this.banner) {
|
||||
lines.unshift(this.banner + '\n');
|
||||
}
|
||||
_a = this.rules;
|
||||
for (_b = 0, _c = _a.length; _b < _c; _b++) {
|
||||
rule = _a[_b];
|
||||
if (rule.letter === arg || rule.flag === arg) {
|
||||
options[rule.name] = rule.has_argument ? args.shift() : true;
|
||||
matched_rule = true;
|
||||
break;
|
||||
}
|
||||
spaces = 15 - rule.flag.length;
|
||||
spaces = spaces > 0 ? (function() {
|
||||
_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(' ');
|
||||
}
|
||||
return _d;
|
||||
}).call(this).join('') : '';
|
||||
let_part = rule.letter ? rule.letter + ', ' : ' ';
|
||||
lines.push(' ' + let_part + rule.flag + spaces + rule.description);
|
||||
}
|
||||
if (is_option && !matched_rule) {
|
||||
throw new Error("unrecognized option: " + arg);
|
||||
}
|
||||
if (!(is_option)) {
|
||||
options.arguments.push(arg);
|
||||
}
|
||||
}
|
||||
return options;
|
||||
};
|
||||
// Return the help text for this OptionParser, for --help and such.
|
||||
op.prototype.help = function help() {
|
||||
var _a, _b, _c, _d, _e, _f, _g, _h, i, let_part, lines, rule, spaces;
|
||||
lines = ['Available options:'];
|
||||
if (this.banner) {
|
||||
lines.unshift(this.banner + '\n');
|
||||
}
|
||||
_a = this.rules;
|
||||
for (_b = 0, _c = _a.length; _b < _c; _b++) {
|
||||
rule = _a[_b];
|
||||
spaces = 15 - rule.flag.length;
|
||||
spaces = spaces > 0 ? (function() {
|
||||
_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(' ');
|
||||
}
|
||||
return _d;
|
||||
}).call(this).join('') : '';
|
||||
let_part = rule.letter ? rule.letter + ', ' : ' ';
|
||||
lines.push(' ' + let_part + rule.flag + spaces + rule.description);
|
||||
}
|
||||
return lines.join('\n');
|
||||
};
|
||||
return lines.join('\n');
|
||||
};
|
||||
return OptionParser;
|
||||
}).call(this);
|
||||
// Regex matchers for option flags.
|
||||
LONG_FLAG = /^(--\w[\w\-]+)/;
|
||||
SHORT_FLAG = /^(-\w)/;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue