squared 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,10 +49,10 @@ module Squared
49
49
  ret.map! do |val|
50
50
  next val if opt?(val)
51
51
 
52
- if quote || val.is_a?(Pathname)
53
- shell_quote(val, force: force, double: double)
54
- elsif escape
52
+ if !(pa = val.is_a?(Pathname)) && escape
55
53
  shell_escape(val, quote: quote, double: double)
54
+ elsif quote || pa
55
+ shell_quote(val, force: force, double: double)
56
56
  else
57
57
  val
58
58
  end
@@ -153,7 +153,7 @@ module Squared
153
153
 
154
154
  def pattern?(val)
155
155
  val.start_with?('\A', '^') || val.end_with?('\z', '$') ||
156
- val.match?(/[.)][*+?]|\(\?:|\\[dsw\d]|\[.+\]|\{\d+,?\d*\}/i)
156
+ val.match?(/[.)][*+?]|\(\?[:=!><]|\\[dsw\d]|\[.+\]|\{\d+,?\d*\}/i)
157
157
  end
158
158
 
159
159
  private
@@ -221,6 +221,8 @@ module Squared
221
221
  si = []
222
222
  bl = []
223
223
  ml = []
224
+ sw = {}
225
+ se = {}
224
226
  list.flat_map do |val|
225
227
  x, y = val.split('|', 2)
226
228
  if y
@@ -233,6 +235,11 @@ module Squared
233
235
  end
234
236
  end
235
237
  .each do |val|
238
+ if val.size > 1 && val =~ /^\{([^},]*)(?:,([^}]+))?\}(.+)$/
239
+ op1 = $1 unless $1.empty?
240
+ op2 = $2
241
+ val = $3
242
+ end
236
243
  if (n = val.index('='))
237
244
  flag = val[0, n]
238
245
  case val[n.succ]
@@ -265,9 +272,12 @@ module Squared
265
272
  end
266
273
  m << flag if val[n + 2] == 'm'
267
274
  bare << flag if val.end_with?('?')
275
+ val = flag
268
276
  else
269
277
  bare << val
270
278
  end
279
+ sw[val] = op1 if op1
280
+ se[val] = op2 if op2
271
281
  end
272
282
  no = (no || []).map { |val| (n = val.index('=')) ? val[0, n] : val }
273
283
  bare.concat(no)
@@ -316,30 +326,34 @@ module Squared
316
326
  if single&.match?(opt)
317
327
  add "-#{opt}"
318
328
  elsif bare.include?(opt)
319
- add(opt.size == 1 ? "-#{opt}" : "--#{opt}")
320
- elsif opt.start_with?(/no[-_]/) && no.include?(name = opt[3..-1])
321
- add "--no-#{name}"
329
+ if sw[opt]
330
+ add "#{sw[opt]}#{opt}"
331
+ else
332
+ add(opt.size == 1 ? "-#{opt}" : "--#{opt}")
333
+ end
334
+ elsif opt.start_with?(/no[-_]/) && no.include?(s = opt[3..-1])
335
+ add "--no-#{s}"
322
336
  else
323
337
  if opt =~ OPT_VALUE
324
338
  key = $1
325
339
  val = $2
326
340
  merge = m.include?(key)
327
- if e.include?(key)
328
- add shell_option(key, val, merge: merge, sep: sep)
341
+ switch = sw[key]
342
+ kwargs = { sep: se[key] || sep, merge: merge, switch: switch }
343
+ if e.include?(key) || (bl.include?(key) && %w[true false].include?(val))
344
+ add shell_option(key, val, **kwargs)
329
345
  elsif q.include?(key)
330
- add quote_option(key, val, double: qq.include?(key), merge: merge, sep: sep)
346
+ add quote_option(key, val, double: qq.include?(key), **kwargs)
331
347
  elsif p.include?(key)
332
348
  if val.match?(/\A(["']).+\1\z/)
333
- add shell_option(key, val, escape: false, merge: merge, sep: sep)
349
+ add shell_option(key, val, escape: false, **kwargs)
334
350
  elsif path
335
- add quote_option(key, path + val, merge: merge, sep: sep)
351
+ add quote_option(key, path + val, **kwargs)
336
352
  else
337
353
  push opt
338
354
  end
339
- elsif b.include?(key) || (bl.include?(key) && %w[true false].include?(val)) || numcheck.call(key, val)
340
- add basic_option(key, val, merge: merge, sep: sep)
341
- elsif merge
342
- add basic_option(key, val, merge: true, sep: sep)
355
+ elsif b.include?(key) || numcheck.call(key, val) || se[key] || merge || switch
356
+ add basic_option(key, val, **kwargs)
343
357
  else
344
358
  push opt
345
359
  end
@@ -348,7 +362,7 @@ module Squared
348
362
  push opt
349
363
  skip = true if args
350
364
  end
351
- skip = true if first&.any? { |s| s.is_a?(Regexp) ? opt.match?(s) : !opt.include?(s) }
365
+ skip = true if first&.any? { |pat| pat.is_a?(Regexp) ? opt.match?(pat) : !opt.include?(pat) }
352
366
  end
353
367
  end
354
368
  @values = @values.empty? ? /\A\s+\z/ : /\A(#{@values.join('|')})#{sep}(.+)\z/m
@@ -374,7 +388,8 @@ module Squared
374
388
  self
375
389
  end
376
390
 
377
- def append_any(*args, escape: false, quote: true, **kwargs)
391
+ def append_any(*args, escape: false, **kwargs)
392
+ quote = kwargs.fetch(:quote, true)
378
393
  (args.empty? ? extras : args.flatten(1)).each do |val|
379
394
  if block_given?
380
395
  temp = val
@@ -382,7 +397,7 @@ module Squared
382
397
  if val.is_a?(Array)
383
398
  found << temp
384
399
  k, v, q = val
385
- add_option(k, v, escape: escape, quote: quote, double: q == '"', merge: q == true, **kwargs)
400
+ add_option(k, v, escape: escape, double: q == '"', merge: q == true, **kwargs)
386
401
  next
387
402
  end
388
403
  end
@@ -390,10 +405,10 @@ module Squared
390
405
 
391
406
  if exist?(val)
392
407
  add_path(val, **kwargs)
393
- elsif quote
394
- add_quote(val, **kwargs)
395
408
  elsif escape
396
409
  add shell_escape(val, **kwargs)
410
+ elsif quote
411
+ add_quote(val, **kwargs)
397
412
  else
398
413
  add val
399
414
  end
@@ -507,7 +522,7 @@ module Squared
507
522
 
508
523
  def add_path(*args, option: nil, force: true, double: false, **kwargs)
509
524
  if args.empty?
510
- list = select { |val| val.is_a?(String) }
525
+ list = grep(String)
511
526
  found.concat(list)
512
527
  list.map! { |val| path + val } if path
513
528
  append(list, force: force, **kwargs)
@@ -523,6 +538,7 @@ module Squared
523
538
  end
524
539
 
525
540
  def add_quote(*args, **kwargs)
541
+ kwargs.delete(:quote)
526
542
  merge(args.compact.map { |s| s == '--' || OptionPartition.opt?(s) ? s : shell_quote(s, **kwargs) })
527
543
  self
528
544
  end
@@ -532,8 +548,8 @@ module Squared
532
548
  self
533
549
  end
534
550
 
535
- def add_first(fallback = nil, prefix: nil, path: false, escape: false, quote: false, reverse: false,
536
- expect: false, **kwargs)
551
+ def add_first(fallback = nil, prefix: nil, path: false, escape: false, reverse: false, expect: false,
552
+ **kwargs)
537
553
  val = (reverse ? pop : shift) || fallback
538
554
  if val
539
555
  temp = val
@@ -541,10 +557,10 @@ module Squared
541
557
  unless block_given? && !(val = yield val).is_a?(String)
542
558
  if path
543
559
  add_path(val, **kwargs)
544
- elsif quote
545
- add_quote(val, **kwargs)
546
560
  elsif escape
547
561
  add shell_escape(val, **kwargs)
562
+ elsif kwargs[:quote]
563
+ add_quote(val, **kwargs)
548
564
  else
549
565
  add val
550
566
  end
@@ -655,7 +671,7 @@ module Squared
655
671
  end
656
672
 
657
673
  def exist?(*args, add: false, first: false, last: false, glob: false)
658
- return with_glob?(File.join(*args), glob) unless args.empty?
674
+ return !args.first.nil? && with_glob?(File.join(*args), glob) unless args.empty?
659
675
 
660
676
  if first || last
661
677
  return false unless (val = first ? self.first : self.last)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squared
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - An Pham