lib/scss_lint/cli.rb in scss-lint-0.5.2 vs lib/scss_lint/cli.rb in scss-lint-0.6

- old
+ new

@@ -1,37 +1,95 @@ require 'scss_lint' -require 'colorize' require 'optparse' module SCSSLint class CLI - def initialize(args) - opts = OptionParser.new do |opts| - opts.banner = 'Usage: scss-lint [scss-files]' - end.parse!(args) + attr_accessor :options - files = SCSSLint.extract_files_from(opts) + def initialize(args = []) + @args = args + @options = {} + end - runner = Runner.new + def parse_arguments + parser = OptionParser.new do |opts| + opts.banner = "Usage: #{opts.program_name} [options] [scss-files]" + + opts.separator '' + opts.separator 'Common options:' + + opts.on('-e', '--exclude file,...', Array, + 'List of file names to exclude') do |files| + options[:excluded_files] = files + end + + opts.on('-i', '--ignore-linter linter,...', Array, + "Specify which linters you don't want to run") do |linters| + options[:ignored_linters] = linters + end + + opts.on_tail('-h', '--help', 'Show this message') do + print_help opts.help + end + + opts.on_tail('-v', '--version', 'Show version') do + print_version opts.program_name, VERSION + end + + opts.on('-x', '--xml', 'Output the results in XML format') do + options[:reporter] = SCSSLint::Reporter::XMLReporter + end + end + begin - runner.run files - report_lints(runner.lints) - exit 1 if runner.lints? - rescue NoFilesError => ex - puts ex.message - exit -1 + parser.parse!(@args) + + # Take the rest of the arguments as files/directories + options[:files] = @args + rescue OptionParser::InvalidOption => ex + print_help parser.help, ex end end - def report_lints(lints) - lints.sort_by { |l| [l.filename, l.line] }.each do |lint| - if lint.filename - print "#{lint.filename}:".yellow - else - print 'line'.yellow - end + def run + runner = Runner.new(options) + runner.run(find_files) + report_lints(runner.lints) + halt 1 if runner.lints? + rescue NoFilesError, NoSuchLinter => ex + puts ex.message + halt -1 + end - puts "#{lint.line} - #{lint.description}" + private + + def find_files + excluded_files = options.fetch(:excluded_files, []) + + SCSSLint.extract_files_from(options[:files]).reject do |file| + excluded_files.include?(file) end + end + + def report_lints(lints) + sorted_lints = lints.sort_by { |l| [l.filename, l.line] } + reporter = options.fetch(:reporter, Reporter::DefaultReporter).new(sorted_lints) + puts reporter.report_lints + end + + def print_help(help_message, err = nil) + puts err, '' if err + puts help_message + halt + end + + def print_version(program_name, version) + puts "#{program_name} #{version}" + halt + end + + # Used for ease-of testing + def halt(exit_status = 0) + exit exit_status end end end