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.



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

def initialize(args)
  @args = args
end

Class Method Details

.run(argv) ⇒ Object



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

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

  CLI.debug("backtrace: #{ex.backtrace.join("\n\t")}")
end

Instance Method Details

#commandObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cc/cli/runner.rb', line 48

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



44
45
46
# File 'lib/cc/cli/runner.rb', line 44

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

#command_classObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/cc/cli/runner.rb', line 33

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

#command_not_foundObject



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

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

#runObject



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

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