Class: ShellShock::HelpCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/shell_shock/help_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#helpObject (readonly)

Returns the value of attribute help.



5
6
7
# File 'lib/shell_shock/help_command.rb', line 5

def help
  @help
end

#usageObject (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_commandsObject



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