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,
  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
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ CLI

Returns a new instance of CLI

Parameters:

  • args (Array) (defaults to: [])

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

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

Returns:

  • (OptionParser)

48
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
# File 'lib/scss_lint/cli.rb', line 48

def options_parser
  @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|
      set_output_format(format)
    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


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

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


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/scss_lint/cli.rb', line 92

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
  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