Class: RuboCop::Service::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



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

def initialize
  @options = { verbose: false }
  @commands = {}
end

Instance Method Details

#command(name, description = nil, &block) ⇒ Object



61
62
63
# File 'lib/rubocop/service/cli.rb', line 61

def command(name, description = nil, &block)
  @commands[name] = [description, block]
end

#run(argv) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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/service/cli.rb', line 13

def run(argv)
  parser = OptionParser.new

  parser.program_name = "rubocop-service"
  parser.version = VERSION

  command "install", "Patch the rubocop." do
    RuboCop::Service::Installer.new.run
  end
  command "uninstall", "Unpatch the rubocop." do
    RuboCop::Service::Uninstaller.new.run
  end
  command "start", "Start the manager server." do
    RuboCop::Service::Server.start
  end
  command "stop", "Stop the manager server." do
    RuboCop::Service::Server.stop
  end
  command "status", "Show status of the manager server." do
    RuboCop::Service::Server.status
  end
  command nil do
    puts parser.help
  end

  parser.banner = +<<~BANNER
    Usage: rubocop-service [options] [command]

    Commands:
  BANNER
  @commands.each do |command, (desc, _block)|
    next unless desc
    parser.banner << "    #{command} - #{desc}\n"
  end

  parser.separator ""
  parser.separator "Options:"
  parser.on("-v", "--verbose", "Verbose output") do |verb|
    @options[:verbose] = verb
  end

  parser.parse! argv

  ENV["RUBOCOP_SERVICE_VERBOSE"] = "true" if @options[:verbose]

  @commands[argv.first] ? @commands[argv.first].last.call : @commands[nil].last.call
end