Class: Optitron::Option::Opt
- Inherits:
-
Optitron::Option
- Object
- Optitron::Option
- Optitron::Option::Opt
- 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
-
#include_in_params ⇒ Object
(also: #include_in_params?)
Returns the value of attribute include_in_params.
-
#parent_cmd ⇒ Object
Returns the value of attribute parent_cmd.
-
#run ⇒ Object
Returns the value of attribute run.
-
#short_name ⇒ Object
Returns the value of attribute short_name.
-
#use_no ⇒ Object
Returns the value of attribute use_no.
Attributes inherited from Optitron::Option
#default, #desc, #group, #has_default, #inclusion_test, #name, #parameterize, #required, #type
Instance Method Summary collapse
- #consume(response, tokens) ⇒ Object
- #find_matching_token(tokens) ⇒ Object
-
#initialize(name, desc = nil, opts = nil) ⇒ Opt
constructor
A new instance of Opt.
- #match?(tok) ⇒ Boolean
Methods inherited from Optitron::Option
#any?, #array?, #boolean?, #greedy?, #hash?, #interpolate_type, #numeric?, #string?, #validate
Constructor Details
#initialize(name, desc = nil, opts = nil) ⇒ Opt
Returns a new instance of Opt.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/optitron/option.rb', line 107 def initialize(name, desc = nil, opts = nil) if desc.is_a?(Hash) desc, opts = nil, desc end @name, @desc = name, desc self.type = opts && opts[:type] || :boolean self.short_name = opts[:short_name] if opts && opts[:short_name] self.run = opts[:run] if opts && opts[:run] self.group = opts[:group] if opts && opts[:group] self.inclusion_test = opts[:in] if opts && opts[:in] self.required = opts && opts.key?(:required) ? opts[:required] : false self.default = opts && opts.key?(:default) ? opts[:default] : (@type == :boolean ? false : nil) self.use_no = opts && opts.key?(:use_no) ? opts[:use_no] : false end |
Instance Attribute Details
#include_in_params ⇒ Object Also known as: include_in_params?
Returns the value of attribute include_in_params.
105 106 107 |
# File 'lib/optitron/option.rb', line 105 def include_in_params @include_in_params end |
#parent_cmd ⇒ Object
Returns the value of attribute parent_cmd.
105 106 107 |
# File 'lib/optitron/option.rb', line 105 def parent_cmd @parent_cmd end |
#run ⇒ Object
Returns the value of attribute run.
105 106 107 |
# File 'lib/optitron/option.rb', line 105 def run @run end |
#short_name ⇒ Object
Returns the value of attribute short_name.
105 106 107 |
# File 'lib/optitron/option.rb', line 105 def short_name @short_name end |
#use_no ⇒ Object
Returns the value of attribute use_no.
105 106 107 |
# File 'lib/optitron/option.rb', line 105 def use_no @use_no end |
Instance Method Details
#consume(response, tokens) ⇒ Object
134 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 176 177 178 179 180 181 182 183 184 |
# File 'lib/optitron/option.rb', line 134 def consume(response, tokens) if opt_tok = find_matching_token(tokens) opt_tok_index = tokens.index(opt_tok) tokens.delete_at(opt_tok_index) case @type when :boolean value = if opt_tok.name == "no-#{name}" default elsif opt_tok.respond_to?(:value) opt_tok.value elsif opt_tok.name == short_name and tokens[opt_tok_index].respond_to?(:lit) and BOOLEAN_VALUES.include?(tokens[opt_tok_index].lit) tokens.delete_at(opt_tok_index).lit end response.params_array << [self, value.nil? ? !default : value] when :numeric, :string, :float, nil value = if opt_tok.name == name if opt_tok.respond_to?(:value) opt_tok.value else response.add_error("missing", opt_tok.name) end elsif tokens[opt_tok_index].respond_to?(:lit) tokens.delete_at(opt_tok_index).lit elsif default default else response.add_error("required", opt_tok.name) end response.params_array << [self, value] when :array values = [] values << opt_tok.value if opt_tok.respond_to?(:value) while tokens[opt_tok_index].respond_to?(:lit) values << tokens.delete_at(opt_tok_index).lit end response.params_array << [self, values] when :hash values = [] if opt_tok.respond_to?(:value) response.add_error("not in the form key:value", name) if opt_tok.value[':'].nil? values << opt_tok.value.split(':', 2) end while tokens[opt_tok_index].respond_to?(:lit) and !tokens[opt_tok_index].lit[':'].nil? values << tokens.delete_at(opt_tok_index).lit.split(':', 2) end response.params_array << [self, Hash[values]] else raise "unknown type: #{@type.inspect}" end end end |
#find_matching_token(tokens) ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/optitron/option.rb', line 126 def find_matching_token(tokens) tokens.find do |t| if t.respond_to?(:name) and (t.name == name or t.name == short_name or t.name == "no-#{name}") t.respond_to?(:value) ^ (t.name == short_name) end end end |
#match?(tok) ⇒ Boolean
122 123 124 |
# File 'lib/optitron/option.rb', line 122 def match?(tok) tok.respond_to?(:name) && (!use_no ? [name, short_name] : [name, short_name, "no-#{name}"]).include?(tok.name) end |