From bea40a7a92da38347b59132eb0abcf2647539f0f Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 21 Feb 2010 13:48:38 -0500 Subject: [PATCH] re-enabling the --no-wrap flag, and cleaning up an unused method in command_line --- lib/command_line.js | 34 ++++++++++------------------------ lib/optparse.js | 2 +- src/command_line.coffee | 18 ++++-------------- src/optparse.coffee | 2 +- 4 files changed, 16 insertions(+), 40 deletions(-) diff --git a/lib/command_line.js b/lib/command_line.js index 06cd0754..0f4781a5 100644 --- a/lib/command_line.js +++ b/lib/command_line.js @@ -1,11 +1,11 @@ (function(){ - var BANNER, SWITCHES, coffee, compile, compile_script, compile_scripts, fs, lint, option_parser, options, optparse, parse_options, path, sources, usage, version, watch_scripts, write_js; + var BANNER, SWITCHES, coffee, compile_script, compile_scripts, fs, lint, option_parser, options, optparse, parse_options, path, sources, usage, version, watch_scripts, write_js; fs = require('fs'); path = require('path'); coffee = require('coffee-script'); optparse = require('optparse'); BANNER = "coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee"; - SWITCHES = [['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-r', '--run', 'compile and run a CoffeeScript'], ['-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'], ['-e', '--eval', 'compile a string from the command line'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-tr', '--tree', 'print the parse tree that Jison produces'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']]; + SWITCHES = [['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-r', '--run', 'compile and run a CoffeeScript'], ['-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'], ['-e', '--eval', 'compile a string from the command line'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-tr', '--tree', 'print the parse tree that Jison produces'], ['-n', '--no-wrap', 'compile without the top-level function wrapper'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']]; options = {}; sources = []; option_parser = null; @@ -45,26 +45,6 @@ puts("CoffeeScript version " + coffee.VERSION); return process.exit(0); }; - // Compile a single source file to JavaScript. - compile = function compile(script, source) { - source = source || 'error'; - options = {}; - if (options.no_wrap) { - options.no_wrap = true; - } - if (options.globals) { - options.globals = true; - } - try { - return CoffeeScript.compile(script, options); - } catch (error) { - process.stdio.writeError(source + ': ' + error.toString()); - if (!(options.watch)) { - process.exit(1); - } - return null; - } - }; // Compiles the source CoffeeScript, returning the desired JavaScript, tokens, // or JSLint results. compile_scripts = function compile_scripts() { @@ -80,15 +60,18 @@ // Compile a single source script, containing the given code, according to the // requested options. Both compile_scripts and watch_scripts share this method. compile_script = function compile_script(source, code) { - var js, opts; + var js, o, opts; opts = options; + o = opts.no_wrap ? { + no_wrap: true + } : {}; try { if (opts.tokens) { return coffee.print_tokens(coffee.tokenize(code)); } else if (opts.tree) { return puts(coffee.tree(code).toString()); } else { - js = coffee.compile(code); + js = coffee.compile(code, o); if (opts.run) { return eval(js); } else if (opts.print) { @@ -186,6 +169,9 @@ 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(); diff --git a/lib/optparse.js b/lib/optparse.js index 72118859..911cfe49 100755 --- a/lib/optparse.js +++ b/lib/optparse.js @@ -66,7 +66,7 @@ }; // Private: // Regex matchers for option flags. - LONG_FLAG = /^(--\w+)/; + LONG_FLAG = /^(--[\w\-]+)/; SHORT_FLAG = /^(-\w+)/; OPTIONAL = /\[(.+)\]/; // Build rules from a list of valid switch tuples in the form: diff --git a/src/command_line.coffee b/src/command_line.coffee index 5cae2864..829fbcff 100644 --- a/src/command_line.coffee +++ b/src/command_line.coffee @@ -20,6 +20,7 @@ SWITCHES: [ ['-e', '--eval', 'compile a string from the command line'] ['-t', '--tokens', 'print the tokens that the lexer produces'] ['-tr','--tree', 'print the parse tree that Jison produces'] + ['-n', '--no-wrap', 'compile without the top-level function wrapper'] ['-v', '--version', 'display CoffeeScript version'] ['-h', '--help', 'display this help message'] ] @@ -54,19 +55,6 @@ version: -> puts "CoffeeScript version " + coffee.VERSION process.exit 0 -# Compile a single source file to JavaScript. -compile: (script, source) -> - source ||= 'error' - options: {} - options.no_wrap: true if options.no_wrap - options.globals: true if options.globals - try - CoffeeScript.compile(script, options) - catch error - process.stdio.writeError(source + ': ' + error.toString()) - process.exit 1 unless options.watch - null - # Compiles the source CoffeeScript, returning the desired JavaScript, tokens, # or JSLint results. compile_scripts: -> @@ -79,11 +67,12 @@ compile_scripts: -> # requested options. Both compile_scripts and watch_scripts share this method. compile_script: (source, code) -> opts: options + o: if opts.no_wrap then {no_wrap: true} else {} try if opts.tokens then coffee.print_tokens coffee.tokenize code else if opts.tree then puts coffee.tree(code).toString() else - js: coffee.compile code + js: coffee.compile code, o if opts.run then eval js else if opts.print then puts js else if opts.lint then lint js @@ -131,6 +120,7 @@ parse_options: -> 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() diff --git a/src/optparse.coffee b/src/optparse.coffee index ce481c23..c475f569 100644 --- a/src/optparse.coffee +++ b/src/optparse.coffee @@ -45,7 +45,7 @@ op::help: -> # Private: # Regex matchers for option flags. -LONG_FLAG: /^(--\w+)/ +LONG_FLAG: /^(--[\w\-]+)/ SHORT_FLAG: /^(-\w+)/ OPTIONAL: /\[(.+)\]/