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, #has_default, #name, #parameterize, #required, #type

Instance Method Summary collapse

Methods inherited from Optitron::Option

#interpolate_type, #validate

Constructor Details

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

Returns a new instance of Arg.



145
146
147
148
149
150
151
152
153
# File 'lib/optitron/option.rb', line 145

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]
  @required = opts && opts.key?(:required) ? opts[:required] : true
  @type = opts && opts[:type]
end

Instance Attribute Details

#greedyObject Also known as: greedy?

Returns the value of attribute greedy.



143
144
145
# File 'lib/optitron/option.rb', line 143

def greedy
  @greedy
end

#inclusion_testObject

Returns the value of attribute inclusion_test.



143
144
145
# File 'lib/optitron/option.rb', line 143

def inclusion_test
  @inclusion_test
end

Instance Method Details

#consume(response, tokens) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/optitron/option.rb', line 155

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