2010-07-24 15:31:43 +00:00
|
|
|
(function() {
|
2010-06-12 23:05:13 +00:00
|
|
|
var LONG_FLAG, MULTI_FLAG, OPTIONAL, OptionParser, SHORT_FLAG, buildRule, buildRules, normalizeArguments;
|
2010-02-28 00:46:45 +00:00
|
|
|
exports.OptionParser = (function() {
|
2010-05-15 03:40:04 +00:00
|
|
|
OptionParser = function(rules, banner) {
|
2010-02-28 00:46:45 +00:00
|
|
|
this.banner = banner;
|
2010-06-12 23:05:13 +00:00
|
|
|
this.rules = buildRules(rules);
|
2010-02-28 00:46:45 +00:00
|
|
|
return this;
|
2010-02-24 22:57:58 +00:00
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
OptionParser.prototype.parse = function(args) {
|
Add command-line compiler hooks. To invoke, pass a file after -r and listen for any of these events: 'compile', 'success' and 'exception'. Example:
coffee -e -r ./snarl 'Hello!'
Contents of 'snarl.coffee' in the working directory:
http = require 'http'
CoffeeScript.on 'exception', (err) ->
client = http.createClient 9889, 'localhost'
request = client.request 'GET', '/?d={"action":1,"applicationName":"CoffeeScript","title":' + JSON.stringify(err.message) + ',"description":' + JSON.stringify(err.stack) + ',"priority":3}'
request.end()
err.handled = yes
To examine arguments available for each event (for debugging and getting started), use `puts JSON.stringify arguments`.
See http://nodejs.org/api.html#modules-309 and NODE_PATH for more details on how -r looks for files.
2010-08-07 17:24:37 +00:00
|
|
|
var _a, _b, _c, _d, _e, arg, i, isOption, matchedRule, options, rule, value;
|
2010-02-28 00:46:45 +00:00
|
|
|
options = {
|
|
|
|
arguments: []
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
args = normalizeArguments(args);
|
2010-07-11 13:57:42 +00:00
|
|
|
_a = args;
|
|
|
|
for (i = 0, _b = _a.length; i < _b; i++) {
|
|
|
|
arg = _a[i];
|
2010-06-12 23:05:13 +00:00
|
|
|
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
|
|
|
matchedRule = false;
|
2010-07-11 13:57:42 +00:00
|
|
|
_d = this.rules;
|
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
rule = _d[_c];
|
2010-06-12 23:05:13 +00:00
|
|
|
if (rule.shortFlag === arg || rule.longFlag === arg) {
|
Add command-line compiler hooks. To invoke, pass a file after -r and listen for any of these events: 'compile', 'success' and 'exception'. Example:
coffee -e -r ./snarl 'Hello!'
Contents of 'snarl.coffee' in the working directory:
http = require 'http'
CoffeeScript.on 'exception', (err) ->
client = http.createClient 9889, 'localhost'
request = client.request 'GET', '/?d={"action":1,"applicationName":"CoffeeScript","title":' + JSON.stringify(err.message) + ',"description":' + JSON.stringify(err.stack) + ',"priority":3}'
request.end()
err.handled = yes
To examine arguments available for each event (for debugging and getting started), use `puts JSON.stringify arguments`.
See http://nodejs.org/api.html#modules-309 and NODE_PATH for more details on how -r looks for files.
2010-08-07 17:24:37 +00:00
|
|
|
value = rule.hasArgument ? args[i += 1] : true;
|
|
|
|
options[rule.name] = rule.isList ? (options[rule.name] || []).concat(value) : value;
|
2010-06-12 23:05:13 +00:00
|
|
|
matchedRule = true;
|
2010-02-28 00:46:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
if (isOption && !matchedRule) {
|
2010-08-14 21:25:29 +00:00
|
|
|
throw new Error("unrecognized option: " + (arg));
|
2010-02-28 00:46:45 +00:00
|
|
|
}
|
2010-07-11 13:57:42 +00:00
|
|
|
if (!isOption) {
|
|
|
|
options.arguments = args.slice(i, args.length);
|
|
|
|
break;
|
2010-02-14 20:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-28 00:46:45 +00:00
|
|
|
return options;
|
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
OptionParser.prototype.help = function() {
|
2010-06-12 23:05:13 +00:00
|
|
|
var _a, _b, _c, _d, i, letPart, lines, rule, spaces;
|
2010-02-28 00:46:45 +00:00
|
|
|
lines = ['Available options:'];
|
|
|
|
if (this.banner) {
|
2010-08-14 21:25:29 +00:00
|
|
|
lines.unshift("" + (this.banner) + "\n");
|
2010-02-25 23:54:08 +00:00
|
|
|
}
|
2010-03-27 20:04:47 +00:00
|
|
|
_b = this.rules;
|
|
|
|
for (_a = 0, _c = _b.length; _a < _c; _a++) {
|
|
|
|
rule = _b[_a];
|
2010-06-12 23:05:13 +00:00
|
|
|
spaces = 15 - rule.longFlag.length;
|
2010-02-28 00:46:45 +00:00
|
|
|
spaces = spaces > 0 ? (function() {
|
2010-05-31 23:38:45 +00:00
|
|
|
_d = [];
|
2010-07-17 22:45:29 +00:00
|
|
|
for (i = 0; (0 <= spaces ? i <= spaces : i >= spaces); (0 <= spaces ? i += 1 : i -= 1)) {
|
2010-02-28 00:46:45 +00:00
|
|
|
_d.push(' ');
|
|
|
|
}
|
|
|
|
return _d;
|
2010-04-10 18:40:05 +00:00
|
|
|
})().join('') : '';
|
2010-06-12 23:05:13 +00:00
|
|
|
letPart = rule.shortFlag ? rule.shortFlag + ', ' : ' ';
|
2010-08-07 12:02:16 +00:00
|
|
|
lines.push(' ' + letPart + rule.longFlag + spaces + rule.description);
|
2010-02-14 20:16:33 +00:00
|
|
|
}
|
2010-03-06 18:59:11 +00:00
|
|
|
return "\n" + (lines.join('\n')) + "\n";
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
|
|
|
return OptionParser;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
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-08-08 03:33:35 +00:00
|
|
|
OPTIONAL = /\[(\w+(\*?))\]/;
|
2010-06-12 23:05:13 +00:00
|
|
|
buildRules = function(rules) {
|
2010-02-26 04:39:14 +00:00
|
|
|
var _a, _b, _c, _d, tuple;
|
2010-03-27 20:04:47 +00:00
|
|
|
_a = []; _c = rules;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
tuple = _c[_b];
|
2010-02-16 23:00:40 +00:00
|
|
|
_a.push((function() {
|
2010-02-14 20:16:33 +00:00
|
|
|
if (tuple.length < 3) {
|
|
|
|
tuple.unshift(null);
|
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
return buildRule.apply(this, tuple);
|
2010-04-10 18:40:05 +00:00
|
|
|
})());
|
2010-02-14 20:16:33 +00:00
|
|
|
}
|
2010-02-16 23:00:40 +00:00
|
|
|
return _a;
|
2010-02-14 20:16:33 +00:00
|
|
|
};
|
Add command-line compiler hooks. To invoke, pass a file after -r and listen for any of these events: 'compile', 'success' and 'exception'. Example:
coffee -e -r ./snarl 'Hello!'
Contents of 'snarl.coffee' in the working directory:
http = require 'http'
CoffeeScript.on 'exception', (err) ->
client = http.createClient 9889, 'localhost'
request = client.request 'GET', '/?d={"action":1,"applicationName":"CoffeeScript","title":' + JSON.stringify(err.message) + ',"description":' + JSON.stringify(err.stack) + ',"priority":3}'
request.end()
err.handled = yes
To examine arguments available for each event (for debugging and getting started), use `puts JSON.stringify arguments`.
See http://nodejs.org/api.html#modules-309 and NODE_PATH for more details on how -r looks for files.
2010-08-07 17:24:37 +00:00
|
|
|
buildRule = function(shortFlag, longFlag, description, options) {
|
2010-02-14 20:16:33 +00:00
|
|
|
var match;
|
2010-06-12 23:05:13 +00:00
|
|
|
match = longFlag.match(OPTIONAL);
|
|
|
|
longFlag = longFlag.match(LONG_FLAG)[1];
|
2010-08-14 22:02:07 +00:00
|
|
|
options || (options = {});
|
2010-02-14 20:16:33 +00:00
|
|
|
return {
|
2010-06-12 23:05:13 +00:00
|
|
|
name: longFlag.substr(2),
|
|
|
|
shortFlag: shortFlag,
|
|
|
|
longFlag: longFlag,
|
2010-02-14 20:16:33 +00:00
|
|
|
description: description,
|
Add command-line compiler hooks. To invoke, pass a file after -r and listen for any of these events: 'compile', 'success' and 'exception'. Example:
coffee -e -r ./snarl 'Hello!'
Contents of 'snarl.coffee' in the working directory:
http = require 'http'
CoffeeScript.on 'exception', (err) ->
client = http.createClient 9889, 'localhost'
request = client.request 'GET', '/?d={"action":1,"applicationName":"CoffeeScript","title":' + JSON.stringify(err.message) + ',"description":' + JSON.stringify(err.stack) + ',"priority":3}'
request.end()
err.handled = yes
To examine arguments available for each event (for debugging and getting started), use `puts JSON.stringify arguments`.
See http://nodejs.org/api.html#modules-309 and NODE_PATH for more details on how -r looks for files.
2010-08-07 17:24:37 +00:00
|
|
|
hasArgument: !!(match && match[1]),
|
2010-08-08 03:33:35 +00:00
|
|
|
isList: !!(match && match[2])
|
2010-02-14 20:16:33 +00:00
|
|
|
};
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
normalizeArguments = function(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 = [];
|
2010-03-27 20:04:47 +00:00
|
|
|
_b = args;
|
|
|
|
for (_a = 0, _c = _b.length; _a < _c; _a++) {
|
|
|
|
arg = _b[_a];
|
2010-08-14 21:52:37 +00:00
|
|
|
if (match = arg.match(MULTI_FLAG)) {
|
2010-03-27 20:04:47 +00:00
|
|
|
_e = match[1].split('');
|
|
|
|
for (_d = 0, _f = _e.length; _d < _f; _d++) {
|
|
|
|
l = _e[_d];
|
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
|
|
|
})();
|