class Brainstem::CLI::AbstractCommand

Attributes

args[RW]

Storage for passed, unparsed args.

options[RW]

Storage for given options.

Public Class Methods

call(args = []) click to toggle source

Convenience method for instantiating the command and calling it.

@return [Brainstem::CLI::AbstractCommand] the instance

# File lib/brainstem/cli/abstract_command.rb, line 22
def self.call(args = [])
  instance = new(args)
  instance.call
  instance
end
new(args = []) click to toggle source

Returns a new instance of the command with options set.

# File lib/brainstem/cli/abstract_command.rb, line 31
def initialize(args = [])
  self.args     = args
  self.options  = default_options
  extract_options!
end

Public Instance Methods

call() click to toggle source

Kicks off execution of app-level code. Has available to it options, which contains the options extracted from the command line.

# File lib/brainstem/cli/abstract_command.rb, line 49
def call
  raise NotImplementedError,
    "Override #call and implement your application logic."
end
default_options() click to toggle source

Returns the hash of default options used as a base into which cli args are merged.

# File lib/brainstem/cli/abstract_command.rb, line 41
def default_options
  {}
end
extract_options!() click to toggle source

Extracts command-line options for this specific command based on the OptionParser specified in self.option_parser.

@return [Hash] the extracted command-line options

# File lib/brainstem/cli/abstract_command.rb, line 70
def extract_options!
  option_parser.order!(args)
end
option_parser() click to toggle source

An OptionParser instance that specifies how options should be extracted specific to this command.

Available to this method is the options method, which is the primary method of communicating options to the call method.

@return [OptionParser] an instance of OptionParser @see ruby-doc.org/stdlib-2.2.2/libdoc/optparse/rdoc/OptionParser.html

# File lib/brainstem/cli/abstract_command.rb, line 84
def option_parser
  raise NotImplementedError,
    "Must return an instance of OptionParser."
end