Sha256: a82e5d2be169909f90f0ae9b7003fa463dc5b4e6a8caef4247e193da22a364a3

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module ShellShock
  class HelpCommand
    attr_reader :help, :usage

    def initialize(commands)
      @commands = commands
      @usage = "<command name>"
      @help = "displays the help information for a command"
    end

    def completion(text)
      @commands.keys.grep(/^#{Regexp.escape(text)}/).sort
    end

    def execute(command)
      command.empty? ? display_help_for_commands : display_help_for_command(command)
    end

    def display_help_for_commands
      return if @commands.keys.empty?

      puts "Available commands:"
      @commands.keys.sort.each { |command| puts command }
    end

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shell_shock-0.0.6 lib/shell_shock/help_command.rb