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