Sha256: 80995740056d041b7a6c733828ce811ca6365967f73d27c0d47cc1a05506124e

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

module Wod::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!
      return if @defaults_created
      @defaults_created = true
      group 'General Commands' do |group|
        group.command 'help',                         'show this usage'
        group.command 'version',                      'show the gem version'
        group.space
        group.command 'login',                        'log in with your apple credentials'
        group.command 'logout',                       'clear local authentication credentials'
        group.space
        group.command 'devices',                         'list your registered devices'
        group.command 'devices:add <name> <udid>',      'add a new device'
        group.command 'devices:remove <name>',      'remove device (Still counts against device limit)'
      end
    end    
    
    def index
      puts usage
    end
    
    def usage
      puts "wod v#{Wod::VERSION}"
      puts
      
      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

Wod::Command::Help.create_default_groups!

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wod-0.0.4 lib/wod/commands/help.rb
wod-0.0.3 lib/wod/commands/help.rb
wod-0.0.2 lib/wod/commands/help.rb