Class: Heroics::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(name, commands, output) ⇒ CLI

Instantiate a CLI for an API described by a JSON schema.



10
11
12
13
14
# File 'lib/heroics/cli.rb', line 10

def initialize(name, commands, output)
  @name = name
  @commands = commands
  @output = output
end

Instance Method Details

#run(*parameters) ⇒ Object

Run a command.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/heroics/cli.rb', line 21

def run(*parameters)
  name = parameters.shift
  if name.nil? || name == 'help'
    if command_name = parameters.first
      command = @commands[command_name]
      command.usage
    else
      usage
    end
  else
    command = @commands[name]
    if command.nil?
      @output.write("There is no command called '#{name}'.\n")
    else
      command.run(*parameters)
    end
  end
end