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

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

Instance Attribute Details

#config_storeObject (readonly)

Returns the value of attribute config_store.



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

def config_store
  @config_store
end

#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

#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



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

def run(args = ARGV)
  trap_interrupt

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

  inspector = FileInspector.new(@options)
  any_failed = inspector.process_files(target_files, @config_store) do
    wants_to_quit?
  end
  inspector.display_error_summary

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

#trap_interruptObject



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

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