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.



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

def initialize(args)
  @args = args
end

Class Method Details

.run(argv) ⇒ Object



8
9
10
11
12
13
14
15
# 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.debug("backtrace: #{ex.backtrace.join("\n\t")}")
  exit 1
end

Instance Method Details

#commandObject



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

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



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

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

#command_classObject



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

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

#command_not_foundObject



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

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

#runObject



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

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