Class: Cri::Command

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

Overview

Cri::Command represents a command that can be executed on the commandline. It is an abstract superclass for all commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#baseObject

Returns the value of attribute base.



7
8
9
# File 'lib/SANStore/cri/command.rb', line 7

def base
  @base
end

Instance Method Details

#<=>(other) ⇒ Object

Compares this command’s name to the other given command’s name.



98
99
100
# File 'lib/SANStore/cri/command.rb', line 98

def <=>(other)
  self.name <=> other.name
end

#aliasesObject

Returns an array of strings containing the aliases for this command. Subclasses must implement this method.

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/SANStore/cri/command.rb', line 17

def aliases
  raise NotImplementedError.new("Command subclasses should override #aliases")
end

#helpObject

Returns the help text for this command.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/SANStore/cri/command.rb', line 63

def help
  text = ''

  # Append usage
  text << usage + "\n"

  # Append aliases
  unless aliases.empty?
    text << "\n"
    text << "aliases: #{aliases.join(' ')}\n"
  end

  # Append short description
  text << "\n"
  text << short_desc + "\n"

  # Append long description
  text << "\n"
  text << long_desc.wrap_and_indent(78, 4) + "\n"

  # Append options
  unless option_definitions.empty?
    text << "\n"
    text << "options:\n"
    text << "\n"
    option_definitions.sort { |x,y| x[:long] <=> y[:long] }.each do |opt_def|
      text << sprintf("    -%1s --%-10s %s\n\n", opt_def[:short], opt_def[:long], opt_def[:desc].wrap_and_indent(78, 20).lstrip)
    end
  end

  # Return text
  text
end

#long_descObject

Returns a string containing this command’s complete description, which should explain what this command does and how it works in detail. Subclasses must implement this method.

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/SANStore/cri/command.rb', line 31

def long_desc
  raise NotImplementedError.new("Command subclasses should override #long_desc")
end

#nameObject

Returns a string containing the name of thi command. Subclasses must implement this method.

Raises:

  • (NotImplementedError)


11
12
13
# File 'lib/SANStore/cri/command.rb', line 11

def name
  raise NotImplementedError.new("Command subclasses should override #name")
end

#option_definitionsObject

Returns an array containing this command’s option definitions. See the documentation for Cri::OptionParser for details on what option definitions look like. Subclasses may implement this method if the command has options.



45
46
47
# File 'lib/SANStore/cri/command.rb', line 45

def option_definitions
  []
end

#run(options, arguments) ⇒ Object

Executes the command. Subclasses must implement this method (obviously… what’s the point of a command that can’t be run?).

options

A hash containing the parsed commandline options. For example, ‘–foo=bar’ will be converted into { :foo => ‘bar’ }. See the Cri::OptionParser documentation for details.

arguments

An array of strings representing the commandline arguments given to this command.

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/SANStore/cri/command.rb', line 58

def run(options, arguments)
  raise NotImplementedError.new("Command subclasses should override #run")
end

#short_descObject

Returns a string containing this command’s short description, which should not be longer than 50 characters. Subclasses must implement this method.

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/SANStore/cri/command.rb', line 24

def short_desc
  raise NotImplementedError.new("Command subclasses should override #short_desc")
end

#usageObject

Returns a string containing this command’s usage. Subclasses must implement this method.

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/SANStore/cri/command.rb', line 37

def usage
  raise NotImplementedError.new("Command subclasses should override #usage")
end