Class: SCSSLint::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/scss_lint/cli.rb

Overview

Responsible for parsing command-line options and executing the appropriate application logic based on the options specified.

Constant Summary collapse

EXIT_CODES =

Subset of semantic exit codes conforming to ‘sysexits` documentation.

{
  ok:        0,
  warning:   1,  # One or more warnings (but no errors) were reported
  error:     2,  # One or more errors were reported
  usage:     64, # Command line usage error
  no_input:  66, # Input file did not exist or was not readable
  software:  70, # Internal software error
  config:    78, # Configuration error
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ CLI



24
25
26
27
28
# File 'lib/scss_lint/cli.rb', line 24

def initialize(args = [])
  @args    = args
  @options = {}
  @config  = Config.default
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/scss_lint/cli.rb', line 10

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/scss_lint/cli.rb', line 10

def options
  @options
end

Instance Method Details

#options_parserOptionParser



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/scss_lint/cli.rb', line 49

def options_parser # rubocop:disable MethodLength
  @options_parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{opts.program_name} [options] [scss-files]"

    opts.separator ''
    opts.separator 'Common options:'

    opts.on('-c', '--config file', 'Specify configuration file', String) do |file|
      @options[:config_file] = file
    end

    opts.on('-e', '--exclude file,...', Array,
            'List of file names to exclude') do |files|
      @options[:excluded_files] = files
    end

    opts.on('-f', '--format Formatter', 'Specify how to display lints', String) do |format|
      define_output_format(format)
    end

    opts.on_tail('--show-formatters', 'Shows available formatters') do
      print_formatters
    end

    opts.on('-i', '--include-linter linter,...', Array,
            'Specify which linters you want to run') do |linters|
      @options[:included_linters] = linters
    end

    opts.on('-x', '--exclude-linter linter,...', Array,
            "Specify which linters you don't want to run") do |linters|
      @options[:excluded_linters] = linters
    end

    opts.on_tail('--show-linters', 'Shows available linters') do
      print_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
  end
end

#parse_argumentsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/scss_lint/cli.rb', line 30

def parse_arguments
  begin
    options_parser.parse!(@args)

    # Take the rest of the arguments as files/directories
    @options[:files] = @args
  rescue OptionParser::InvalidOption => ex
    print_help options_parser.help, ex
  end

  begin
    setup_configuration
  rescue InvalidConfiguration, NoSuchLinter => ex
    puts ex.message
    halt :config
  end
end

#runObject

rubocop:disable MethodLength



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/scss_lint/cli.rb', line 97

def run # rubocop:disable MethodLength
  runner = Runner.new(@config)
  runner.run(files_to_lint)
  report_lints(runner.lints)

  if runner.lints.any? { |lint| lint.error? }
    halt :error
  elsif runner.lints.any?
    halt :warning
  end
rescue InvalidConfiguration => ex
  puts ex
  halt :config
rescue NoFilesError, Errno::ENOENT => ex
  puts ex.message
  halt :no_input
rescue NoSuchLinter => ex
  puts ex.message
  halt :usage
rescue => ex
  puts ex.message
  puts ex.backtrace
  puts 'Report this bug at '.color(:yellow) + BUG_REPORT_URL.color(:cyan)
  halt :software
end