Permalink
Browse files

added jshint options to the command line interface using OptionParser

  • Loading branch information...
1 parent e98d72b commit ec586585fc5d861d4c12b55a894eecde4befc221 @whoward committed Feb 2, 2012
View
@@ -2,15 +2,39 @@
require 'rubygems'
require File.expand_path("../lib/jslint-v8", File.dirname(__FILE__))
-if ARGV.empty?
- puts "usage: #{File.basename(__FILE__)} FILES"
- exit 1
+def print_usage
+ STDERR.puts JSLintV8::OptionParser.new.help
+ exit(255)
end
+# if no parameters are passed then print usage and exit
+print_usage if ARGV.empty?
+
+# parse the options out of the command line
+begin
+ parser = JSLintV8::OptionParser.new
+ parser.parse!
+rescue SystemExit => e
+ # exit with a failing status code, happens with the -h and -v options
+ exit(255)
+rescue Exception => e
+ # print out an error message and exit with a failing exit code
+ STDERR.puts "#{$0}: #{e.message} (#{e.class.to_s})"
+ exit(255)
+end
+
+# if no parameters are left (nothing beyond options given) then print usage and exit
+print_usage if ARGV.empty?
+
+# set up a formatter for standard output
formatter = JSLintV8::Formatter.new(STDOUT)
+# set up a runner
+runner = JSLintV8::Runner.new(ARGV)
+runner.jslint_options.merge!(parser.options[:lint_options])
+
# get a list of all failed files, printing . or * along the way depending on the result
-lint_result = JSLintV8::Runner.new(ARGV).run do |file, errors|
+lint_result = runner.run do |file, errors|
formatter.tick(errors)
end
View
@@ -3,4 +3,6 @@ module JSLintV8 end
require File.expand_path("jslint-v8/runner", File.dirname(__FILE__))
require File.expand_path("jslint-v8/rake_task", File.dirname(__FILE__))
require File.expand_path("jslint-v8/lint_error", File.dirname(__FILE__))
-require File.expand_path("jslint-v8/formatter", File.dirname(__FILE__))
+require File.expand_path("jslint-v8/formatter", File.dirname(__FILE__))
+require File.expand_path("jslint-v8/option_parser", File.dirname(__FILE__))
+require File.expand_path("jslint-v8/version", File.dirname(__FILE__))
@@ -0,0 +1,39 @@
+require 'optparse'
+
+module JSLintV8
+ class OptionParser < ::OptionParser
+
+ attr_reader :options
+
+ def initialize
+ super
+
+ @options = {}
+ @options[:lint_options] = {}
+
+ self.version = JSLintV8::Version::STRING
+ self.banner = "#{self.banner} <filepattern>..."
+
+ Runner::DefaultOptions.keys.each do |option|
+ default = Runner::DefaultOptions[option] ? "on" : "off"
+
+ long = "--[no-]#{option}"
+ desc = "#{Runner::OptionDescriptions[option]} (default=#{default})"
+
+ on(long, desc) do |value|
+ options[:lint_options][option] = value
+ end
+ end
+
+ on("-h", "--help", "Show this message") do
+ STDERR.puts self.help
+ exit(-1)
+ end
+
+ on("-v", "--version", "Show version") do
+ STDERR.puts "#{self.program_name} version #{JSLintV8::Version::STRING}"
+ exit(-1)
+ end
+ end
+ end
+end
@@ -0,0 +1,47 @@
+Usage: jslint-v8 [options] <filepattern>...
+ --[no-]asi if automatic semicolon insertion should be tolerated (default=off)
+ --[no-]bitwise if bitwise operators should not be allowed (default=on)
+ --[no-]boss if advanced usage of assignments should be allowed (default=off)
+ --[no-]browser if the standard browser globals should be predefined (default=off)
+ --[no-]couch if CouchDB globals should be predefined (default=off)
+ --[no-]curly if curly braces around blocks should be required (even in if/for/while) (default=off)
+ --[no-]debug if debugger statements should be allowed (default=off)
+ --[no-]devel if logging globals should be predefined (console, alert, etc.) (default=off)
+ --[no-]dojo if Dojo Toolkit globals should be predefined (default=off)
+ --[no-]eqeqeq if === should be required (default=on)
+ --[no-]eqnull if == null comparisons should be tolerated (default=off)
+ --[no-]es5 if ES5 syntax should be allowed (default=off)
+ --[no-]evil if eval should be allowed (default=off)
+ --[no-]expr if ExpressionStatement should be allowed as Programs (default=off)
+ --[no-]forin if for in statements must filter (default=off)
+ --[no-]globalstrict if global "use strict"; should be allowed (also enables 'strict') (default=off)
+ --[no-]immed if immediate invocations must be wrapped in parens (default=on)
+ --[no-]jquery if jQuery globals should be predefined (default=off)
+ --[no-]latedef if the use before definition should not be tolerated (default=off)
+ --[no-]laxbreak if line breaks should not be checked (default=off)
+ --[no-]loopfunc if functions should be allowed to be defined within loops (default=off)
+ --[no-]mootools if MooTools globals should be predefined (default=off)
+ --[no-]newcap if constructor names must be capitalized (default=on)
+ --[no-]noarg if arguments.caller and arguments.callee should be disallowed (default=off)
+ --[no-]node if the Node.js environment globals should be predefined (default=off)
+ --[no-]noempty if empty blocks should be disallowed (default=off)
+ --[no-]nonew if using `new` for side-effects should be disallowed (default=off)
+ --[no-]nomen if names should be checked (default=on)
+ --[no-]onevar if only one var statement per function should be allowed (default=on)
+ --[no-]passfail if the scan should stop on first error (default=off)
+ --[no-]plusplus if increment/decrement should not be allowed (default=on)
+ --[no-]prototypejs if Prototype and Scriptaculous globals should be predefined (default=off)
+ --[no-]regexdash if unescaped last dash (-) inside brackets should be tolerated (default=off)
+ --[no-]regexp if the . should not be allowed in regexp literals (default=on)
+ --[no-]rhino if the Rhino environment globals should be predefined (default=off)
+ --[no-]undef if variables should be declared before used (default=on)
+ --[no-]scripturl if script-targeted URLs should be tolerated (default=off)
+ --[no-]shadow if variable shadowing should be tolerated (default=off)
+ --[no-]strict require the "use strict"; pragma (default=off)
+ --[no-]sub if all forms of subscript notation are tolerated (default=off)
+ --[no-]supernew if `new function () { ... };` and `new Object;` should be tolerated (default=off)
+ --[no-]trailing if trailing whitespace rules apply (default=off)
+ --[no-]white if strict whitespace rules apply (default=off)
+ --[no-]wsh if the Windows Scripting Host environment globals should be predefined (default=off)
+ -h, --help Show this message
+ -v, --version Show version
@@ -0,0 +1 @@
+jslint-v8 version <%= JSLintV8::Version::STRING %>
View
@@ -9,10 +9,35 @@ def test_executable_exists
end
def test_empty_args
- result = `#{Executable}`
+ # capture standard error by redirecting it to standard out
+ result = `#{Executable} 2>&1`
- assert_equal "usage: jslint-v8 FILES\n", result
- assert_equal false, $?.success?
+ assert_equal erb_fixture("usage-output"), result
+ assert_equal 255, $?.exitstatus
+ end
+
+ def test_version_output
+ # capture standard error by redirecting it to standard out
+ result = `#{Executable} -v 2>&1`
+
+ assert_equal erb_fixture("version-output"), result
+ assert_equal 255, $?.exitstatus
+ end
+
+ def test_help_output
+ # capture standard error by redirecting it to standard out
+ result = `#{Executable} -h foo.js bar.js baz.js 2>&1`
+
+ assert_equal erb_fixture("usage-output"), result
+ assert_equal 255, $?.exitstatus
+ end
+
+ def test_only_options_given
+ # capture standard error by redirecting it to standard out
+ result = `#{Executable} --browser --jquery 2>&1`
+
+ assert_equal erb_fixture("usage-output"), result
+ assert_equal 255, $?.exitstatus
end
def test_valid

0 comments on commit ec58658

Please sign in to comment.