Class: Smartdict::Commands::HelpCommand

Inherits:
AbstractCommand show all
Includes:
Smartdict::Core
Defined in:
lib/smartdict/commands/help_command.rb

Constant Summary

Constants inherited from AbstractCommand

AbstractCommand::INDENT_SIZE

Instance Method Summary collapse

Methods inherited from AbstractCommand

arguments, default, #extract_arguments_and_options, help_message, help_syntax_message, help_usage_message, inherited, #initialize, options, prog_name, run, #set_arguments!, #set_arguments_and_options!, set_description, set_name, #set_options!, set_summary, set_syntax, set_usage

Constructor Details

This class inherits a constructor from Smartdict::Commands::AbstractCommand

Instance Method Details

#executeObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/smartdict/commands/help_command.rb', line 23

def execute
  if cmd_name = @arguments[:command]
    if cmd_class = CommandManager.find(cmd_name)
      puts cmd_class.help_message
    else
      abort "Uknown command: #{cmd_name}"
    end
  else
    puts help_message
  end
end

#help_commands_messageObject



42
43
44
45
46
47
48
49
50
# File 'lib/smartdict/commands/help_command.rb', line 42

def help_commands_message
  width = CommandManager.all.keys.map(&:size).max
  result = " " * INDENT_SIZE + "Commands:\n"
  CommandManager.all.each do |command_name, command_class|
    result << " " * 2 * INDENT_SIZE + "#{command_name.ljust(width)}"
    result << "    #{command_class.summary}\n"
  end
  result
end

#help_messageObject



35
36
37
38
39
# File 'lib/smartdict/commands/help_command.rb', line 35

def help_message
  message = "#{description}\n\n"
  message << "#{self.class.help_syntax_message}\n"
  message << help_commands_message
end