Class: Brainstem::Cli

Inherits:
Object
  • Object
show all
Includes:
Brainstem::Concerns::Optional
Defined in:
lib/brainstem/cli.rb

Constant Summary collapse

EXECUTABLE_NAME =
'brainstem'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options = {}) ⇒ Cli

Creates a new instance of the Cli to respond to user input.

Input is expected to be the name of the subcommand, followed by any additional arguments.



29
30
31
32
33
34
# File 'lib/brainstem/cli.rb', line 29

def initialize(args, options = {})
  super options

  self._args              = args.dup.freeze
  self.requested_command  = args.shift
end

Instance Attribute Details

#_argsObject

Holds a copy of the initial given args for debugging purposes.



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

def _args
  @_args
end

#requested_commandObject

Storage for the extracted command.



59
60
61
# File 'lib/brainstem/cli.rb', line 59

def requested_command
  @requested_command
end

Class Method Details

.call(args, options = {}) ⇒ Brainstem::Cli

Convenience for instantiating and calling the Cli object.

Returns:



19
20
21
# File 'lib/brainstem/cli.rb', line 19

def self.call(args, options = {})
  new(args, options).call
end

Instance Method Details

#callBrainstem::Cli

Routes to an application endpoint depending on given options.

Returns:



41
42
43
44
45
46
47
48
49
# File 'lib/brainstem/cli.rb', line 41

def call
  if requested_command && commands.has_key?(requested_command)
    self.command_method = commands[requested_command].method(:call)
  end

  command_method.call(_args.drop(1))

  self
end