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.

Parameters:

  • name (String)

    The name of the CLI.

  • schema (Schema)

    The JSON schema describing the API.

  • client (Client)

    A client generated from the JSON schema.

  • output (IO)

    The stream to write to.



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.

Parameters:

  • parameters (Array)

    The parameters to use when running the command. The first parameters is the name of the command and the remaining parameters are passed to it.



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