Class: Optitron::Option::Arg
- Inherits:
-
Optitron::Option
- Object
- Optitron::Option
- Optitron::Option::Arg
- 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
-
#greedy ⇒ Object
(also: #greedy?)
Returns the value of attribute greedy.
-
#inclusion_test ⇒ Object
Returns the value of attribute inclusion_test.
Attributes inherited from Optitron::Option
#default, #desc, #has_default, #name, #parameterize, #required, #type
Instance Method Summary collapse
- #consume(response, tokens) ⇒ Object
-
#initialize(name = nil, desc = nil, opts = nil) ⇒ Arg
constructor
A new instance of Arg.
Methods inherited from Optitron::Option
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
#greedy ⇒ Object 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_test ⇒ Object
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 |