Class: RuboCop::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cli.rb,
lib/rubocop/cli/command.rb,
lib/rubocop/cli/environment.rb,
lib/rubocop/cli/command/base.rb,
lib/rubocop/cli/command/version.rb,
lib/rubocop/cli/command/show_cops.rb,
lib/rubocop/cli/command/init_dotfile.rb,
lib/rubocop/cli/command/execute_runner.rb,
lib/rubocop/cli/command/auto_genenerate_config.rb

Overview

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

Defined Under Namespace

Modules: Command Classes: Environment, Finished

Constant Summary collapse

STATUS_SUCCESS =
0
STATUS_OFFENSES =
1
STATUS_ERROR =
2
STATUS_INTERRUPTED =
128 + Signal.list['INT']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.


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

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

Instance Attribute Details

#config_storeObject (readonly)

Returns the value of attribute config_store


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

def config_store
  @config_store
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

Instance Method Details

#run(args = ARGV) ⇒ Integer

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

rubocop:disable Metrics/MethodLength, Metrics/AbcSize

Parameters:

  • args (Array<String>) (defaults to: ARGV)

    command line arguments

Returns:

  • (Integer)

    UNIX exit code


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cli.rb', line 31

def run(args = ARGV)
  @options, paths = Options.new.parse(args)
  @env = Environment.new(@options, @config_store, paths)

  if @options[:init]
    run_command(:init)
  else
    validate_options_vs_config
    act_on_options
    apply_default_formatter
    execute_runners
  end
rescue ConfigNotFoundError, IncorrectCopNameError, OptionArgumentError => e
  warn e.message
  STATUS_ERROR
rescue RuboCop::Error => e
  warn Rainbow("Error: #{e.message}").red
  STATUS_ERROR
rescue Finished
  STATUS_SUCCESS
rescue OptionParser::InvalidOption => e
  warn e.message
  warn 'For usage information, use --help'
  STATUS_ERROR
rescue StandardError, SyntaxError, LoadError => e
  warn e.message
  warn e.backtrace
  STATUS_ERROR
end