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.
7 8 9 10 11 |
# File 'lib/shell_shock/help_command.rb', line 7 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.
5 6 7 |
# File 'lib/shell_shock/help_command.rb', line 5 def help @help end |
#usage ⇒ Object (readonly)
Returns the value of attribute usage.
5 6 7 |
# File 'lib/shell_shock/help_command.rb', line 5 def usage @usage end |
Instance Method Details
#completion(text) ⇒ Object
13 14 15 |
# File 'lib/shell_shock/help_command.rb', line 13 def completion(text) @commands.keys.grep(/^#{Regexp.escape(text)}/).sort end |
#display_help_for_command(command_name) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/shell_shock/help_command.rb', line 28 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
21 22 23 24 25 26 |
# File 'lib/shell_shock/help_command.rb', line 21 def display_help_for_commands return if @commands.keys.empty? puts "Available commands:" @commands.keys.sort.each { |command| puts command } end |
#execute(command) ⇒ Object
17 18 19 |
# File 'lib/shell_shock/help_command.rb', line 17 def execute(command) command.empty? ? display_help_for_commands : display_help_for_command(command) end |