1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

upgrading the optparse library to avoid having to register callbacks for each argument. It just returns a simple options hash.

This commit is contained in:
Jeremy Ashkenas 2010-02-24 17:57:58 -05:00
parent 92cd80226c
commit aba8cb1b08
4 changed files with 37 additions and 107 deletions

View file

@ -1,21 +1,19 @@
(function(){
var LONG_FLAG, OPTIONAL, SHORT_FLAG, build_rule, build_rules, op, spaces;
// Create an OptionParser with a list of valid options.
op = (exports.OptionParser = function OptionParser(rules) {
this.banner = 'Usage: [Options]';
op = (exports.OptionParser = function OptionParser(rules, banner) {
this.banner = banner || 'Usage: [Options]';
this.options_title = 'Available options:';
this.rules = build_rules(rules);
this.actions = {};
return this;
});
// Add a callback to fire when a particular option is encountered.
op.prototype.add = function add(value, callback) {
return this.actions[value] = callback;
};
// Parse the argument array, calling defined callbacks, returning the remaining non-option arguments.
op.prototype.parse = function parse(args) {
var _a, _b, arg, callback, is_option, results, rule, value;
results = [];
var _a, _b, arg, is_option, options, rule;
arguments = Array.prototype.slice.call(arguments, 0);
options = {
arguments: []
};
args = args.concat([]);
while (((arg = args.shift()))) {
is_option = false;
@ -23,20 +21,16 @@
for (_b = 0; _b < _a.length; _b++) {
rule = _a[_b];
if (rule.letter === arg || rule.flag === arg) {
callback = this.actions[rule.name];
value = rule.argument && args.shift();
if (callback) {
callback(value);
}
options[rule.name] = rule.argument ? args.shift() : true;
is_option = true;
break;
}
}
if (!(is_option)) {
results.push(arg);
options.arguments.push(arg);
}
}
return results;
return options;
};
// Return the help text for this OptionParser, for --help and such.
op.prototype.help = function help() {