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

@ -13,14 +13,20 @@
exports.run = function run() { exports.run = function run() {
var flags, separator; var flags, separator;
parse_options(); parse_options();
if (options.help) {
return usage();
}
if (options.version) {
return version();
}
if (options.interactive) { if (options.interactive) {
return require('repl'); return require('repl');
} }
if (options.eval) { if (options.eval) {
return compile_script('terminal', sources[0]); return compile_script('unknown', sources[0]);
} }
if (!(sources.length)) { if (!(sources.length)) {
usage(); return usage();
} }
separator = sources.indexOf('--'); separator = sources.indexOf('--');
flags = []; flags = [];
@ -66,7 +72,7 @@
compile_script = function compile_script(source, code) { compile_script = function compile_script(source, code) {
var js, o, opts; var js, o, opts;
opts = options; opts = options;
o = opts.no_wrap ? { o = opts['no-wrap'] ? {
no_wrap: true no_wrap: true
} : {}; } : {};
try { try {
@ -145,57 +151,8 @@
}; };
// Use OptionParser for all the options. // Use OptionParser for all the options.
parse_options = function parse_options() { parse_options = function parse_options() {
var oparser, opts, paths; option_parser = new optparse.OptionParser(SWITCHES, BANNER);
opts = (options = {}); options = option_parser.parse(process.ARGV);
oparser = (option_parser = new optparse.OptionParser(SWITCHES)); return sources = options.arguments.slice(2, options.arguments.length);
oparser.banner = BANNER;
oparser.add('interactive', function() {
return opts.interactive = true;
});
oparser.add('run', function() {
return opts.run = true;
});
oparser.add('output', function(dir) {
return opts.output = dir;
});
oparser.add('watch', function() {
return opts.watch = true;
});
oparser.add('print', function() {
return opts.print = true;
});
oparser.add('lint', function() {
return opts.lint = true;
});
oparser.add('eval', function() {
return opts.eval = true;
});
oparser.add('tokens', function() {
return opts.tokens = true;
});
oparser.add('tree', function() {
return opts.tree = true;
});
oparser.add('no-wrap', function() {
return opts.no_wrap = true;
});
oparser.add('help', (function(__this) {
var __func = function() {
return usage();
};
return (function() {
return __func.apply(__this, arguments);
});
})(this));
oparser.add('version', (function(__this) {
var __func = function() {
return version();
};
return (function() {
return __func.apply(__this, arguments);
});
})(this));
paths = oparser.parse(process.ARGV);
return sources = paths.slice(2, paths.length);
}; };
})(); })();

View file

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

View file

@ -32,9 +32,11 @@ option_parser: null
# The CommandLine handles all of the functionality of the `coffee` utility. # The CommandLine handles all of the functionality of the `coffee` utility.
exports.run: -> exports.run: ->
parse_options() parse_options()
return require 'repl' if options.interactive return usage() if options.help
return compile_script 'terminal', sources[0] if options.eval return version() if options.version
usage() unless sources.length return require 'repl' if options.interactive
return compile_script 'unknown', sources[0] if options.eval
return usage() unless sources.length
separator: sources.indexOf '--' separator: sources.indexOf '--'
flags: [] flags: []
if separator >= 0 if separator >= 0
@ -67,7 +69,7 @@ compile_scripts: ->
# requested options. Both compile_scripts and watch_scripts share this method. # requested options. Both compile_scripts and watch_scripts share this method.
compile_script: (source, code) -> compile_script: (source, code) ->
opts: options opts: options
o: if opts.no_wrap then {no_wrap: true} else {} o: if opts['no-wrap'] then {no_wrap: true} else {}
try try
if opts.tokens then coffee.print_tokens coffee.tokenize code if opts.tokens then coffee.print_tokens coffee.tokenize code
else if opts.tree then puts coffee.tree(code).toString() else if opts.tree then puts coffee.tree(code).toString()
@ -108,23 +110,7 @@ lint: (js) ->
# Use OptionParser for all the options. # Use OptionParser for all the options.
parse_options: -> parse_options: ->
opts: options: {} option_parser: new optparse.OptionParser SWITCHES, BANNER
oparser: option_parser: new optparse.OptionParser SWITCHES options: option_parser.parse(process.ARGV)
oparser.banner: BANNER sources: options.arguments[2...options.arguments.length]
oparser.add 'interactive', -> opts.interactive: true
oparser.add 'run', -> opts.run: true
oparser.add 'output', (dir) -> opts.output: dir
oparser.add 'watch', -> opts.watch: true
oparser.add 'print', -> opts.print: true
oparser.add 'lint', -> opts.lint: true
oparser.add 'eval', -> opts.eval: true
oparser.add 'tokens', -> opts.tokens: true
oparser.add 'tree', -> opts.tree: true
oparser.add 'no-wrap', -> opts.no_wrap: true
oparser.add 'help', => usage()
oparser.add 'version', => version()
paths: oparser.parse(process.ARGV)
sources: paths[2...paths.length]

View file

@ -1,30 +1,23 @@
# Create an OptionParser with a list of valid options. # Create an OptionParser with a list of valid options.
op: exports.OptionParser: (rules) -> op: exports.OptionParser: (rules, banner) ->
@banner: 'Usage: [Options]' @banner: banner or 'Usage: [Options]'
@options_title: 'Available options:' @options_title: 'Available options:'
@rules: build_rules(rules) @rules: build_rules(rules)
@actions: {}
this this
# Add a callback to fire when a particular option is encountered.
op::add: (value, callback) ->
@actions[value]: callback
# Parse the argument array, calling defined callbacks, returning the remaining non-option arguments. # Parse the argument array, calling defined callbacks, returning the remaining non-option arguments.
op::parse: (args) -> op::parse: (args) ->
results: [] options: {arguments: []}
args: args.concat [] args: args.concat []
while (arg: args.shift()) while (arg: args.shift())
is_option: false is_option: false
for rule in @rules for rule in @rules
if rule.letter is arg or rule.flag is arg if rule.letter is arg or rule.flag is arg
callback: @actions[rule.name] options[rule.name]: if rule.argument then args.shift() else true
value: rule.argument and args.shift()
callback(value) if callback
is_option: true is_option: true
break break
results.push arg unless is_option options.arguments.push arg unless is_option
results options
# Return the help text for this OptionParser, for --help and such. # Return the help text for this OptionParser, for --help and such.
op::help: -> op::help: ->