class RuboCop::CLI

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

Attributes

config_store[R]
options[R]

Public Class Methods

new() click to toggle source
# File lib/rubocop/cli.rb, line 12
def initialize
  @options = {}
  @config_store = ConfigStore.new
end

Public Instance Methods

run(args = ARGV) click to toggle source

@api public

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

@param args [Array<String>] command line arguments @return [Integer] UNIX exit code

# File lib/rubocop/cli.rb, line 25
def run(args = ARGV)
  @options, paths = Options.new.parse(args)
  validate_options_vs_config
  act_on_options
  apply_default_formatter

  execute_runner(paths)
rescue RuboCop::Error => e
  warn Rainbow("Error: #{e.message}").red
  return 2
rescue Finished
  return 0
rescue StandardError, SyntaxError, LoadError => e
  warn e.message
  warn e.backtrace
  return 2
end
trap_interrupt(runner) click to toggle source
# File lib/rubocop/cli.rb, line 43
def trap_interrupt(runner)
  Signal.trap('INT') do
    exit!(1) if runner.aborting?
    runner.abort
    warn
    warn 'Exiting... Interrupt again to exit immediately.'
  end
end

Private Instance Methods

act_on_options() click to toggle source
# File lib/rubocop/cli.rb, line 63
def act_on_options
  handle_exiting_options

  ConfigLoader.debug = @options[:debug]
  ConfigLoader.auto_gen_config = @options[:auto_gen_config]
  ConfigLoader.ignore_parent_exclusion = @options[:ignore_parent_exclusion]

  @config_store.options_config = @options[:config] if @options[:config]
  @config_store.force_default_config! if @options[:force_default_config]

  if @options[:color]
    # color output explicitly forced on
    Rainbow.enabled = true
  elsif @options[:color] == false
    # color output explicitly forced off
    Rainbow.enabled = false
  end
end
apply_default_formatter() click to toggle source
# File lib/rubocop/cli.rb, line 103
def apply_default_formatter
  # This must be done after the options have already been processed,
  # because they can affect how ConfigStore behaves
  @options[:formatters] ||= begin
    cfg = @config_store.for(Dir.pwd).for_all_cops
    formatter = cfg['DefaultFormatter'] || 'progress'
    [[formatter, @options[:output_path]]]
  end

  return unless @options[:auto_gen_config]

  @options[:formatters] << [Formatter::DisabledConfigFormatter,
                            ConfigLoader::AUTO_GENERATED_FILE]
end
config_lines(cop) click to toggle source
# File lib/rubocop/cli.rb, line 164
def config_lines(cop)
  cnf = @config_store.for(Dir.pwd).for_cop(cop)
  cnf.to_yaml.lines.to_a.butfirst.map { |line| '  ' + line }
end
cops_of_department(cops, department) click to toggle source
# File lib/rubocop/cli.rb, line 160
def cops_of_department(cops, department)
  cops.with_department(department).sort!
end
display_error_summary(errors) click to toggle source
# File lib/rubocop/cli.rb, line 177
    def display_error_summary(errors)
      return if errors.empty?

      warn Rainbow("\n#{pluralize(errors.size, 'error')} occurred:").red

      errors.each { |error| warn error }

      warn <<-WARNING.strip_indent
        Errors are usually caused by RuboCop bugs.
        Please, report your problems to RuboCop's issue tracker.
        Mention the following information in the issue report:
        #{RuboCop::Version.version(true)}
      WARNING
    end
display_warning_summary(warnings) click to toggle source
# File lib/rubocop/cli.rb, line 169
def display_warning_summary(warnings)
  return if warnings.empty?

  warn Rainbow("\n#{pluralize(warnings.size, 'warning')}:").yellow

  warnings.each { |warning| warn warning }
end
execute_runner(paths) click to toggle source
# File lib/rubocop/cli.rb, line 82
def execute_runner(paths)
  runner = Runner.new(@options, @config_store)

  trap_interrupt(runner)
  all_passed = runner.run(paths)
  display_warning_summary(runner.warnings)
  display_error_summary(runner.errors)
  maybe_print_corrected_source

  all_passed && !runner.aborting? && runner.errors.empty? ? 0 : 1
end
handle_exiting_options() click to toggle source
# File lib/rubocop/cli.rb, line 94
def handle_exiting_options
  return unless Options::EXITING_OPTIONS.any? { |o| @options.key? o }

  puts RuboCop::Version.version(false) if @options[:version]
  puts RuboCop::Version.version(true) if @options[:verbose_version]
  print_available_cops if @options[:show_cops]
  raise Finished
end
maybe_print_corrected_source() click to toggle source
# File lib/rubocop/cli.rb, line 192
def maybe_print_corrected_source
  # If we are asked to autocorrect source code read from stdin, the only
  # reasonable place to write it is to stdout
  # Unfortunately, we also write other information to stdout
  # So a delimiter is needed for tools to easily identify where the
  # autocorrected source begins
  return unless @options[:stdin] && @options[:auto_correct]
  puts '=' * 20
  print @options[:stdin]
end
print_available_cops() click to toggle source
print_cop_details(cops) click to toggle source
print_cops_of_department(registry, department, show_all) click to toggle source
selected_cops_of_department(cops, department) click to toggle source
# File lib/rubocop/cli.rb, line 154
def selected_cops_of_department(cops, department)
  cops_of_department(cops, department).select do |cop|
    @options[:show_cops].include?(cop.cop_name)
  end
end
validate_options_vs_config() click to toggle source
# File lib/rubocop/cli.rb, line 54
def validate_options_vs_config
  if @options[:parallel] &&
     !@config_store.for(Dir.pwd).for_all_cops['UseCache']
    raise ArgumentError, '-P/--parallel uses caching to speed up ' \
                         'execution, so combining with AllCops: ' \
                         'UseCache: false is not allowed.'
  end
end