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.



16
17
18
# File 'lib/cc/cli/runner.rb', line 16

def initialize(args)
  @args = args
end

Class Method Details

.run(argv) ⇒ Object



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

def self.run(argv)
  new(argv).run
rescue => ex
  $stderr.puts("error: (#{ex.class}) #{ex.message}")
  CLI.logger.debug("backtrace: #{ex.backtrace.join("\n\t")}")
  exit 1
end

Instance Method Details

#commandObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cc/cli/runner.rb', line 51

def command
  command_name = @args.first
  case command_name
  when nil, "-h", "-?", "--help"
    "help"
  when "-v", "--version"
    "version"
  else
    command_name
  end
end

#command_argumentsObject



47
48
49
# File 'lib/cc/cli/runner.rb', line 47

def command_arguments
  Array(@args[1..-1])
end

#command_classObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/cc/cli/runner.rb', line 36

def command_class
  command_const = Command[command]
  if command_const.abstract?
    nil
  else
    command_const
  end
rescue NameError
  nil
end

#command_not_foundObject



31
32
33
34
# File 'lib/cc/cli/runner.rb', line 31

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

#runObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/cc/cli/runner.rb', line 20

def run
  VersionChecker.new.check if check_version?

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