Sha256: a5c2f94703ce04901ad2ad417abd4db1a763447fe5dde35e61236d53295e21e6

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

# encoding: utf-8

module Cri

  # @todo Document
  class CommandDSL

    def initialize(command=nil)
      @command = command || Cri::Command.new
    end

    # @todo Document
    def command
      @command
    end

    # @todo Document
    def subcommand(cmd=nil, &block)
      if cmd.nil?
        cmd = Cri::Command.define(&block)
      end

      @command.add_command(cmd)
    end

    # @todo Document
    def name(arg)
      @command.name = arg
    end

    # @todo Document
    def aliases(*args)
      @command.aliases = args.flatten
    end

    # @todo Document
    def summary(arg)
      @command.short_desc = arg
    end

    # @todo Document
    def description(arg)
      @command.long_desc = arg
    end

    # @todo Document
    def usage(arg)
      @command.usage = arg
    end

    # @todo Document
    def option(short, long, desc, params={}, &block)
      requiredness = params[:argument] || :forbidden
      self.add_option(short, long, desc, requiredness, block)
    end
    alias_method :opt, :option

    # @todo Document
    def required(short, long, desc, &block)
      self.add_option(short, long, desc, :required, block)
    end

    # @todo Document
    def flag(short, long, desc, &block)
      self.add_option(short, long, desc, :forbidden, block)
    end
    alias_method :forbidden, :flag

    # @todo Document
    def optional(short, long, desc, &block)
      self.add_option(short, long, desc, :optional, block)
    end

    # @todo Document
    def run(&block)
      unless block.arity != 2 || block.arity != 3
        raise ArgumentError,
          "The block given to Cri::Command#run expects two or three args"
      end

      @command.block = block
    end

  protected

    # @todo Document
    def add_option(short, long, desc, argument, block)
      @command.option_definitions << {
        :short    => short.to_s,
        :long     => long.to_s,
        :desc     => desc,
        :argument => argument,
        :block    => block }
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cri-2.0b1 lib/cri/command_dsl.rb
cri-2.0a3 lib/cri/command_dsl.rb