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/class.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

enable_aixterm, enable_drawing

Methods included from Common::Shell

basic_option, fill_option, quote_option, shell_escape, shell_option, shell_quote, shell_split

Constructor Details

#initialize(opts, list, target = Set.new, project: nil, path: nil, **kwargs, &blk) ⇒ OptionPartition

Returns a new instance of OptionPartition.



67
68
69
70
71
72
73
74
# File 'lib/squared/workspace/project/support/class.rb', line 67

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



60
61
62
# File 'lib/squared/workspace/project/support/class.rb', line 60

def errors
  @errors
end

#extrasObject (readonly)

Returns the value of attribute extras.



60
61
62
# File 'lib/squared/workspace/project/support/class.rb', line 60

def extras
  @extras
end

#foundObject (readonly)

Returns the value of attribute found.



60
61
62
# File 'lib/squared/workspace/project/support/class.rb', line 60

def found
  @found
end

#pathObject (readonly)

Returns the value of attribute path.



60
61
62
# File 'lib/squared/workspace/project/support/class.rb', line 60

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



60
61
62
# File 'lib/squared/workspace/project/support/class.rb', line 60

def project
  @project
end

#targetObject (readonly)

Returns the value of attribute target.



60
61
62
# File 'lib/squared/workspace/project/support/class.rb', line 60

def target
  @target
end

#valuesObject (readonly)

Returns the value of attribute values.



60
61
62
# File 'lib/squared/workspace/project/support/class.rb', line 60

def values
  @values
end

Class Method Details

.append(target, *args, delim: false, escape: false, quote: true) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/squared/workspace/project/support/class.rb', line 18

def append(target, *args, delim: false, escape: false, quote: true, **)
  return if (ret = args.flatten).empty?

  target << '--' if delim && !target.include?('--')
  ret.map! { |val| escape ? shell_escape(val, quote: quote) : shell_quote(val) } if escape || quote
  if target.is_a?(Set)
    target.merge(ret)
  else
    target.concat(ret)
  end
  ret
end

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

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'lib/squared/workspace/project/support/class.rb', line 50

def arg?(target, *args, value: false, **)
  r, s = args.partition { |val| val.is_a?(Regexp) }
  unless s.empty?
    s.map! { |val| Regexp.escape(shell_option(val)) }
    r << /\A(?:#{s.join('|')})#{value ? '[ =].' : '(?: |=|\z)'}/
  end
  target.any? { |opt| r.any? { |val| opt&.match?(val) } }
end

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/squared/workspace/project/support/class.rb', line 31

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

  kwargs[:subject] ||= stripext(target.first)
  kwargs[:hint] ||= 'unrecognized'
  append(target, opts, delim: true) if kwargs.delete(:append)
  warn log_message(Logger::WARN, opts.join(', '), pass: true, **kwargs)
  return if pass || confirm("Run? [#{sub_style(target, styles: styles)}] [y/N] ", 'N')

  raise_error 'user cancelled'
end

.strip(val) ⇒ Object



43
44
45
46
47
48
# File 'lib/squared/workspace/project/support/class.rb', line 43

def strip(val)
  return [] unless val

  val = shell_split(val) if val.is_a?(String)
  val.map { |s| s.sub(/\A-([a-z\d])(.+)\z/i, '\1=\2').sub(/\A--?/, '') }.reject(&:empty?)
end

Instance Method Details

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



190
191
192
193
# File 'lib/squared/workspace/project/support/class.rb', line 190

def append(*args, **kwargs)
  OptionPartition.append(target, *(args.empty? ? extras : args), **kwargs)
  self
end

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

Returns:

  • (Boolean)


208
209
210
# File 'lib/squared/workspace/project/support/class.rb', line 208

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

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



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/squared/workspace/project/support/class.rb', line 195

def clear(opts = nil, errors: false, **kwargs)
  styles = project.theme[:inline] if project
  if !opts
    opts = errors ? @errors : @extras
  elsif errors
    OptionPartition.clear(target, @errors, styles: styles, **kwargs)
    @errors.clear
  end
  OptionPartition.clear(target, opts.reject { |val| found.include?(val) }, styles: styles, **kwargs)
  opts.clear
  self
end

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



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/squared/workspace/project/support/class.rb', line 76

def parse(list, opts = extras, no: nil, single: nil, args: false, first: nil, &blk)
  @extras = []
  @values = []
  bare = []
  e = []
  b = []
  m = []
  p = []
  q = []
  qq = []
  i = []
  f = []
  si = []
  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 (n = val.index('='))
      flag = val[0, n]
      case val[n + 1]
      when 'e'
        e << flag
      when 'b'
        b << flag
      when 'm'
        m << flag
      when 'q'
        qq << flag if val[n + 2] == 'q'
        q << flag
      when 'p'
        p << flag
      when 'i'
        i << flag
      when 'f'
        f << flag
      when 'n'
        si << flag
      when 'v'
        @values << Regexp.escape(flag)
      else
        next
      end
      m << flag if val[n + 2] == 'm'
      bare << flag if val.end_with?('?')
    else
      bare << val
    end
  end
  no = (no || []).map { |val| (n = val.index('=')) ? val[0, n] : val }
  bare.concat(no)
  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) && pat.match?(v) } }
  skip = false
  opts.each do |opt|
    next @extras << opt if skip

    if single&.match?(opt)
      target << "-#{opt}"
    elsif bare.include?(opt)
      target << (opt.size == 1 ? "-#{opt}" : "--#{opt}")
    elsif opt.start_with?('no-') && no.include?(name = opt[3..-1])
      target << "--no-#{name}"
    else
      if opt =~ /\A([^=]+)=(.+)\z/
        key = $1
        val = $2
        merge = m.include?(key)
        if e.include?(key)
          target << shell_option(key, val, merge: merge)
        elsif q.include?(key)
          target << quote_option(key, val, double: qq.include?(key), merge: merge)
        elsif p.include?(key) && path
          target << quote_option(key, path + val, merge: merge)
        elsif b.include?(key) || numcheck.call(key, val)
          target << basic_option(key, val, merge: merge)
        elsif merge
          target << basic_option(key, val, merge: true)
        else
          @extras << opt
        end
        opt = key
      else
        @extras << opt
        skip = true if args
      end
      skip = true if first&.any? { |s| s.is_a?(Regexp) ? opt.match?(s) : !opt.include?(s) }
    end
  end
  @values = @values.empty? ? /\A\s+\z/ : /\A(#{@values.join('|')})=(.+)\z/m
  @extras.each_with_index(&blk) if block_given?
  self
end

#swap(opts = nil) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/squared/workspace/project/support/class.rb', line 181

def swap(opts = nil)
  unless opts
    opts = found
    @found = []
  end
  @extras = opts
  self
end