Class: Optitron::Option::Arg

Inherits:
Optitron::Option show all
Defined in:
lib/optitron/option.rb

Constant Summary

Constants inherited from Optitron::Option

BOOLEAN_VALUES, FALSE_BOOLEAN_VALUES, TRUE_BOOLEAN_VALUES

Instance Attribute Summary collapse

Attributes inherited from Optitron::Option

#default, #desc, #group, #has_default, #name, #parameterize, #required, #type

Instance Method Summary collapse

Methods inherited from Optitron::Option

#any?, #array?, #boolean?, #greedy?, #hash?, #interpolate_type, #numeric?, #string?, #validate

Constructor Details

#initialize(name = nil, desc = nil, opts = nil) ⇒ Arg

Returns a new instance of Arg.



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/optitron/option.rb', line 203

def initialize(name = nil, desc = nil, opts = nil)
  if desc.is_a?(Hash)
    desc, opts = nil, desc
  end
  @name, @desc = name, desc
  self.inclusion_test = opts[:in] if opts && opts[:in]
  self.default = opts && opts[:default]
  self.group = opts[:group] if opts && opts[:group]
  self.type = opts && opts[:type]
  self.required = opts && opts.key?(:required) ? opts[:required] : (@default.nil? and !greedy?)
end

Instance Attribute Details

#greedyObject

Returns the value of attribute greedy.



202
203
204
# File 'lib/optitron/option.rb', line 202

def greedy
  @greedy
end

#inclusion_testObject

Returns the value of attribute inclusion_test.



202
203
204
# File 'lib/optitron/option.rb', line 202

def inclusion_test
  @inclusion_test
end

#parent_cmdObject

Returns the value of attribute parent_cmd.



202
203
204
# File 'lib/optitron/option.rb', line 202

def parent_cmd
  @parent_cmd
end

Instance Method Details

#consume(response, tokens) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/optitron/option.rb', line 232

def consume(response, tokens)
  case type
  when :greedy, :array
    consume_array(response, tokens) { |t| t.lit }
  when :hash
    consume_array(response, tokens) { |t| t.lit[':'] && t.lit.split(':', 2) }
    response.args_with_tokens.last[-1] = Hash[response.args_with_tokens.last[-1]]
  else
    arg_token = tokens.find{ |tok| tok.respond_to?(:lit) }
    if arg_token || has_default
      tokens.delete_at(tokens.index(arg_token)) if arg_token
      response.args_with_tokens << [self, arg_token ? arg_token.lit : default]
    elsif !arg_token and required?
      response.add_error("required", name)
    end
  end
end

#consume_array(response, tokens) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/optitron/option.rb', line 215

def consume_array(response, tokens)
  arg_tokens = tokens.select{ |tok| tok.respond_to?(:lit) }
  response.args_with_tokens << [self, []]
  while !arg_tokens.size.zero?
    if val = yield(arg_tokens.first)
      arg_tok = arg_tokens.shift
      tokens.delete_at(tokens.index(arg_tok))
      response.args_with_tokens.last.last << val
    else
      break
    end
  end
  if required? and response.args_with_tokens.last.last.size.zero?
    response.add_error("required", name)
  end
end