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
19
20
# File 'lib/cc/cli/runner.rb', line 16

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

  @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.debug("backtrace: #{ex.backtrace.join("\n\t")}")
end

Instance Method Details

#commandObject



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

def command
  @args.first
end

#command_argumentsObject



50
51
52
# File 'lib/cc/cli/runner.rb', line 50

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

#command_classObject



36
37
38
39
40
# File 'lib/cc/cli/runner.rb', line 36

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

#command_nameObject



42
43
44
45
46
47
48
# File 'lib/cc/cli/runner.rb', line 42

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



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



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

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