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.



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

#helpObject (readonly)

Returns the value of attribute help.



3
4
5
# File 'lib/shell_shock/help_command.rb', line 3

def help
  @help
end

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



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