Class: CC::CLI::Runner

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



19
20
21
22
23
# File 'lib/cc/cli/runner.rb', line 19

def initialize(args)
  SafeYAML::OPTIONS[:default_mode] = :safe

  @args = args
end

Class Method Details

.run(argv) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/cc/cli/runner.rb', line 9

def self.run(argv)
  new(argv).run
rescue => ex
  $stderr.puts("error: (#{ex.class}) #{ex.message}")

  if ENV["CODECLIMATE_DEBUG"]
    $stderr.puts("backtrace: #{ex.backtrace.join("\n\t")}")
  end
end

Instance Method Details

#commandObject



57
58
59
# File 'lib/cc/cli/runner.rb', line 57

def command
  @args.first
end

#command_argumentsObject



53
54
55
# File 'lib/cc/cli/runner.rb', line 53

def command_arguments
  @args[1..-1]
end

#command_classObject



39
40
41
42
43
# File 'lib/cc/cli/runner.rb', line 39

def command_class
  CLI.const_get(command_name)
rescue NameError
  nil
end

#command_nameObject



45
46
47
48
49
50
51
# File 'lib/cc/cli/runner.rb', line 45

def command_name
  case command
  when nil, '-h', '-?', '--help' then 'Help'
  when '-v', '--version'         then 'Version'
  else command.sub(":", "::").underscore.camelize
  end
end

#command_not_foundObject



34
35
36
37
# File 'lib/cc/cli/runner.rb', line 34

def command_not_found
  $stderr.puts "unknown command #{command}"
  exit 1
end

#runObject



25
26
27
28
29
30
31
32
# File 'lib/cc/cli/runner.rb', line 25

def run
  if command_class
    command = command_class.new(command_arguments)
    command.execute
  else
    command_not_found
  end
end