Module: Parameters::Options
- Defined in:
- lib/parameters/options.rb
Overview
Constant Summary collapse
- USAGES =
The usage messages for various Parameter Types.
{ 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.
{ 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
-
.accepts(param) ⇒ Class
Returns the OptionParser acceptance class for the parameter.
-
.define(opts, param, options = {}) ⇒ Object
Defines an option for the parameter.
-
.flag(param) ⇒ String
Returns the option flag for the given parameter.
-
.parser(object) {|opts| ... } ⇒ OptionParser
Defines an OptionParser for a set of parameters.
-
.usage(param) ⇒ String
Returns the Usage String for the parameter.
Class Method Details
.accepts(param) ⇒ Class
Returns the OptionParser acceptance class for the parameter.
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.
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,={}) short_flag = [:flag] long_flag = flag(param) usage = ([: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.
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.
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.
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 |