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-11-12 02:48:08 +00:00
|
|
|
exports.OptionParser = OptionParser = function() {
|
2010-11-21 01:39:35 +00:00
|
|
|
function OptionParser(rules, banner) {
|
|
|
|
this.banner = banner;
|
2010-11-12 02:48:08 +00:00
|
|
|
this.rules = buildRules(rules);
|
|
|
|
}
|
2010-05-15 03:40:04 +00:00
|
|
|
OptionParser.prototype.parse = function(args) {
|
2010-11-02 03:58:03 +00:00
|
|
|
var arg, i, isOption, matchedRule, options, rule, value, _i, _len, _len2, _ref;
|
2010-02-28 00:46:45 +00:00
|
|
|
options = {
|
|
|
|
arguments: []
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
args = normalizeArguments(args);
|
2010-10-22 01:51:06 +00:00
|
|
|
for (i = 0, _len = args.length; i < _len; i++) {
|
2010-10-01 22:26:37 +00:00
|
|
|
arg = args[i];
|
2010-06-12 23:05:13 +00:00
|
|
|
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
|
|
|
matchedRule = false;
|
2010-10-22 01:51:06 +00:00
|
|
|
_ref = this.rules;
|
|
|
|
for (_i = 0, _len2 = _ref.length; _i < _len2; _i++) {
|
2010-10-19 15:07:10 +00:00
|
|
|
rule = _ref[_i];
|
2010-06-12 23:05:13 +00:00
|
|
|
if (rule.shortFlag === arg || rule.longFlag === arg) {
|
2010-10-20 21:00:52 +00:00
|
|
|
value = rule.hasArgument ? args[i += 1] : true;
|
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
|
|
|
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-10-04 12:50:50 +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) {
|
2010-10-12 20:47:45 +00:00
|
|
|
options.arguments = args.slice(i);
|
2010-07-11 13:57:42 +00:00
|
|
|
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-11-02 03:58:03 +00:00
|
|
|
var letPart, lines, rule, spaces, _i, _len, _ref;
|
2010-02-28 00:46:45 +00:00
|
|
|
lines = ['Available options:'];
|
|
|
|
if (this.banner) {
|
2010-10-21 23:50:36 +00:00
|
|
|
lines.unshift("" + this.banner + "\n");
|
2010-02-25 23:54:08 +00:00
|
|
|
}
|
2010-10-22 01:51:06 +00:00
|
|
|
_ref = this.rules;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
rule = _ref[_i];
|
2010-06-12 23:05:13 +00:00
|
|
|
spaces = 15 - rule.longFlag.length;
|
2010-09-22 03:17:43 +00:00
|
|
|
spaces = spaces > 0 ? Array(spaces + 1).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-11-12 02:48:08 +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-11-09 05:17:08 +00:00
|
|
|
var tuple, _i, _len, _results;
|
|
|
|
_results = [];
|
2010-10-22 01:51:06 +00:00
|
|
|
for (_i = 0, _len = rules.length; _i < _len; _i++) {
|
2010-10-01 22:26:37 +00:00
|
|
|
tuple = rules[_i];
|
2010-10-24 05:36:23 +00:00
|
|
|
if (tuple.length < 3) {
|
|
|
|
tuple.unshift(null);
|
|
|
|
}
|
2010-11-09 05:17:08 +00:00
|
|
|
_results.push(buildRule.apply(buildRule, tuple));
|
2010-02-14 20:16:33 +00:00
|
|
|
}
|
2010-11-09 05:17:08 +00:00
|
|
|
return _results;
|
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-11-02 03:11:25 +00:00
|
|
|
options == null && (options = {});
|
2010-06-12 23:05:13 +00:00
|
|
|
match = longFlag.match(OPTIONAL);
|
|
|
|
longFlag = longFlag.match(LONG_FLAG)[1];
|
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-11-02 03:58:03 +00:00
|
|
|
var arg, l, match, result, _i, _j, _len, _len2, _ref;
|
2010-02-26 00:06:08 +00:00
|
|
|
args = args.slice(0);
|
|
|
|
result = [];
|
2010-10-22 01:51:06 +00:00
|
|
|
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
2010-10-01 22:26:37 +00:00
|
|
|
arg = args[_i];
|
2010-08-14 21:52:37 +00:00
|
|
|
if (match = arg.match(MULTI_FLAG)) {
|
2010-10-22 01:51:06 +00:00
|
|
|
_ref = match[1].split('');
|
|
|
|
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
|
2010-10-19 15:07:10 +00:00
|
|
|
l = _ref[_j];
|
2010-02-26 00:06:08 +00:00
|
|
|
result.push('-' + l);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2010-09-21 07:53:58 +00:00
|
|
|
}).call(this);
|