squared 0.6.5 → 0.6.6

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.
@@ -45,15 +45,15 @@ module Squared
45
45
  end
46
46
  ret = out
47
47
  end
48
- if escape || quote
49
- ret.map! do |val|
50
- if opt?(val)
51
- val
52
- elsif escape
53
- shell_escape(val, quote: quote, double: double)
54
- else
55
- shell_quote(val, force: force, double: double)
56
- end
48
+ ret.map! do |val|
49
+ next val if opt?(val)
50
+
51
+ if quote || val.is_a?(Pathname)
52
+ shell_quote(val, force: force, double: double)
53
+ elsif escape
54
+ shell_escape(val, quote: quote, double: double)
55
+ else
56
+ val
57
57
  end
58
58
  end
59
59
  if target.is_a?(Set)
@@ -70,7 +70,7 @@ module Squared
70
70
  kwargs[:subject] ||= target.first.stripext
71
71
  kwargs[:hint] ||= 'unrecognized'
72
72
  append(target, opts, delim: true) if kwargs.delete(:append)
73
- warn log_message(Logger::WARN, opts.join(', '), pass: true, **kwargs)
73
+ warn log_warn(opts.join(', '), pass: true, **kwargs)
74
74
  exit 1 unless pass || confirm("Run? [#{sub_style(target, styles)}]", 'N')
75
75
  end
76
76
 
@@ -87,10 +87,9 @@ module Squared
87
87
  end
88
88
 
89
89
  def strip(val)
90
- return [] unless val
91
-
92
90
  val = shell_split val if val.is_a?(String)
93
- val.map { |s| s.sub(OPT_SINGLE, '\1=\2').sub(OPT_VALUE, '\1=\2').sub(OPT_NAME, '\2') }.reject(&:empty?)
91
+ Array(val).map { |s| s.sub(OPT_SINGLE, '\1=\2').sub(OPT_VALUE, '\1=\2').sub(OPT_NAME, '\2') }
92
+ .reject(&:empty?)
94
93
  end
95
94
 
96
95
  def select(list, bare: true, no: true, single: false, double: false)
@@ -116,6 +115,15 @@ module Squared
116
115
  list
117
116
  end
118
117
 
118
+ def parse_arg!(name, val)
119
+ return unless val.is_a?(String)
120
+
121
+ a, b = name.size == 1 ? %w[- *] : %w[(?:--) +]
122
+ return unless val =~ /\A#{a}?#{Regexp.escape(name)}(=|\s#{b})(["'])?(.+)(?(2)\2\z|\z)/m
123
+
124
+ [name, $3, $2 || (name.size == 1 && $1.empty? ? true : '')]
125
+ end
126
+
119
127
  def arg?(target, *args, value: false, **)
120
128
  r, s = args.partition { |val| val.is_a?(Regexp) }
121
129
  r << matchopts(s, value) unless s.empty?
@@ -133,7 +141,7 @@ module Squared
133
141
  end
134
142
 
135
143
  def pattern?(val)
136
- val.match?(/(?:\A\^|\$\z)/) || val.match?(/(?:\.[*+]|\(\?:|\\[dsw]|\[.+\]|\{\d+,?\d*})/)
144
+ val.match?(/\A\^|\$\z/) || val.match?(/[.)][*+?]|\(\?:|\\[dsw\d]|\[.+\]|\{\d+,?\d*\}/i)
137
145
  end
138
146
 
139
147
  private
@@ -144,7 +152,7 @@ module Squared
144
152
 
145
153
  def matchopts(list, value = false)
146
154
  a, b = Array(list).partition { |val| val.size == 1 || val.match?(OPT_SINGLE) }
147
- return /\A#{shortopt(*a)}}/ if b.empty?
155
+ return /\A#{shortopt(*a)}/ if b.empty?
148
156
  return /\A#{longopt(*b, value)}/ if a.empty?
149
157
 
150
158
  /\A(?:#{shortopt(*a)}|#{longopt(*b, value)})/
@@ -175,7 +183,7 @@ module Squared
175
183
  def_delegator :@extras, :find_all, :detect_all
176
184
  def_delegator :@extras, :find_index, :detect_index
177
185
 
178
- def initialize(opts, list, target = Set.new, project: nil, path: nil, sep: '=', **kwargs, &blk)
186
+ def initialize(opts, list, target = JoinSet.new, project: nil, path: nil, sep: '=', **kwargs, &blk)
179
187
  @target = target.is_a?(Set) ? target : target.to_set
180
188
  @project = project
181
189
  @path = path || project&.path
@@ -185,7 +193,8 @@ module Squared
185
193
  parse(list, opts, **kwargs, &blk)
186
194
  end
187
195
 
188
- def parse(list, opts = extras, no: nil, single: nil, args: false, first: nil, underscore: nil, &blk)
196
+ def parse(list, opts = extras, no: nil, single: nil, args: false, multiple: nil, first: nil, underscore: nil,
197
+ stdin: false, &blk)
189
198
  @extras = []
190
199
  @values = []
191
200
  bare = []
@@ -267,7 +276,10 @@ module Squared
267
276
  ml.concat(tr.call(ml))
268
277
  no.concat(tr.call(no))
269
278
  end
270
- target.multiple = ml.map { |val| val.size == 1 ? "-#{val}" : "--#{val}" } if target.is_a?(JoinSet)
279
+ if target.is_a?(JoinSet)
280
+ target.multiple = multiple if multiple
281
+ target.multiple = ml.map { |val| val.size == 1 ? "-#{val}" : "--#{val}" }
282
+ end
271
283
  numtype = [
272
284
  [i, /\A\d+\z/],
273
285
  [f, /\A\d*(?:\.\d+)?\z/],
@@ -276,6 +288,16 @@ module Squared
276
288
  numcheck = ->(k, v) { numtype.any? { |flag, pat| flag.include?(k) && v.match?(pat) } }
277
289
  skip = false
278
290
  opts.each do |opt|
291
+ if stdin
292
+ if stdin == -1
293
+ add_path opt if exist?(opt)
294
+ next
295
+ elsif opt == '-'
296
+ add '-'
297
+ stdin = -1
298
+ next
299
+ end
300
+ end
279
301
  next skip = true if opt == '--'
280
302
  next push opt if skip
281
303
 
@@ -339,18 +361,30 @@ module Squared
339
361
  self
340
362
  end
341
363
 
342
- def append_any(*args, quote: true, **kwargs)
364
+ def append_any(*args, escape: false, quote: true, **kwargs)
343
365
  (args.empty? ? extras : args.flatten).each do |val|
344
- val = yield val if block_given?
366
+ if block_given?
367
+ temp = val
368
+ val = yield val
369
+ if val.is_a?(Array)
370
+ found << temp
371
+ k, v, q = val
372
+ add_option(k, v, escape: escape, quote: quote, double: q == '"', merge: q == true, **kwargs)
373
+ next
374
+ end
375
+ end
345
376
  next unless val.is_a?(String)
346
377
 
347
378
  if exist?(val)
348
379
  add_path(val, **kwargs)
349
380
  elsif quote
350
381
  add_quote(val, **kwargs)
382
+ elsif escape
383
+ add shell_escape(val, **kwargs)
351
384
  else
352
385
  add val
353
386
  end
387
+ found << (temp || val) if args.empty?
354
388
  end
355
389
  self
356
390
  end
@@ -451,20 +485,26 @@ module Squared
451
485
  else
452
486
  args.each { |val| remove val }
453
487
  end
454
- args = temp[0...index] + args + temp[index..-1]
488
+ args = temp[0, index] + args + temp[index..-1]
455
489
  target.clear
456
490
  end
457
491
  merge args
458
492
  self
459
493
  end
460
494
 
461
- def add_path(*args, force: true, double: false, **kwargs)
495
+ def add_path(*args, option: nil, force: true, double: false, **kwargs)
462
496
  if args.empty?
463
497
  args = select { |val| val.is_a?(String) }
498
+ found.concat(args)
464
499
  args.map! { |val| path + val } if path
465
500
  append(args, force: force, **kwargs)
466
501
  else
467
- add shell_quote(path ? path.join(*args) : File.join(*args), option: false, force: force, double: double)
502
+ val = path ? path.join(*args) : File.join(*args)
503
+ if option
504
+ add quote_option(option, val, double: double)
505
+ else
506
+ add shell_quote(val, option: false, force: force, double: double)
507
+ end
468
508
  end
469
509
  self
470
510
  end
@@ -480,25 +520,35 @@ module Squared
480
520
  self
481
521
  end
482
522
 
483
- def add_first(fallback = nil, prefix: nil, path: false, quote: false, reverse: false, expect: false, **kwargs)
523
+ def add_first(fallback = nil, prefix: nil, path: false, escape: false, quote: false, reverse: false,
524
+ expect: false, **kwargs)
484
525
  val = (reverse ? pop : shift) || fallback
485
526
  if val
486
- val.delete_prefix!(prefix) if prefix && val.is_a?(String)
527
+ temp = val
528
+ val = val.delete_prefix(prefix) if prefix && val.is_a?(String)
487
529
  unless block_given? && !(val = yield val).is_a?(String)
488
530
  if path
489
531
  add_path(val, **kwargs)
490
532
  elsif quote
491
533
  add_quote(val, **kwargs)
534
+ elsif escape
535
+ add shell_escape(val, **kwargs)
492
536
  else
493
537
  add val
494
538
  end
539
+ found << temp unless temp == fallback
495
540
  end
496
541
  elsif expect
497
- raise(expect.is_a?(String) ? expect : 'no value to add')
542
+ raise(expect.is_a?(String) ? expect : 'no value queued')
498
543
  end
499
544
  self
500
545
  end
501
546
 
547
+ def delim
548
+ add '--'
549
+ self
550
+ end
551
+
502
552
  def last(val = nil, &blk)
503
553
  unless block_given?
504
554
  case val
@@ -532,26 +582,26 @@ module Squared
532
582
  end
533
583
 
534
584
  def splice(*exclude, quote: true, delim: true, path: false, pattern: false, &blk)
535
- found, other = if block_given?
536
- partition(&blk)
537
- elsif exclude.first.is_a?(Symbol)
538
- partition(&exclude.first)
539
- else
540
- partition do |val|
541
- next false if pattern && OptionPartition.pattern?(val)
542
-
543
- exclude.none? { |pat| val.match?(Regexp.new(pat)) }
544
- end
545
- end
546
- unless found.empty?
585
+ temp, other = if block_given?
586
+ partition(&blk)
587
+ elsif exclude.first.is_a?(Symbol)
588
+ partition(&exclude.first)
589
+ else
590
+ partition do |val|
591
+ next false if pattern && OptionPartition.pattern?(val)
592
+
593
+ exclude.none? { |pat| val.match?(Regexp.new(pat)) }
594
+ end
595
+ end
596
+ unless temp.empty?
547
597
  add '--' if delim
548
598
  extras.clear
549
599
  concat other
550
600
  if path
551
- found.each { |val| add_path(val) }
601
+ temp.each { |val| add_path(val) }
552
602
  else
553
- found.quote! if quote
554
- merge found
603
+ temp.quote! if quote
604
+ merge temp
555
605
  end
556
606
  end
557
607
  self
@@ -625,7 +675,8 @@ module Squared
625
675
  end
626
676
 
627
677
  def with_glob?(val, glob = true)
628
- return false unless path && val.is_a?(String) && !val.empty?
678
+ return false unless val.is_a?(String) && !val.empty?
679
+ return File.exist?(val) unless path
629
680
 
630
681
  path.join(val).exist? || (glob && !path.glob(val).empty?)
631
682
  end
@@ -162,7 +162,9 @@ module Squared
162
162
  end
163
163
 
164
164
  def reset
165
- @data = {}.tap { |data| (TASK_BASE + TASK_KEYS).each { |key| data[key] = [] } }
165
+ @data = {}
166
+ (TASK_BASE + TASK_KEYS).each { |key| @data[key] = [] }
167
+ @data
166
168
  end
167
169
 
168
170
  def name_get(key)
@@ -11,8 +11,9 @@ module Squared
11
11
  if id.is_a?(Symbol)
12
12
  project id
13
13
  else
14
- values.find { |proj| proj.name == id } || values.find { |proj| proj.project == id } ||
15
- values.find { |proj| proj.path.to_s == File.expand_path(id, base) }
14
+ values.find { |proj| proj.name == id } ||
15
+ values.find { |proj| proj.project == id } ||
16
+ values.find { |proj| proj.path.to_s == File.expand_path(id, base) }
16
17
  end
17
18
  end
18
19
  ret.size == 1 ? ret.first : ret
@@ -20,7 +21,7 @@ module Squared
20
21
 
21
22
  def expect(name)
22
23
  ret = project name
23
- return ret if ret&.path&.directory?
24
+ return ret if ret&.path&.directory? && !ret.path.empty?
24
25
 
25
26
  raise NoMethodError, "project is not initialized (#{name})"
26
27
  end
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.6.5
4
+ version: 0.6.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - An Pham