Module: Parameters::Options

Defined in:
lib/parameters/options.rb

Overview

Since:

  • 0.4.0

Constant Summary collapse

USAGES =

The usage messages for various Parameter Types.

Since:

  • 0.4.0

{
  Types::Integer  => 'NUM',
  Types::Float    => 'DEC',
  Types::Symbol   => 'NAME',
  Types::Time     => 'TIME',
  Types::DateTime => 'DATE|TIME',
  Types::Date     => 'DATE',
  Types::Regexp   => 'REGEXP',
  Types::URI      => 'URI',
  Types::Array    => 'VALUE [...]',
  Types::Set      => 'VALUE [...]',
  Types::Hash     => 'NAME:VALUE [...]'
}
ACCEPTS =

The OptionParser acceptance classes for various Parameter Types.

Since:

  • 0.4.0

{
  Types::Object   => Object,
  Types::Boolean  => TrueClass,
  Types::Integer  => Integer,
  Types::Float    => Float,
  Types::Symbol   => Symbol,
  Types::String   => String,
  Types::Time     => String,
  Types::DateTime => String,
  Types::Date     => String,
  Types::URI      => String
}

Class Method Summary collapse

Class Method Details

.accepts(param) ⇒ Class

Returns the OptionParser acceptance class for the parameter.

Parameters:

  • param (Param)

    The parameter.

Returns:

  • (Class)

    The acceptance class.

Since:

  • 0.4.0



103
104
105
106
107
108
109
110
111
112
113
# 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 = {}) ⇒ Object

Defines an option for the parameter.

Parameters:

  • opts (OptionParser)

    The option parser.

  • param (Param)

    The parameter.

  • options (Hash) (defaults to: {})

    Additional options for the option.

Options Hash (options):

  • :flag (String)

    The short flag for the option.

  • :usage (String)

    The USAGE String for the option.

Since:

  • 0.4.0



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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
      else
        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
      else
        param.value += param.coerce(value)
      end
    end
  else
    opts.on(*args) do |value|
      param.value = value
    end
  end
end

.flag(param) ⇒ String

Returns the option flag for the given parameter.

Parameters:

  • param (Param)

    The parameter.

Returns:

  • (String)

    The option flag.

Since:

  • 0.4.0



51
52
53
54
55
56
57
58
59
# 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| ... } ⇒ OptionParser

Defines an OptionParser for a set of parameters.

Parameters:

  • object (Parameter)

    The Class or Object which included Parameters.

  • opts (OptionParser)

    The option parser to define the parameter options within.

Yields:

  • (opts)

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

Yield Parameters:

  • opts (OptionParser)

    The newly created OptionParser.

Returns:

  • (OptionParser)

    The defined OptionParser.

Since:

  • 0.4.0



197
198
199
200
201
202
203
204
205
# File 'lib/parameters/options.rb', line 197

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) ⇒ String

Returns the Usage String for the parameter.

Parameters:

  • param (Param)

    The parameter.

Returns:

  • (String)

    The Usage String.

Since:

  • 0.4.0



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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