Class: Rubocop::CLI

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

Overview

The CLI is a class responsible of handling all the command line interface logic.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



14
15
16
17
18
# File 'lib/rubocop/cli.rb', line 14

def initialize
  @errors = []
  @options = {}
  @config_store = ConfigStore.new
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#wants_to_quitObject Also known as: wants_to_quit?

If set true while running, RuboCop will abort processing and exit gracefully.



9
10
11
# File 'lib/rubocop/cli.rb', line 9

def wants_to_quit
  @wants_to_quit
end

Instance Method Details

#display_error_summary(errors) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/cli.rb', line 51

def display_error_summary(errors)
  return if errors.empty?
  plural = errors.count > 1 ? 's' : ''
  warn "\n#{errors.count} error#{plural} occurred:".color(:red)
  errors.each { |error| warn error }
  warn 'Errors are usually caused by RuboCop bugs.'
  warn 'Please, report your problems to RuboCop\'s issue tracker.'
  warn 'Mention the following information in the issue report:'
  warn Rubocop::Version.version(true)
end

#run(args = ARGV) ⇒ Fixnum

Entry point for the application logic. Here we do the command line arguments processing and inspect the target files

Returns:

  • (Fixnum)

    UNIX exit code



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cli.rb', line 24

def run(args = ARGV)
  trap_interrupt

  @options, remaining_args = Options.new.parse(args)
  target_files = target_finder.find(remaining_args)

  act_on_options(remaining_args)

  any_failed = process_files(target_files)

  display_error_summary(@errors)

  !any_failed && !wants_to_quit ? 0 : 1
rescue => e
  $stderr.puts e.message
  return 1
end

#trap_interruptObject



42
43
44
45
46
47
48
49
# File 'lib/rubocop/cli.rb', line 42

def trap_interrupt
  Signal.trap('INT') do
    exit!(1) if wants_to_quit?
    self.wants_to_quit = true
    $stderr.puts
    $stderr.puts 'Exiting... Interrupt again to exit immediately.'
  end
end