Class: Consoler::Matcher

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(arguments, options) ⇒ Matcher

Create a matcher

Parameters:



14
15
16
17
18
19
20
21
# File 'lib/consoler/matcher.rb', line 14

def initialize(arguments, options)
  @arguments = arguments
  @options = options

  @index = 0
  @matched_options = {}
  @argument_values = []
end

Instance Method Details

#_analyze(arg) ⇒ true? (private)

Analyze a single argument

Parameters:

  • arg (String)

    Single argument

Returns:

  • (true, nil)

    true on success, nil on failure



66
67
68
69
70
71
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
# File 'lib/consoler/matcher.rb', line 66

def _analyze(arg)
  is_long = false
  is_short = false
  name = nil

  if arg[0..1] == '--' then
    is_long = true
    name = arg[2..-1]
  elsif arg[0] == '-' then
    is_short = true
    name = arg[1..-1]
  end

  # arg is not a long/short option, add to arguments values
  unless is_long or is_short then
    @argument_values.push arg
    return true
  end

  unless name.nil? then
    # get the name of the option, short options use the first character
    option_name = if is_short then
                    name[0]
                  else
                    name
                  end

    option = @options.get option_name

    # no option by this name in options
    return nil if option.nil?

    needs_short = option.is_short
    needs_long = option.is_long

    # see if the type if right, short or long
    if needs_long and not is_long then
      return nil
    elsif needs_short and not is_short then
      return nil
    end

    if is_long then
      if option.is_value then
        # is_value needs a next argument for its value
        return nil if _peek_next.nil?
        @matched_options[name] = _peek_next
        _skip_next
      else
        @matched_options[name] = true
      end
    end

    if is_short then
      if name.size == 1 and option.is_value then
        # is_value needs a next argument for its value
        return nil if _peek_next.nil?
        @matched_options[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?

          if @matched_options[n].nil? then
            @matched_options[n] = 0
          end

          @matched_options[n] += 1
        end
      end
    end
  end

  return true
end

#_each_optional_before_sortedConsoler::Matcher (private)

Loop through the optionals before map

Sorted by number of optionals in a group

Returns:



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/consoler/matcher.rb', line 311

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_defaultsConsoler::Matcher (private)

Give all unmatched optional options there default value

Returns:



294
295
296
297
298
299
300
301
302
303
304
# File 'lib/consoler/matcher.rb', line 294

def _fill_defaults
  @options.each do |option|
    if option.is_optional then
      unless @matched_options.has_key? option.name then
        @matched_options[option.name] = option.default_value
      end
    end
  end

  self
end

#_loop_args {|String| ... } ⇒ Consoler::Matcher (private)

Loop through the arguments

Yields:

  • (String)

    An argument

Returns:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/consoler/matcher.rb', line 148

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 do
    yield @arguments.args[@index]

    _skip_next
  end

  self
end

#_match_argumentsArray<String> (private)

Match arguments to defined option arguments

Returns:

  • (Array<String>)

    The remaining args



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/consoler/matcher.rb', line 187

def _match_arguments
  @optionals_before = {}
  @optionals_before_has_remaining = false

  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] then
          @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 then
      @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 do
    remaining.push @argument_values[argument_values_index]
    argument_values_index += 1
  end

  remaining
end

#_match_arguments_optionals_beforeConsoler::Matcher (private)

Create a map of all optionals and before which mandatory argument they appear

Returns:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/consoler/matcher.rb', line 228

def _match_arguments_optionals_before
  @optionals_before = {}
  tracker = {}

  @options.each do |option, key|
    next unless option.is_argument

    if option.is_optional then
      # 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 != {} then
    # use a special key so we can handle it differently in the filling process
    @optionals_before[:REMAINING] = tracker
    @optionals_before_has_remaining = true
  end

  _match_arguments_options_before_matcher

  self
end

#_match_arguments_options_before_matcherConsoler::Matcher (private)

Match remaining args against the optionals map

Returns:



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/consoler/matcher.rb', line 265

def _match_arguments_options_before_matcher
  # 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 then
    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 then
      total += before.size

      before.each do |val|
        val[:included] = true;
      end
    end
  end

  self
end

#_peek_nextString? (private)

Peek at the next argument

Only useful inside #_loop_args

Returns:

  • (String, nil)


168
169
170
# File 'lib/consoler/matcher.rb', line 168

def _peek_next
  @arguments.args[@index + 1]
end

#_skip_nextnil, Consoler::Matcher (private)

Skip to the next argument

Useful if you use a peeked argument

Returns:



178
179
180
181
182
# File 'lib/consoler/matcher.rb', line 178

def _skip_next
  @index += 1

  self
end

#matchHash?

Match arguments against options

Returns:

  • (Hash, nil)

    Matched information, or nil is returned when there was no match



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
# File 'lib/consoler/matcher.rb', line 26

def match
  parse_options = true

  _loop_args do |arg|
    unless parse_options then
      @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 == '--' then
      parse_options = false
      next
    end

    analyzed = _analyze arg

    if analyzed.nil?
      return nil
    end
  end

  remaining = _match_arguments
  _fill_defaults

  if @matched_options.size == @options.size then
    @matched_options['remaining'] = remaining
    return @matched_options
  end

  return nil
end