Class: Consoler::Matcher
- Inherits:
-
Object
- Object
- Consoler::Matcher
- Defined in:
- lib/consoler/matcher.rb
Overview
Argument/Options matcher
Given a list of arguments and a list option try to match them
Instance Method Summary collapse
-
#_analyze(arg) ⇒ true?
private
Analyze a single argument.
-
#_each_optional_before_sorted ⇒ Consoler::Matcher
private
Loop through the optionals before map.
-
#_fill_defaults ⇒ Consoler::Matcher
private
Give all unmatched optional options there default value.
-
#_loop_args {|String| ... } ⇒ Consoler::Matcher
private
Loop through the arguments.
-
#_match_arguments ⇒ Array<String>?
private
Match arguments to defined option arguments.
-
#_match_arguments_optionals_before ⇒ Consoler::Matcher
private
Create a map of all optionals and before which mandatory argument they appear.
-
#_match_arguments_options_before_matcher ⇒ Consoler::Matcher
private
Match remaining args against the optionals map.
-
#_peek_next ⇒ String?
private
Peek at the next argument.
-
#_skip_next ⇒ nil, Consoler::Matcher
private
Skip to the next argument.
-
#initialize(arguments, options) ⇒ Matcher
constructor
Create a matcher.
-
#match ⇒ Hash?
Match arguments against options.
-
#option_value!(option) ⇒ Object
private
Set the value of an option.
Constructor Details
#initialize(arguments, options) ⇒ Matcher
Create a matcher
12 13 14 15 16 17 18 19 |
# File 'lib/consoler/matcher.rb', line 12 def initialize(arguments, ) @arguments = arguments @options = @index = 0 @matched_options = {} @argument_values = [] end |
Instance Method Details
#_analyze(arg) ⇒ true? (private)
Analyze a single argument
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/consoler/matcher.rb', line 72 def _analyze(arg) is_long = false is_short = false name = nil if arg[0..1] == '--' is_long = true name = arg[2..-1] elsif arg[0] == '-' is_short = true name = arg[1..-1] end # arg is not a long/short option, add to arguments values unless is_long || is_short @argument_values.push arg return true end unless name.nil? # get the name of the option, short options use the first character option_name = if is_short name[0] else name end option, matched = @options.get_with_alias option_name # no option by this name in options return nil if option.nil? # see if the type if right, short or long if matched.is_long && !is_long return nil elsif matched.is_short && !is_short return nil end if is_long if option.is_value # is_value needs a next argument for its value return nil if _peek_next.nil? @matched_options[option.name] = _peek_next _skip_next else option_value! option end end if is_short if name.size == 1 && option.is_value # is_value needs a next argument for its value return nil if _peek_next.nil? @matched_options[option.name] = _peek_next _skip_next else # for every character (short option) increment the option value name.split('').each do |n| short_option = @options.get n return nil if short_option.nil? option_value! short_option end end end end true end |
#_each_optional_before_sorted ⇒ Consoler::Matcher (private)
Loop through the optionals before map
Sorted by number of optionals in a group
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/consoler/matcher.rb', line 335 def _each_optional_before_sorted @optionals_before.each do |_, optionals| tmp = [] optionals.each do |optional_index, before| tmp.push( count: before.size, index: optional_index, ) end tmp.sort! { |a, b| b[:count] - a[:count] }.each do |item| yield optionals[item[:index]] end end self end |
#_fill_defaults ⇒ Consoler::Matcher (private)
Give all unmatched optional options there default value
318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/consoler/matcher.rb', line 318 def _fill_defaults @options.each do |option| next unless option.is_optional unless @matched_options.key? option.name @matched_options[option.name] = option.default_value end end self end |
#_loop_args {|String| ... } ⇒ Consoler::Matcher (private)
Loop through the arguments
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/consoler/matcher.rb', line 166 def _loop_args @index = 0 size = @arguments.args.size # use an incrementing index, to be able to peek to the next in the list # and to skip an item while @index < size yield @arguments.args[@index] _skip_next end self end |
#_match_arguments ⇒ Array<String>? (private)
Match arguments to defined option arguments
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/consoler/matcher.rb', line 206 def _match_arguments @optionals_before = {} @optionals_before_has_remaining = false total_argument_values = @argument_values.size argument_values_index = 0 _match_arguments_optionals_before @optionals_before.each do |mandatory_arg_name, optionals| # fill the optional argument option with a value if there are enough # arguments supplied (info available from optionals map) optionals.each do |_, optional| optional.each do |before| if before[:included] return nil if argument_values_index >= total_argument_values @matched_options[before[:name]] = @argument_values[argument_values_index] argument_values_index += 1 end end end # only fill mandatory argument if its not the :REMAINING key if mandatory_arg_name != :REMAINING return nil if argument_values_index >= total_argument_values @matched_options[mandatory_arg_name] = @argument_values[argument_values_index] argument_values_index += 1 end end remaining = [] # left over arguments while argument_values_index < @argument_values.size remaining.push @argument_values[argument_values_index] argument_values_index += 1 end remaining end |
#_match_arguments_optionals_before ⇒ Consoler::Matcher (private)
Create a map of all optionals and before which mandatory argument they appear
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/consoler/matcher.rb', line 252 def _match_arguments_optionals_before @optionals_before = {} tracker = {} @options.each do |option, _key| next unless option.is_argument if option.is_optional # setup tracker for optional group tracker[option.is_optional] = [] if tracker[option.is_optional].nil? # mark all optionals as not-included tracker[option.is_optional].push( included: false, name: option.name, ) else @optionals_before[option.name] = tracker tracker = {} end end # make sure all optionals are accounted for in the map if tracker != {} # use a special key so we can handle it differently in the filling process @optionals_before[:REMAINING] = tracker @optionals_before_has_remaining = true end self end |
#_match_arguments_options_before_matcher ⇒ Consoler::Matcher (private)
Match remaining args against the optionals map
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/consoler/matcher.rb', line 289 def # number of arguments that are needed to fill our mandatory argument options mandatories_matched = @optionals_before.size # there are optionals at the end of the options, don't match the void if @optionals_before_has_remaining mandatories_matched -= 1 end total = 0 # loop through optional map _each_optional_before_sorted do |before| # are there enough arguments left to fill this optional group if (total + before.size + mandatories_matched) <= @argument_values.size total += before.size before.each do |val| val[:included] = true end end end self end |
#_peek_next ⇒ String? (private)
Peek at the next argument
Only useful inside #_loop_args
186 187 188 |
# File 'lib/consoler/matcher.rb', line 186 def _peek_next @arguments.args[@index + 1] end |
#_skip_next ⇒ nil, Consoler::Matcher (private)
Skip to the next argument
Useful if you use a peeked argument
196 197 198 199 200 |
# File 'lib/consoler/matcher.rb', line 196 def _skip_next @index += 1 self end |
#match ⇒ Hash?
Match arguments against options
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/consoler/matcher.rb', line 24 def match = true _loop_args do |arg| unless @argument_values.push arg next end # when "argument" is --, then stop parsing the rest of the arguments # and treat the rest as regular arguments if arg == '--' = false next end analyzed = _analyze arg if analyzed.nil? return nil end end remaining = _match_arguments _fill_defaults if @matched_options.size == @options.size @matched_options['remaining'] = remaining # make sure all aliases are also filled @options.each do |option| option.aliases.each do |alias_| @matched_options[alias_.name] = @matched_options[option.name] end end return @matched_options end nil end |
#option_value!(option) ⇒ Object (private)
Set the value of an option
Long or short option needed
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/consoler/matcher.rb', line 150 def option_value!(option) if option.is_short if @matched_options[option.name].nil? @matched_options[option.name] = 0 end @matched_options[option.name] += 1 else @matched_options[option.name] = true end end |