round 2, light refactors.

This commit is contained in:
Jeremy Ashkenas 2010-11-04 23:05:04 -04:00
parent bb05e07e4e
commit f6a8d4c074
5 changed files with 12 additions and 19 deletions

View File

@ -25,7 +25,6 @@
if (options.fileName) { if (options.fileName) {
err.message = "In " + options.fileName + ", " + err.message; err.message = "In " + options.fileName + ", " + err.message;
} }
console.error(err.toString());
throw err; throw err;
} }
}; };

View File

@ -2,14 +2,13 @@
var ALL_SWITCHES, BANNER, CoffeeScript, DEPRECATED_SWITCHES, EventEmitter, SWITCHES, compileOptions, compileScript, compileScripts, compileStdio, exec, fs, helpers, lint, optionParser, optparse, opts, parseOptions, path, printTokens, sources, spawn, usage, version, watch, writeJs, _ref; var ALL_SWITCHES, BANNER, CoffeeScript, DEPRECATED_SWITCHES, EventEmitter, SWITCHES, compileOptions, compileScript, compileScripts, compileStdio, exec, fs, helpers, lint, optionParser, optparse, opts, parseOptions, path, printTokens, sources, spawn, usage, version, watch, writeJs, _ref;
fs = require('fs'); fs = require('fs');
path = require('path'); path = require('path');
helpers = require('./helpers');
optparse = require('./optparse'); optparse = require('./optparse');
CoffeeScript = require('./coffee-script'); CoffeeScript = require('./coffee-script');
helpers = require('./helpers');
_ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec; _ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;
EventEmitter = require('events').EventEmitter; EventEmitter = require('events').EventEmitter;
helpers.extend(CoffeeScript, new EventEmitter); helpers.extend(CoffeeScript, new EventEmitter);
global.CoffeeScript = CoffeeScript; BANNER = 'Usage: coffee [options] path/to/script.coffee';
BANNER = 'coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee';
SWITCHES = [['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-o', '--output [DIR]', 'set the directory for compiled JavaScript'], ['-w', '--watch', 'watch scripts for changes, and recompile'], ['-p', '--print', 'print the compiled JavaScript to stdout'], ['-l', '--lint', 'pipe the compiled JavaScript through JSLint'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-e', '--eval', 'compile a string from the command line'], ['-r', '--require [FILE*]', 'require a library before executing your script'], ['-b', '--bare', 'compile without the top-level function wrapper'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-n', '--nodes', 'print the parse tree that Jison produces'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']]; SWITCHES = [['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-o', '--output [DIR]', 'set the directory for compiled JavaScript'], ['-w', '--watch', 'watch scripts for changes, and recompile'], ['-p', '--print', 'print the compiled JavaScript to stdout'], ['-l', '--lint', 'pipe the compiled JavaScript through JSLint'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-e', '--eval', 'compile a string from the command line'], ['-r', '--require [FILE*]', 'require a library before executing your script'], ['-b', '--bare', 'compile without the top-level function wrapper'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-n', '--nodes', 'print the parse tree that Jison produces'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
DEPRECATED_SWITCHES = [['--no-wrap', 'compile without the top-level function wrapper']]; DEPRECATED_SWITCHES = [['--no-wrap', 'compile without the top-level function wrapper']];
ALL_SWITCHES = SWITCHES.concat(DEPRECATED_SWITCHES); ALL_SWITCHES = SWITCHES.concat(DEPRECATED_SWITCHES);
@ -218,7 +217,7 @@
}; };
}; };
usage = function() { usage = function() {
console.log((new optparse.OptionParser(SWITCHES)).help()); console.log((new optparse.OptionParser(SWITCHES, BANNER)).help());
return process.exit(0); return process.exit(0);
}; };
version = function() { version = function() {

View File

@ -2,8 +2,8 @@
var LONG_FLAG, MULTI_FLAG, OPTIONAL, OptionParser, SHORT_FLAG, buildRule, buildRules, normalizeArguments; var LONG_FLAG, MULTI_FLAG, OPTIONAL, OptionParser, SHORT_FLAG, buildRule, buildRules, normalizeArguments;
exports.OptionParser = (function() { exports.OptionParser = (function() {
OptionParser = (function() { OptionParser = (function() {
function OptionParser(rules, banner) { function OptionParser(rules, _arg) {
this.banner = banner; this.banner = _arg;
this.rules = buildRules(rules); this.rules = buildRules(rules);
return this; return this;
} }

View File

@ -7,22 +7,18 @@
# External dependencies. # External dependencies.
fs = require 'fs' fs = require 'fs'
path = require 'path' path = require 'path'
helpers = require './helpers'
optparse = require './optparse' optparse = require './optparse'
CoffeeScript = require './coffee-script' CoffeeScript = require './coffee-script'
helpers = require './helpers'
{spawn, exec} = require 'child_process' {spawn, exec} = require 'child_process'
{EventEmitter} = require 'events' {EventEmitter} = require 'events'
# Allow CoffeeScript to emit Node.js events, and add it to global scope. # Allow CoffeeScript to emit Node.js events.
helpers.extend CoffeeScript, new EventEmitter helpers.extend CoffeeScript, new EventEmitter
global.CoffeeScript = CoffeeScript
# The help banner that is printed when `coffee` is called without arguments. # The help banner that is printed when `coffee` is called without arguments.
BANNER = ''' BANNER = '''
coffee compiles CoffeeScript source files into JavaScript. Usage: coffee [options] path/to/script.coffee
Usage:
coffee path/to/script.coffee
''' '''
# The list of all the valid option flags that `coffee` knows how to handle. # The list of all the valid option flags that `coffee` knows how to handle.
@ -64,7 +60,7 @@ exports.run = ->
return version() if opts.version return version() if opts.version
return require './repl' if opts.interactive return require './repl' if opts.interactive
return compileStdio() if opts.stdio return compileStdio() if opts.stdio
return compileScript null, sources[0] if opts.eval return compileScript null, sources[0] if opts.eval
return require './repl' unless sources.length return require './repl' unless sources.length
separator = sources.indexOf '--' separator = sources.indexOf '--'
flags = [] flags = []
@ -195,7 +191,7 @@ compileOptions = (fileName) -> {fileName, bare: opts.bare or opts['no-wrap']}
# Print the `--help` usage message and exit. Deprecated switches are not # Print the `--help` usage message and exit. Deprecated switches are not
# shown. # shown.
usage = -> usage = ->
console.log (new optparse.OptionParser SWITCHES).help() console.log (new optparse.OptionParser SWITCHES, BANNER).help()
process.exit 0 process.exit 0
# Print the `--version` message and exit. # Print the `--version` message and exit.

View File

@ -13,9 +13,8 @@ exports.OptionParser = class OptionParser
# [short-flag, long-flag, description] # [short-flag, long-flag, description]
# #
# Along with an an optional banner for the usage help. # Along with an an optional banner for the usage help.
constructor: (rules, banner) -> constructor: (rules, @banner) ->
@banner = banner @rules = buildRules rules
@rules = buildRules rules
# Parse the list of arguments, populating an `options` object with all of the # Parse the list of arguments, populating an `options` object with all of the
# specified options, and returning it. `options.arguments` will be an array # specified options, and returning it. `options.arguments` will be an array