module Parameters::Options

@since 0.4.0

Constants

ACCEPTS

The OptionParser acceptance classes for various Parameter {Types}.

USAGES

The usage messages for various Parameter {Types}.

Public Class Methods

accepts(param) click to toggle source

Returns the OptionParser acceptance class for the parameter.

@param [Param] param

The parameter.

@return [Class]

The acceptance class.

@api semipublic

# File lib/parameters/options.rb, line 103
def self.accepts(param)
  type = param.type

  if type <= Types::Hash
    Hash
  elsif type <= Types::Array
    Array
  else
    ACCEPTS[type]
  end
end
define(opts,param,options={}) click to toggle source

Defines an option for the parameter.

@param [OptionParser] opts

The option parser.

@param [Param] param

The parameter.

@param [Hash] options

Additional options for the option.

@option options [String] :flag

The short flag for the option.

@option options [String] :usage

The USAGE String for the option.

@api public

# File lib/parameters/options.rb, line 135
def self.define(opts,param,options={})
  short_flag = options[:flag]
  long_flag  = flag(param)
  usage      = (options[:usage] || self.usage(param))

  args  = []

  args << short_flag if short_flag
  args << if usage
            "#{long_flag} [#{usage}]"
          else
            long_flag
          end

  args << accepts(param)

  args << "#{param.description}."           if param.description
  args << "Default: #{param.value.inspect}" if param.value

  if param.type <= Types::Hash
    opts.on(*args) do |value|
      if param.value.nil?
        param.value = value
      elsif value
        param.value.merge!(param.coerce(value))
      end
    end
  elsif param.type <= Types::Array
    opts.on(*args) do |value|
      if param.value.nil?
        param.value = value
      elsif value
        param.value += param.coerce(value)
      end
    end
  else
    opts.on(*args) do |value|
      param.value = value
    end
  end
end
flag(param) click to toggle source

Returns the option flag for the given parameter.

@param [Param] param

The parameter.

@return [String]

The option flag.

@api semipublic

# File lib/parameters/options.rb, line 51
def self.flag(param)
  name = param.name.to_s.gsub('_','-')

  if param.type == Types::Boolean
    "--[no-]#{name}"
  else
    "--#{name}"
  end
end
parser(object) { |opts| ... } click to toggle source

Defines an OptionParser for a set of parameters.

@param [Parameter] object

The Class or Object which included Parameters.

@yield [opts]

If a block is given, it will be passed the newly created OptionParser.

@yieldparam [OptionParser] opts

The newly created OptionParser.

@return [OptionParser]

The defined OptionParser.

@api public

# File lib/parameters/options.rb, line 194
def self.parser(object)
  OptionParser.new do |opts|
    object.each_param do |param|
      define(opts,param)
    end

    yield opts if block_given?
  end
end
usage(param) click to toggle source

Returns the Usage String for the parameter.

@param [Param] param

The parameter.

@return [String]

The Usage String.

@api semipublic

# File lib/parameters/options.rb, line 72
def self.usage(param)
  name = param.name.to_s

  type_usage = lambda { |type|
    if type.class == Types::Hash
      type_usage[type.key_type] + ':' + 
      type_usage[type.value_type] + ' [...]'
    elsif type.class <= Types::Array
      type_usage[type.element_type] + ' [...]'
    elsif (type == Types::String) ||
          (type == Types::Object)
      name.upcase
    else
      USAGES[type]
    end
  }

  return type_usage[param.type]
end