Class: Nehm::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/nehm/command.rb

Overview

Base class for all Nehm commands. When creating a new nehm command, define #initialize, #execute, #arguments, #program_name, #summary and #usage (as appropriate) See the above mentioned methods for details

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

In ‘initialize’ should be defined all options by method ‘add_option’ See get_command.rb as example



27
28
29
30
# File 'lib/nehm/command.rb', line 27

def initialize
  @options = {}
  @options_descs = {}
end

Instance Attribute Details

#optionsObject

Hash with options of the command



16
17
18
# File 'lib/nehm/command.rb', line 16

def options
  @options
end

#options_descsObject

Hash with descriptions of each option



21
22
23
# File 'lib/nehm/command.rb', line 21

def options_descs
  @options_descs
end

Instance Method Details

#add_option(option, usage, desc) ⇒ Object

Add a command-line option

Nehm don’t use options with dashes to be more user-friendly

See ‘get_command.rb’ as example



103
104
105
106
# File 'lib/nehm/command.rb', line 103

def add_option(option, usage, desc)
  @options[option] = nil
  @options_descs[usage] = desc
end

#argumentsObject

Override to provide details of the arguments a command takes

For example:

def usage
  "#{program_name} COMMAND"
end

def arguments
  ['COMMAND', 'name of command to show help']
end


72
73
74
# File 'lib/nehm/command.rb', line 72

def arguments
  {}
end

#executeObject

Override to provide command handling

#options will be filled in with your parsed options, unparsed options will be left in #options

Raises:

  • (StandardError)


55
56
57
# File 'lib/nehm/command.rb', line 55

def execute
  raise StandardError, 'generic command has no actions'
end

#handle_options(args) ⇒ Object

Handle the given list of arguments by parsing them and recording the results



44
45
46
47
# File 'lib/nehm/command.rb', line 44

def handle_options(args)
  parser = OptionParser.new(args, self)
  parser.parse
end

#invoke(args) ⇒ Object

Invoke the command with the given list of arguments



35
36
37
38
# File 'lib/nehm/command.rb', line 35

def invoke(args)
  handle_options(args)
  execute
end

#program_nameObject

The name of the command for command-line invocation



79
80
# File 'lib/nehm/command.rb', line 79

def program_name
end

#summaryObject

Override to display a short description of what this command does



85
86
# File 'lib/nehm/command.rb', line 85

def summary
end

#usageObject

Override to display the usage for an individual nehm command

The text “[options]” is automatically appended to the usage text



93
94
# File 'lib/nehm/command.rb', line 93

def usage
end