Class: Squared::Workspace::Project::Support::OptionPartition

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Prompt, Shell, Common::Format
Includes:
Common::Shell
Defined in:
lib/squared/workspace/project/support/optionpartition.rb

Constant Summary

Constants included from Common

Common::ARG, Common::PATH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Format

emphasize, enable_aixterm, enable_drawing, message

Methods included from Common::Shell

basic_option, fill_option, line_width, quote_option, shell_bin, shell_escape, shell_option, shell_parse, shell_quote, shell_split

Constructor Details

#initialize(opts, list, target = JoinSet.new, project: nil, path: nil, strict: false, sep: '=', **kwargs, &blk) ⇒ OptionPartition

Returns a new instance of OptionPartition.



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 219

def initialize(opts, list, target = JoinSet.new, project: nil, path: nil, strict: false, sep: '=', **kwargs,
               &blk)
  @target = target.is_a?(Set) ? target : target.to_set
  @project = project
  @path = path || project&.path
  @strict = strict
  @sep = sep
  @errors = []
  @found = []
  parse(list, opts, **kwargs, &blk)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



205
206
207
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 205

def errors
  @errors
end

#extrasObject (readonly)

Returns the value of attribute extras.



205
206
207
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 205

def extras
  @extras
end

#foundObject (readonly)

Returns the value of attribute found.



205
206
207
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 205

def found
  @found
end

#pathObject (readonly)

Returns the value of attribute path.



205
206
207
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 205

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



205
206
207
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 205

def project
  @project
end

#sepObject (readonly)

Returns the value of attribute sep.



205
206
207
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 205

def sep
  @sep
end

#targetObject (readonly)

Returns the value of attribute target.



205
206
207
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 205

def target
  @target
end

#valuesObject (readonly)

Returns the value of attribute values.



205
206
207
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 205

def values
  @values
end

Class Method Details

.append(target, *args, delim: false, escape: false, quote: true, strip: nil, double: false, filter: nil, pass: nil, **kwargs) ⇒ Object



23
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
65
66
67
68
69
70
71
72
73
74
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 23

def append(target, *args, delim: false, escape: false, quote: true, strip: nil, double: false, filter: nil,
           pass: nil, **kwargs)
  return if (ret = args.flatten(1)).empty?

  target << '--' if delim && !target.include?('--')
  if strip
    pat, s = Array(strip)
    ret.map! do |val|
      next val unless val.is_a?(String)

      if pat.is_a?(String) && pat.size == 1 && s.nil?
        val.delete_prefix(pat)
      else
        val.gsub(pat, s || '')
      end
    end
  end
  if filter
    ret, err = ret.partition { |val| filter.match?(val.to_s) }
    pass&.concat(err)
  end
  if block_given?
    ret = ret.each_with_object([]) do |val, out|
      arg = yield val
      case arg
      when String
        out << arg
      when nil, false
        pass&.push(val)
      else
        out << val
      end
    end
  end
  ret.map! do |val|
    next val if opt?(val)

    if !(pa = val.is_a?(Pathname)) && escape
      shell_escape(val, quote: quote, double: double)
    elsif quote || pa
      shell_quote(val, double: double, **kwargs)
    else
      val
    end
  end
  if target.is_a?(Set)
    target.merge(ret)
  else
    target.concat(ret)
  end
  ret
end

.arg?(target, *args, value: false) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
164
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 156

def arg?(target, *args, value: false)
  r, s = args.partition { |val| val.is_a?(Regexp) }
  r << matchopts(s, value) unless s.empty?
  a = target.to_a
  if (n = a.index('--'))
    a = a[0..n]
  end
  r.any? { |pat| a.any?(pat) }
end

.clear(target, opts, pass: true, strict: false, styles: nil, **kwargs) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 76

def clear(target, opts, pass: true, strict: false, styles: nil, **kwargs)
  return if opts.empty?

  kwargs[:subject] ||= target.first.stripext
  kwargs[:hint] ||= 'unrecognized'
  append(target, opts, delim: true) if kwargs.delete(:append)
  warn log_warn(opts.join(', '), pass: true, **kwargs)
  exit 1 unless !strict && (pass || confirm("Run? [#{sub_style(target, styles)}]", 'N'))
end

.delete_key(target, *args, value: false, reverse: false, count: -1)) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 86

def delete_key(target, *args, value: false, reverse: false, count: -1)
  args.each_with_object([]) do |val, out|
    next if (opts = target.grep(matchopt(val, value))).empty?

    opts = opts.first(count) if count >= 0
    opts.send(reverse ? :reverse_each : :each) { |key| target.delete(key) }
    out.concat(opts)
  end
end

.opt?(val) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 166

def opt?(val)
  return false unless val.is_a?(String)

  val.start_with?('-') && (OPT_NAME.match?(val) || OPT_VALUE.match?(val) || OPT_SINGLE.match?(val))
end

.parse_arg!(name, val) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 147

def parse_arg!(name, val)
  return unless val.is_a?(String)

  a, b = name.size == 1 ? %w[- *] : %w[(?:--) +]
  return unless val =~ /\A#{a}?#{Regexp.escape(name)}(=|\s#{b})(["'])?(.+)(?(2)\2\z|\z)/m

  [name, $3, $2 || (name.size == 1 && $1.empty? ? true : '')]
end

.parse_option(val) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 135

def parse_option(val)
  val = val.strip
  return [val, nil, ''] unless (i = val.index('='))

  last = val[i.succ..-1].strip
  if last =~ /\A(["'])(.*)\1\z/m
    last = $2
    quote = $1
  end
  [val[0..i.pred], last, quote || '']
end

.pattern?(val) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
175
176
177
178
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 172

def pattern?(val)
  Regexp.new(val)
  val.start_with?('\A', '^') || val.end_with?('\z', '$') ||
  val.match?(/[.)][*+?]|\(\?[:=!><]|\\[dsw\d]|\[.+\]|\{\d+,?\d*\}/i)
rescue RegexpError
  false
end

.select(list, bare: true, no: true, single: false, double: false) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 102

def select(list, bare: true, no: true, single: false, double: false)
  ret = bare ? list.grep_v(/=/) : list.grep(/=/).map { |val| val.split('=', 2).first }
  ret.map! { |val| val.split('|', 2).last }
  ret = ret.grep_v(/\Ano-/) unless no
  return ret if single == double

  ret.select { |val| single ? val.size == 1 : val.size > 1 }
end

.strip(val) ⇒ Object



96
97
98
99
100
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 96

def strip(val)
  val = shell_split val if val.is_a?(String)
  Array(val).map { |s| s.sub(OPT_SINGLE, '\1=\2').sub(OPT_VALUE, '\1=\2').sub(OPT_NAME, '\2') }
            .reject(&:empty?)
end

.union(list, other) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 111

def union(list, other)
  base = list.map { |val| parse_option(val).first }
  other.each do |val|
    next if base.include?(parse_option(val).first)

    list << val
  end
  list
end

.uniq!(list, pass: []) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 121

def uniq!(list, pass: [])
  keys = {}
  list.each_with_index do |val, i|
    j = val =~ OPT_VALUE ? $1 : val
    (keys[j] ||= []) << i unless pass.include?(j)
  end
  data = keys.map { |item| item[1].size > 1 ? item[1][0..-2] : [] }.reject(&:empty?)
  return if data.empty?

  data.each { |key| key.each { |i| list[i] = nil } }
  list.compact!
  list
end

Instance Method Details

#add_first(fallback = nil, prefix: nil, path: false, escape: false, reverse: false, expect: false, strip: nil, **kwargs) ⇒ Object



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 587

def add_first(fallback = nil, prefix: nil, path: false, escape: false, reverse: false, expect: false,
              strip: nil, **kwargs)
  val = (reverse ? pop : shift) || fallback
  if val
    temp = val
    if val.is_a?(String)
      val = val.delete_prefix(prefix) if prefix
      if strip
        pat, s = Array(strip)
        val = if pat.is_a?(String) && pat.size == 1 && s.nil?
                val.delete_prefix(pat)
              else
                val.gsub(pat, s || '')
              end
      end
    end
    unless block_given? && !(val = yield val).is_a?(String)
      if path
        add_path(val, **kwargs)
      elsif escape
        add shell_escape(val, **kwargs)
      elsif kwargs[:quote]
        add_quote(val, **kwargs)
      else
        add val
      end
      found << temp unless temp == fallback
    end
  elsif expect
    raise(expect.is_a?(String) ? expect : 'no value queued')
  end
  self
end

#add_option(flag, val = nil, **kwargs) ⇒ Object



582
583
584
585
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 582

def add_option(flag, val = nil, **kwargs)
  add shell_option(flag, val, **kwargs)
  self
end

#add_path(*args, option: nil, force: true, double: false, **kwargs) ⇒ Object



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 559

def add_path(*args, option: nil, force: true, double: false, **kwargs)
  if args.empty?
    list = grep(String)
    found.concat(list)
    list.map! { |val| path + val } if path
    append(list, force: force, **kwargs)
  else
    val = path ? path.join(*args) : File.join(*args)
    if option
      add quote_option(option, val, double: double)
    else
      add shell_quote(val, option: false, force: force, double: double)
    end
  end
  self
end

#add_quote(*args, **kwargs) ⇒ Object



576
577
578
579
580
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 576

def add_quote(*args, **kwargs)
  kwargs.delete(:quote)
  merge(args.compact.map { |s| s == '--' || OptionPartition.opt?(s) ? s : shell_quote(s, **kwargs) })
  self
end

#adjoin(*args, with: nil, start: false) ⇒ Object



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 511

def adjoin(*args, with: nil, start: false)
  index = -1
  temp = compact
  if with
    pat = case with
          when String, Symbol
            /\A#{Regexp.escape(with)}\z/
          when Array
            OptionPartition.send(:matchopts, with)
          else
            with
          end
    temp.each_with_index do |val, i|
      if val.to_s.match?(pat)
        index = i + (start.is_a?(Numeric) ? start : 1)
        break
      end
    end
  else
    temp.each_with_index do |val, i|
      if index == 0
        next unless val.is_a?(String) && val.start_with?('-')

        index = i
        break
      elsif i > 0 && !val.to_s.start_with?('-')
        if start
          index = i + (start.is_a?(Numeric) ? start : 1)
          break
        end
        index = 0
      end
    end
  end
  if index > 0
    if args.empty?
      args = dup
      reset
    else
      args.each { |val| remove val }
    end
    args = temp[0, index] + args + temp[index..-1]
    target.clear
  end
  merge args
  self
end

#append(*args, clear: false, preserve: '"', **kwargs, &blk) ⇒ Object



419
420
421
422
423
424
425
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 419

def append(*args, clear: false, preserve: '"', **kwargs, &blk)
  args = clear ? extras.dup.tap { extras.clear } : extras if args.empty?
  pass = kwargs[:pass] ||= []
  OptionPartition.append(target, *args, preserve: preserve, **kwargs, &blk)
  errors.concat(pass)
  self
end

#append?(key, val = nil, type: nil, force: false, sep: '=', **kwargs) ⇒ Boolean

Returns:

  • (Boolean)


717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 717

def append?(key, val = nil, type: nil, force: false, sep: '=', **kwargs)
  return false unless force || !arg?(key)

  val = yield self if block_given?
  return false unless val

  type ||= :quote if kwargs.empty?
  add case type
      when :quote
        quote_option(key, val, sep: sep)
      when :basic
        basic_option(key, val, sep: sep)
      else
        shell_option(key, val, sep: sep, **kwargs)
      end
  true
end

#append_any(*args, escape: false, **kwargs) ⇒ Object



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 427

def append_any(*args, escape: false, **kwargs)
  quote = kwargs.fetch(:quote, true)
  (args.empty? ? extras : args.flatten(1)).each do |val|
    if block_given?
      temp = val
      val = yield val
      if val.is_a?(Array)
        found << temp
        k, v, q = val
        add_option(k, v, escape: escape, double: q == '"', merge: q == true, **kwargs)
        next
      end
    end
    next unless val.is_a?(String)

    if exist?(val)
      add_path(val, **kwargs)
    elsif escape
      add shell_escape(val, **kwargs)
    elsif quote
      add_quote(val, **kwargs)
    else
      add val
    end
    found << (temp || val) if args.empty?
  end
  self
end

#arg?(*args, **kwargs) ⇒ Boolean

Returns:

  • (Boolean)


735
736
737
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 735

def arg?(*args, **kwargs)
  OptionPartition.arg?(target, *args, **kwargs)
end

#clear(opts = nil, errors: false, strict: @strict, **kwargs) ⇒ Object



498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 498

def clear(opts = nil, errors: false, strict: @strict, **kwargs)
  styles = project.theme[:inline] if project
  if errors
    OptionPartition.clear(target, @errors, strict: strict, styles: styles, **kwargs)
    @errors.clear
    return self unless opts
  end
  opts ||= extras
  OptionPartition.clear(target, opts - found, strict: strict, styles: styles, **kwargs)
  opts.clear
  self
end

#delete_key(*args, **kwargs) ⇒ Object



456
457
458
459
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 456

def delete_key(*args, **kwargs)
  OptionPartition.delete_key(target, *args, **kwargs)
  self
end

#delimObject



627
628
629
630
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 627

def delim
  add '--'
  self
end

#detachObject



621
622
623
624
625
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 621

def detach
  ret = extras
  @extras = []
  ret
end

#exist?(*args, add: false, first: false, last: false, glob: false) ⇒ Boolean

Returns:

  • (Boolean)


739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 739

def exist?(*args, add: false, first: false, last: false, glob: false)
  return !args.first.nil? && with_glob?(File.join(*args), glob) unless args.empty?

  if first || last
    return false unless (val = first ? self.first : self.last)

    with_glob?(val, glob).tap do |ret|
      next unless add && ret

      add_first(path: true, reverse: !first)
    end
  else
    each_with_index do |val, i|
      next unless with_glob?(val, glob)

      if add
        remove_at i
        add_path val
      end
      return true
    end
    false
  end
end

#last(val = nil, &blk) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 632

def last(val = nil, &blk)
  unless block_given?
    case val
    when nil
      return extras.last
    when Numeric
      return extras.last(val)
    when String, Array, Regexp
      val = OptionPartition.send(:matchopts, val) unless val.is_a?(Regexp)
      blk = proc { |s| s&.match?(val) }
    else
      raise TypeError, Common::Format.message('unknown', val, hint: target.first)
    end
  end
  ret = find_all(&blk)
  unless ret.empty?
    ret = case val
          when nil
            ret.first(1)
          when Numeric
            ret.first(val)
          else
            ret
          end
    ret.each do |opt|
      delete opt
      add opt
    end
  end
  val.nil? ? ret.first : ret
end

#parse(list, opts = extras, no: nil, single: nil, args: false, multiple: nil, first: nil, underscore: nil, stdin: false, &blk) ⇒ Object



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
261
262
263
264
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 231

def parse(list, opts = extras, no: nil, single: nil, args: false, multiple: nil, first: nil, underscore: nil,
          stdin: false, &blk)
  @extras = []
  @values = []
  bare = []
  e = []
  b = []
  m = []
  p = []
  q = []
  qq = []
  i = []
  f = []
  si = []
  bl = []
  ml = []
  sw = {}
  se = {}
  list.flat_map do |val|
    x, y = val.split('|', 2)
    if y
      if (n = val.index('='))
        x += val[n..-1]
      end
      [x, y]
    else
      x
    end
  end
  .each do |val|
    if val.size > 1 && val =~ /^\{([^},]*)(?:,([^}]+))?\}(.+)$/
      op1 = $1 unless $1.empty?
      op2 = $2
      val = $3
    end
    if (n = val.index('='))
      flag = val[0, n]
      enum = flag.split(':')
      mod = if enum.size > 1
              pre = enum.shift
              enum.map { |s| "#{pre}:#{s}" }
            else
              [flag]
            end
      case val[n.succ]
      when 'e'
        e.concat(mod)
      when 'b'
        b.concat(mod)
      when 'm'
        m.concat(mod)
      when 'q'
        qq.concat(mod) if val[n + 2] == 'q'
        q.concat(mod)
      when 'p'
        p.concat(mod)
      when 'i'
        i.concat(mod)
      when 'f'
        f.concat(mod)
      when 'n'
        si.concat(mod)
      when 'v'
        @values.concat(mod.map { |s| Regexp.escape(s) })
      when '!'
        bl.concat(mod)
      when '+'
        ml.concat(mod)
        bare.concat(mod)
      else
        next
      end
      m.concat(mod) if val[n + 2] == 'm'
      bare.concat(mod) if val.end_with?('?')
    else
      bare << val
      mod = [val]
    end
    mod.each do |key|
      sw[key] = op1 if op1
      se[key] = op2 if op2
    end
  end
  no = (no || []).map { |val| (n = val.index('=')) ? val[0, n] : val }
  bare.concat(no)
  if underscore
    tr = ->(a) { a.map { |val| val.tr('-', '_') } }
    @values.concat(tr.call(@values))
    bare.concat(tr.call(bare))
    e.concat(tr.call(e))
    b.concat(tr.call(b))
    m.concat(tr.call(m))
    p.concat(tr.call(p))
    q.concat(tr.call(q))
    qq.concat(tr.call(qq))
    i.concat(tr.call(i))
    f.concat(tr.call(f))
    si.concat(tr.call(si))
    bl.concat(tr.call(bl))
    ml.concat(tr.call(ml))
    no.concat(tr.call(no))
  end
  if target.is_a?(JoinSet)
    target.multiple = multiple if multiple
    target.multiple = ml.map { |val| val.size == 1 ? "-#{val}" : "--#{val}" }
  end
  numtype = [
    [i, /\A\d+\z/],
    [f, /\A\d*(\.\d+)?\z/],
    [si, /\A-?\d+\z/]
  ].freeze
  numcheck = ->(k, v) { numtype.any? { |flag, pat| flag.include?(k) && v.match?(pat) } }
  skip = false
  opts.each do |opt|
    if stdin
      if stdin == -1
        add_path opt if exist?(opt)
        next
      elsif opt == '-'
        add '-'
        stdin = -1
        next
      end
    end
    next skip = true if opt == '--'
    next push opt if skip

    if single&.match?(opt)
      add "-#{opt}"
    elsif bare.include?(opt)
      if sw[opt]
        add "#{sw[opt]}#{opt}"
      else
        add(opt.size == 1 ? "-#{opt}" : "--#{opt}")
      end
    elsif opt.start_with?(/no[-_]/) && no.include?(s = opt[3..-1])
      add "--no-#{s}"
    else
      if opt =~ OPT_VALUE
        key = $1
        val = $2
        has = lambda do |a|
          return true if a.include?(key)
          return false unless (k = a.find { |s| s.end_with?(':*') })

          key.start_with?(k.chomp('*')) && !key.end_with?(':')
        end
        kwargs = { sep: se[key] || sep, merge: has.call(m), switch: sw[key] }
        if has.call(e) || (has.call(bl) && %w[true false].include?(val))
          add shell_option(key, val, **kwargs)
        elsif has.call(q)
          add quote_option(key, val, double: has.call(qq), **kwargs)
        elsif has.call(p)
          if val.match?(/\A(["']).+\1\z/)
            add shell_option(key, val, escape: false, **kwargs)
          elsif path
            add quote_option(key, path + val, **kwargs)
          else
            push opt
          end
        elsif kwargs[:merge] || has.call(b) || numcheck.call(key, val) || se[key] || sw[key]
          add basic_option(key, val, **kwargs)
        else
          push opt
        end
        opt = key
      else
        push opt
        skip = true if args
      end
      skip = true if first&.any? { |pat| pat.is_a?(Regexp) ? opt.match?(pat) : !opt.include?(pat) }
    end
  end
  @values = @values.empty? ? /\A\s+\z/ : /\A(#{@values.join('|')})#{sep}(.+)\z/m
  @extras.each_with_index(&blk) if block_given?
  self
end

#readline(msg, fallback = nil, option: nil, quote: false, force: true, double: false) ⇒ Object



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 690

def readline(msg, fallback = nil, option: nil, quote: false, force: true, double: false)
  begin
    ret = Common::Prompt.reline.readline("#{msg}#{force ? ':' : '?'} ", false).strip
  rescue Interrupt
    exit(force ? 1 : 0)
  else
    exit 1 if ret.empty? && !(ret = fallback) && force
  end
  return unless ret

  if option
    add quote_option(option, ret, double: double)
  elsif quote
    add shell_quote(ret, option: false, double: double)
  else
    add ret
  end
  ret
end

#reset(errors: false) ⇒ Object



710
711
712
713
714
715
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 710

def reset(errors: false)
  extras.clear
  found.clear
  clear(errors: true) if errors
  self
end

#splice(*exclude, quote: true, delim: true, path: false, pattern: false, &blk) ⇒ Object



664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 664

def splice(*exclude, quote: true, delim: true, path: false, pattern: false, &blk)
  temp, other = if block_given?
                  partition(&blk)
                else
                  exclude.map! { |pat| Regexp.new(pat) }
                  partition do |val|
                    val = val.to_s
                    next if pattern && OptionPartition.pattern?(val)

                    exclude.none? { |pat| val.match?(pat) }
                  end
                end
  unless temp.empty?
    add '--' if delim
    extras.clear
    concat other
    if path
      temp.each { |val| add_path(val) }
    else
      temp.quote! if quote
      merge temp
    end
  end
  self
end

#swap(opts = nil, &blk) ⇒ Object



409
410
411
412
413
414
415
416
417
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 409

def swap(opts = nil, &blk)
  unless opts
    opts = found
    @found = []
  end
  opts.sort!(&blk) if block_given?
  @extras = opts
  self
end

#uniq(list) ⇒ Object



493
494
495
496
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 493

def uniq(list)
  ignore = map { |val| nameonly(val) }
  list.reject { |val| ignore.include?(s = nameonly(val)) || any?(OptionPartition.send(:matchopt, s)) }
end

#uniq!(list) ⇒ Object



764
765
766
767
768
769
770
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 764

def uniq!(list)
  list = uniq list
  return if list.empty?

  concat list
  self
end

#values_of(*args, strict: true, first: false, last: false) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/squared/workspace/project/support/optionpartition.rb', line 461

def values_of(*args, strict: true, first: false, last: false)
  eq, s = strict ? [sep, '[^ ]+'] : ['(?:=| +)', '[^-][^ ]*']
  g = ["\"((?:[^\"]|(?<=\\\\)\"(?!$#{'| ' if windows?}))*)\""]
  g << "'((?:[^']|'\\\\'')*)'" unless windows?
  g << "(#{s})"
  list = args.map do |opt|
    if opt.size == 1
      /(?:\A| )-#{opt} ?([^ ]+)/
    else
      /(?:\A| )--#{opt + eq}(?:#{g.join('|')})/
    end
  end
  ret = []
  target.each do |opt|
    list.each do |pat|
      next unless opt =~ pat

      ret << ($1 || $2 || $3)
      break
    end
  end
  return ret unless first || last

  if last.is_a?(Numeric)
    ret.last(last)
  elsif last
    ret.last
  else
    first.is_a?(Numeric) ? ret.first(first) : ret.first
  end
end