Sha256: 87152f5c3f99b09a6af3c5946c0a0ac65e346fb2b761a0897dd45096675b01f1

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

module Cloudkick::Command
  class Help < Base
    class HelpGroup < Array
      attr_reader :title

      def initialize(title)
        @title = title
      end

      def command(name, description)
        self << [name, description]
      end

      def space
        self << ['', '']
      end
    end

    def self.groups
      @groups ||= []
    end

    def self.group(title, &block)
      groups << begin
                  group = HelpGroup.new(title)
                  yield group
                  group
                end
    end

    def self.create_default_groups!
      group 'Commands' do |group|
        group.command 'help',                               'show this usage'
        group.space
        group.command 'pssh --query <query> ' \
        '<command> ' \
        '[--username <username>] ',
        'parallel ssh your nodes'
      end
    end

    def index
      display usage
    end

    def usage
      longest_command_length = self.class.groups.map do |group|
        group.map { |g| g.first.length }
      end.flatten.max

      self.class.groups.inject(StringIO.new) do |output, group|
        output.puts "=== %s" % group.title
        output.puts

        group.each do |command, description|
          if command.empty?
            output.puts
          else
            output.puts "%-*s # %s" % [longest_command_length, command, description]
          end
        end

        output.puts
        output
      end.string
    end
  end
end

Cloudkick::Command::Help.create_default_groups!

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cloudkick-0.2.16 lib/cloudkick/commands/help.rb
cloudkick-0.2.15 lib/cloudkick/commands/help.rb
cloudkick-0.2.14 lib/cloudkick/commands/help.rb
cloudkick-0.2.13 lib/cloudkick/commands/help.rb