Class: RuboCounsel::CLI

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

Overview

Command-line interface that ties all components together.

Responsibilities:

- Run the Runner to collect offending cops
- Pass offending cop names to the Counselor
- Pass Counselor's choices to ConfigWriter
- Output the generated config

Constant Summary collapse

HELP =
<<~TEXT
  Usage: rubocounsel [options] [paths]

  RuboCounsel walks you through each offending RuboCop cop interactively,
  helping you configure, disable, or skip each one.

  All RuboCop flags are passed through (e.g. --only, --except, --config).

  Options:
    -h, --help       Show this help message
    -v, --version    Show version
TEXT

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths: ["."], rubocop_options: {}, counselor_factory: nil, output: $stdout) ⇒ CLI

Returns a new instance of CLI.



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

def initialize(paths: ["."], rubocop_options: {}, counselor_factory: nil, output: $stdout)
  @paths = paths
  @rubocop_options = rubocop_options
  @counselor_factory = counselor_factory || method(:default_counselor)
  @output = output
end

Class Method Details

.run(args, output: $stdout) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocounsel/cli.rb', line 27

def self.run(args, output: $stdout)
  if args.include?("--help") || args.include?("-h")
    output.puts HELP
    return 0
  end

  if args.include?("--version") || args.include?("-v")
    output.puts "rubocounsel #{RuboCounsel::VERSION}"
    return 0
  end

  options, paths = RuboCop::Options.new.parse(args)
  paths = ["."] if paths.empty?

  new(paths: paths, rubocop_options: options, output: output).run
end

Instance Method Details

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocounsel/cli.rb', line 51

def run
  ::CLI::UI::StdoutRouter.enable

  cop_names = collect_offending_cops
  return 0 if cop_names.empty?

  all_choices = {}
  counsel_cops(cop_names) do |cop_name, choice|
    all_choices[cop_name] = choice
    output_config(all_choices)
  end

  if all_choices.empty?
    @output.puts "All offending cops are already configured."
  end

  0
rescue Interrupt
  @output.puts "\nExiting..."
  0
end