2012-01-10 12:55:41 -05:00
|
|
|
// Generated by CoffeeScript 1.2.1-pre
|
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;
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-12-23 13:50:52 -05:00
|
|
|
exports.OptionParser = OptionParser = (function() {
|
2011-12-24 06:49:21 -05:00
|
|
|
|
2011-12-18 20:00:09 -05:00
|
|
|
OptionParser.name = 'OptionParser';
|
2011-12-24 06:46:09 -05:00
|
|
|
|
2010-11-20 20:39:35 -05:00
|
|
|
function OptionParser(rules, banner) {
|
|
|
|
this.banner = banner;
|
2010-11-11 21:48:08 -05:00
|
|
|
this.rules = buildRules(rules);
|
|
|
|
}
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-05-14 23:40:04 -04:00
|
|
|
OptionParser.prototype.parse = function(args) {
|
2012-02-29 23:46:03 -05:00
|
|
|
var arg, i, isOption, matchedRule, options, originalArgs, pos, rule, seenNonOptionArg, skippingArgument, value, _i, _j, _len, _len1, _ref;
|
2010-02-27 19:46:45 -05:00
|
|
|
options = {
|
2012-01-25 19:47:03 -05:00
|
|
|
"arguments": []
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-12-21 14:06:34 -05:00
|
|
|
skippingArgument = false;
|
2011-10-06 04:11:41 -04:00
|
|
|
originalArgs = args;
|
2010-06-12 19:05:13 -04:00
|
|
|
args = normalizeArguments(args);
|
2011-12-21 14:06:34 -05:00
|
|
|
for (i = _i = 0, _len = args.length; _i < _len; i = ++_i) {
|
2010-10-01 18:26:37 -04:00
|
|
|
arg = args[i];
|
2011-12-21 14:06:34 -05:00
|
|
|
if (skippingArgument) {
|
|
|
|
skippingArgument = false;
|
|
|
|
continue;
|
|
|
|
}
|
2010-12-18 09:29:04 -05:00
|
|
|
if (arg === '--') {
|
2011-10-06 14:51:27 -04:00
|
|
|
pos = originalArgs.indexOf('--');
|
2012-01-25 19:47:03 -05:00
|
|
|
options["arguments"] = options["arguments"].concat(originalArgs.slice(pos + 1));
|
2010-12-18 09:29:04 -05:00
|
|
|
break;
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
2012-01-25 19:47:03 -05:00
|
|
|
seenNonOptionArg = options["arguments"].length > 0;
|
|
|
|
if (!seenNonOptionArg) {
|
|
|
|
matchedRule = false;
|
|
|
|
_ref = this.rules;
|
2012-02-29 23:46:03 -05:00
|
|
|
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
|
2012-01-25 19:47:03 -05:00
|
|
|
rule = _ref[_j];
|
|
|
|
if (rule.shortFlag === arg || rule.longFlag === arg) {
|
|
|
|
value = true;
|
|
|
|
if (rule.hasArgument) {
|
|
|
|
skippingArgument = true;
|
|
|
|
value = args[i + 1];
|
|
|
|
}
|
|
|
|
options[rule.name] = rule.isList ? (options[rule.name] || []).concat(value) : value;
|
|
|
|
matchedRule = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isOption && !matchedRule) {
|
|
|
|
throw new Error("unrecognized option: " + arg);
|
2010-02-27 19:46:45 -05:00
|
|
|
}
|
|
|
|
}
|
2012-01-25 19:47:03 -05:00
|
|
|
if (seenNonOptionArg || !isOption) options["arguments"].push(arg);
|
2010-02-14 15:16:33 -05:00
|
|
|
}
|
2010-02-27 19:46:45 -05:00
|
|
|
return options;
|
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-05-14 23:40:04 -04:00
|
|
|
OptionParser.prototype.help = function() {
|
2010-11-01 23:58:03 -04:00
|
|
|
var letPart, lines, rule, spaces, _i, _len, _ref;
|
2011-01-15 11:04:50 -05:00
|
|
|
lines = [];
|
2011-08-07 02:59:37 -04:00
|
|
|
if (this.banner) lines.unshift("" + this.banner + "\n");
|
2010-10-21 21:51:06 -04:00
|
|
|
_ref = this.rules;
|
2011-12-21 14:37:38 -05:00
|
|
|
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
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-02-27 19:46:45 -05:00
|
|
|
return OptionParser;
|
2011-12-14 10:39:20 -05:00
|
|
|
|
2010-12-23 13:50:52 -05:00
|
|
|
})();
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2012-01-25 19:47:03 -05:00
|
|
|
LONG_FLAG = /^(--\w[\w\-]*)/;
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2012-01-25 19:47:03 -05:00
|
|
|
SHORT_FLAG = /^(-\w)$/;
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-02-25 19:06:08 -05:00
|
|
|
MULTI_FLAG = /^-(\w{2,})/;
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-08-07 23:33:35 -04:00
|
|
|
OPTIONAL = /\[(\w+(\*?))\]/;
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
buildRules = function(rules) {
|
2010-11-09 00:17:08 -05:00
|
|
|
var tuple, _i, _len, _results;
|
|
|
|
_results = [];
|
2011-12-21 14:37:38 -05:00
|
|
|
for (_i = 0, _len = rules.length; _i < _len; _i++) {
|
2010-10-01 18:26:37 -04:00
|
|
|
tuple = rules[_i];
|
2011-08-07 02:59:37 -04:00
|
|
|
if (tuple.length < 3) tuple.unshift(null);
|
2010-11-26 02:08:25 -05:00
|
|
|
_results.push(buildRule.apply(null, tuple));
|
2010-02-14 15:16:33 -05:00
|
|
|
}
|
2010-11-09 00:17:08 -05:00
|
|
|
return _results;
|
2010-02-14 15:16:33 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04: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;
|
2011-08-07 02:59:37 -04:00
|
|
|
if (options == null) options = {};
|
2010-06-12 19:05:13 -04:00
|
|
|
match = longFlag.match(OPTIONAL);
|
|
|
|
longFlag = longFlag.match(LONG_FLAG)[1];
|
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
|
|
|
};
|
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
normalizeArguments = function(args) {
|
2012-02-29 23:46:03 -05:00
|
|
|
var arg, l, match, result, _i, _j, _len, _len1, _ref;
|
2010-02-25 19:06:08 -05:00
|
|
|
args = args.slice(0);
|
|
|
|
result = [];
|
2011-12-21 14:37:38 -05: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('');
|
2012-02-29 23:46:03 -05:00
|
|
|
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
|
2010-11-28 12:28:46 -05:00
|
|
|
l = _ref[_j];
|
2010-02-25 19:06:08 -05:00
|
|
|
result.push('-' + l);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2011-12-14 10:39:20 -05:00
|
|
|
|
2010-09-21 03:53:58 -04:00
|
|
|
}).call(this);
|