Class: Brainstem::CLI::AbstractCommand

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

Direct Known Subclasses

GenerateApiDocsCommand

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ AbstractCommand

Returns a new instance of the command with options set.



32
33
34
35
36
# File 'lib/brainstem/cli/abstract_command.rb', line 32

def initialize(args = [])
  self.args     = args
  self.options  = default_options
  extract_options!
end

Instance Attribute Details

#argsObject

Storage for passed, unparsed args.



67
68
69
# File 'lib/brainstem/cli/abstract_command.rb', line 67

def args
  @args
end

#optionsObject

Storage for given options.



61
62
63
# File 'lib/brainstem/cli/abstract_command.rb', line 61

def options
  @options
end

Class Method Details

.call(args = []) ⇒ Brainstem::CLI::AbstractCommand

Convenience method for instantiating the command and calling it.

Returns:



22
23
24
25
26
# File 'lib/brainstem/cli/abstract_command.rb', line 22

def self.call(args = [])
  instance = new(args)
  instance.call
  instance
end

Instance Method Details

#callObject

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

Raises:

  • (NotImplementedError)


52
53
54
55
# File 'lib/brainstem/cli/abstract_command.rb', line 52

def call
  raise NotImplementedError,
    "Override #call and implement your application logic."
end

#default_optionsObject

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



43
44
45
# File 'lib/brainstem/cli/abstract_command.rb', line 43

def default_options
  {}
end

#extract_options!Hash

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

Returns:

  • (Hash)

    the extracted command-line options



76
77
78
# File 'lib/brainstem/cli/abstract_command.rb', line 76

def extract_options!
  option_parser.order!(args)
end

#option_parserOptionParser

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.

Returns:

  • (OptionParser)

    an instance of OptionParser

Raises:

  • (NotImplementedError)

See Also:



91
92
93
94
# File 'lib/brainstem/cli/abstract_command.rb', line 91

def option_parser
  raise NotImplementedError,
    "Must return an instance of OptionParser."
end