lib/scss_lint/cli.rb in scss-lint-0.18.0 vs lib/scss_lint/cli.rb in scss-lint-0.19.0
- old
+ new
@@ -7,22 +7,23 @@
class CLI
attr_reader :config, :options
# Subset of semantic exit codes conforming to `sysexits` documentation.
EXIT_CODES = {
- ok: 0,
- usage: 64, # Command line usage error
- data: 65, # User input was incorrect (i.e. contains lints)
- no_input: 66, # Input file did not exist or was not readable
- software: 70, # Internal software error
- config: 78, # Configuration error
+ ok: 0,
+ usage: 64, # Command line usage error
+ data: 65, # User input was incorrect (i.e. contains lints)
+ no_input: 66, # Input file did not exist or was not readable
+ software: 70, # Internal software error
+ config: 78, # Configuration error
}
+ # @param args [Array]
def initialize(args = [])
- @args = args
+ @args = args
@options = {}
- @config = Config.default
+ @config = Config.default
end
def parse_arguments
begin
options_parser.parse!(@args)
@@ -33,16 +34,17 @@
print_help options_parser.help, ex
end
begin
setup_configuration
- rescue NoSuchLinter => ex
+ rescue InvalidConfiguration, NoSuchLinter => ex
puts ex.message
halt :config
end
end
+ # @return [OptionParser]
def options_parser
@options_parser ||= OptionParser.new do |opts|
opts.banner = "Usage: #{opts.program_name} [options] [scss-files]"
opts.separator ''
@@ -88,10 +90,13 @@
def run
runner = Runner.new(@config)
runner.run(files_to_lint)
report_lints(runner.lints)
halt :data if runner.lints.any?
+ rescue InvalidConfiguration => ex
+ puts ex
+ halt :config
rescue NoFilesError, Errno::ENOENT => ex
puts ex.message
halt :no_input
rescue NoSuchLinter => ex
puts ex.message
@@ -112,10 +117,12 @@
end
merge_command_line_flags_with_config(@config)
end
+ # @param config [Config]
+ # @return [Config]
def merge_command_line_flags_with_config(config)
if @options[:excluded_files]
@options[:excluded_files].each do |file|
config.exclude_file(file)
end
@@ -148,10 +155,11 @@
config.excluded_file?(file)
end
end
+ # @param list [Array]
def extract_files_from(list)
files = []
list.each do |file|
Find.find(file) do |f|
files << f if scssish_file?(f)
@@ -159,24 +167,28 @@
end
files.uniq
end
VALID_EXTENSIONS = %w[.css .scss]
+ # @param file [String]
+ # @return [Boolean]
def scssish_file?(file)
return false unless FileTest.file?(file)
VALID_EXTENSIONS.include?(File.extname(file))
end
+ # @param lints [Array<Lint>]
def report_lints(lints)
sorted_lints = lints.sort_by { |l| [l.filename, l.line] }
reporter = @options.fetch(:reporter, Reporter::DefaultReporter)
.new(sorted_lints)
output = reporter.report_lints
print output if output
end
+ # @param format [String]
def set_output_format(format)
@options[:reporter] = SCSSLint::Reporter.const_get(format + 'Reporter')
rescue NameError
puts "Invalid output format specified: #{format}"
halt :config
@@ -194,21 +206,26 @@
end
halt
end
+ # @param help_message [String]
+ # @param err [Exception]
def print_help(help_message, err = nil)
puts err, '' if err
puts help_message
halt(err ? :usage : :ok)
end
+ # @param program_name [String]
+ # @param version [String]
def print_version(program_name, version)
puts "#{program_name} #{version}"
halt
end
# Used for ease-of testing
+ # @param exit_status [Symbol]
def halt(exit_status = :ok)
exit(EXIT_CODES[exit_status])
end
end
end