Class: ShellShock::HelpCommand
- Inherits:
-
Object
- Object
- ShellShock::HelpCommand
- Defined in:
- lib/shell_shock/help_command.rb
Instance Attribute Summary collapse
-
#help ⇒ Object
readonly
Returns the value of attribute help.
-
#usage ⇒ Object
readonly
Returns the value of attribute usage.
Instance Method Summary collapse
- #completion(text) ⇒ Object
- #display_help_for_command(command_name) ⇒ Object
- #display_help_for_commands ⇒ Object
- #execute(command) ⇒ Object
-
#initialize(commands) ⇒ HelpCommand
constructor
A new instance of HelpCommand.
Constructor Details
#initialize(commands) ⇒ HelpCommand
Returns a new instance of HelpCommand.
5 6 7 8 9 |
# File 'lib/shell_shock/help_command.rb', line 5 def initialize commands @commands = commands @usage = '<command name>' @help = 'displays the help information for a command' end |
Instance Attribute Details
#help ⇒ Object (readonly)
Returns the value of attribute help.
3 4 5 |
# File 'lib/shell_shock/help_command.rb', line 3 def help @help end |
#usage ⇒ Object (readonly)
Returns the value of attribute usage.
3 4 5 |
# File 'lib/shell_shock/help_command.rb', line 3 def usage @usage end |
Instance Method Details
#completion(text) ⇒ Object
11 12 13 |
# File 'lib/shell_shock/help_command.rb', line 11 def completion text @commands.keys.grep(/^#{Regexp.escape(text)}/).sort end |
#display_help_for_command(command_name) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/shell_shock/help_command.rb', line 25 def display_help_for_command command_name command = @commands[command_name] if command puts "Command \"#{command_name}\"" puts "Usage: #{command_name} #{command.usage}" if command.respond_to?(:usage) puts "Help:\n #{command.help}" if command.respond_to?(:help) else puts "unknown command \"#{command_name}\"" end end |
#display_help_for_commands ⇒ Object
19 20 21 22 23 |
# File 'lib/shell_shock/help_command.rb', line 19 def display_help_for_commands return if @commands.keys.empty? puts 'Available commands:' @commands.keys.sort.each { |command| puts command } end |
#execute(command) ⇒ Object
15 16 17 |
# File 'lib/shell_shock/help_command.rb', line 15 def execute command command.empty? ? display_help_for_commands : display_help_for_command(command) end |