Class: Cri::Command
- Inherits:
-
Object
- Object
- Cri::Command
- 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.
Direct Known Subclasses
SANStore::CLI::Commands::DeleteVol, SANStore::CLI::Commands::Help, SANStore::CLI::Commands::ListVols, SANStore::CLI::Commands::NewVol
Instance Attribute Summary collapse
-
#base ⇒ Object
Returns the value of attribute base.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares this command’s name to the other given command’s name.
-
#aliases ⇒ Object
Returns an array of strings containing the aliases for this command.
-
#help ⇒ Object
Returns the help text for this command.
-
#long_desc ⇒ Object
Returns a string containing this command’s complete description, which should explain what this command does and how it works in detail.
-
#name ⇒ Object
Returns a string containing the name of thi command.
-
#option_definitions ⇒ Object
Returns an array containing this command’s option definitions.
-
#run(options, arguments) ⇒ Object
Executes the command.
-
#short_desc ⇒ Object
Returns a string containing this command’s short description, which should not be longer than 50 characters.
-
#usage ⇒ Object
Returns a string containing this command’s usage.
Instance Attribute Details
#base ⇒ Object
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 |
#aliases ⇒ Object
Returns an array of strings containing the aliases for this command. Subclasses must implement this method.
17 18 19 |
# File 'lib/SANStore/cri/command.rb', line 17 def aliases raise NotImplementedError.new("Command subclasses should override #aliases") end |
#help ⇒ Object
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_desc ⇒ Object
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.
31 32 33 |
# File 'lib/SANStore/cri/command.rb', line 31 def long_desc raise NotImplementedError.new("Command subclasses should override #long_desc") end |
#name ⇒ Object
Returns a string containing the name of thi command. Subclasses must implement this method.
11 12 13 |
# File 'lib/SANStore/cri/command.rb', line 11 def name raise NotImplementedError.new("Command subclasses should override #name") end |
#option_definitions ⇒ Object
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.
58 59 60 |
# File 'lib/SANStore/cri/command.rb', line 58 def run(, arguments) raise NotImplementedError.new("Command subclasses should override #run") end |
#short_desc ⇒ Object
Returns a string containing this command’s short description, which should not be longer than 50 characters. Subclasses must implement this method.
24 25 26 |
# File 'lib/SANStore/cri/command.rb', line 24 def short_desc raise NotImplementedError.new("Command subclasses should override #short_desc") end |
#usage ⇒ Object
Returns a string containing this command’s usage. Subclasses must implement this method.
37 38 39 |
# File 'lib/SANStore/cri/command.rb', line 37 def usage raise NotImplementedError.new("Command subclasses should override #usage") end |