mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
documenting optparse.coffee and repl.coffee
This commit is contained in:
parent
4906cf1aff
commit
d46daa1d7c
12 changed files with 154 additions and 102 deletions
|
@ -1,17 +1,23 @@
|
|||
(function(){
|
||||
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.
|
||||
// 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
|
||||
exports.OptionParser = (function() {
|
||||
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.
|
||||
// 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.
|
||||
OptionParser.prototype.parse = function parse(args) {
|
||||
var _a, _b, _c, arg, is_option, matched_rule, options, rule;
|
||||
arguments = Array.prototype.slice.call(arguments, 0);
|
||||
|
@ -25,7 +31,7 @@
|
|||
_a = this.rules;
|
||||
for (_b = 0, _c = _a.length; _b < _c; _b++) {
|
||||
rule = _a[_b];
|
||||
if (rule.letter === arg || rule.flag === arg) {
|
||||
if (rule.short_flag === arg || rule.long_flag === arg) {
|
||||
options[rule.name] = rule.has_argument ? args.shift() : true;
|
||||
matched_rule = true;
|
||||
break;
|
||||
|
@ -40,7 +46,8 @@
|
|||
}
|
||||
return options;
|
||||
};
|
||||
// Return the help text for this OptionParser, for --help and such.
|
||||
// Return the help text for this **OptionParser**, listing and describing all
|
||||
// of the valid options, 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:'];
|
||||
|
@ -50,7 +57,7 @@
|
|||
_a = this.rules;
|
||||
for (_b = 0, _c = _a.length; _b < _c; _b++) {
|
||||
rule = _a[_b];
|
||||
spaces = 15 - rule.flag.length;
|
||||
spaces = 15 - rule.long_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++) {
|
||||
|
@ -58,20 +65,22 @@
|
|||
}
|
||||
return _d;
|
||||
}).call(this).join('') : '';
|
||||
let_part = rule.letter ? rule.letter + ', ' : ' ';
|
||||
lines.push(" " + let_part + (rule.flag) + spaces + (rule.description));
|
||||
let_part = rule.short_flag ? rule.short_flag + ', ' : ' ';
|
||||
lines.push(" " + let_part + (rule.long_flag) + spaces + (rule.description));
|
||||
}
|
||||
return "\n" + (lines.join('\n')) + "\n";
|
||||
};
|
||||
return OptionParser;
|
||||
}).call(this);
|
||||
// Helpers
|
||||
// -------
|
||||
// Regex matchers for option flags.
|
||||
LONG_FLAG = /^(--\w[\w\-]+)/;
|
||||
SHORT_FLAG = /^(-\w)/;
|
||||
MULTI_FLAG = /^-(\w{2,})/;
|
||||
OPTIONAL = /\[(.+)\]/;
|
||||
// Build rules from a list of valid switch tuples in the form:
|
||||
// [letter-flag, long-flag, help], or [long-flag, help].
|
||||
// Build and return the list of option rules. If the optional *short-flag* is
|
||||
// unspecified, leave it out by padding with `null`.
|
||||
build_rules = function build_rules(rules) {
|
||||
var _a, _b, _c, _d, tuple;
|
||||
_a = []; _b = rules;
|
||||
|
@ -86,20 +95,22 @@
|
|||
}
|
||||
return _a;
|
||||
};
|
||||
// Build a rule from a short-letter-flag, long-form-flag, and help text.
|
||||
build_rule = function build_rule(letter, flag, description) {
|
||||
// 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) {
|
||||
var match;
|
||||
match = flag.match(OPTIONAL);
|
||||
flag = flag.match(LONG_FLAG)[1];
|
||||
match = long_flag.match(OPTIONAL);
|
||||
long_flag = long_flag.match(LONG_FLAG)[1];
|
||||
return {
|
||||
name: flag.substr(2),
|
||||
letter: letter,
|
||||
flag: flag,
|
||||
name: long_flag.substr(2),
|
||||
short_flag: short_flag,
|
||||
long_flag: long_flag,
|
||||
description: description,
|
||||
has_argument: !!(match && match[1])
|
||||
};
|
||||
};
|
||||
// Normalize arguments by expanding merged flags into multiple flags.
|
||||
// Normalize arguments by expanding merged flags into multiple flags. This allows
|
||||
// you to have `-wl` be the same as `--watch --lint`.
|
||||
normalize_arguments = function normalize_arguments(args) {
|
||||
var _a, _b, _c, _d, _e, _f, arg, l, match, result;
|
||||
args = args.slice(0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue