Sha256: 119b3af38b2bc6cc35b9d447e592c085d478c7fbd2be40770bbabc35940e0426

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 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.command 'version',                            'show the gem version'
        group.space
        group.command 'pssh <username> <output> <command>', 'parallel ssh your nodes'
      end
    end

    def index
      display usage
    end

    def version
      display Cloudkick::Client.version
    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.4 lib/cloudkick/commands/help.rb
cloudkick-0.2.3 lib/cloudkick/commands/help.rb
cloudkick-0.2.2 lib/cloudkick/commands/help.rb
cloudkick-0.2.1 lib/cloudkick/commands/help.rb