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

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.



60
61
62
63
64
65
66
67
# File 'lib/squared/workspace/project/support/class.rb', line 60

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.



53
54
55
# File 'lib/squared/workspace/project/support/class.rb', line 53

def errors
  @errors
end

#extrasObject (readonly)

Returns the value of attribute extras.



53
54
55
# File 'lib/squared/workspace/project/support/class.rb', line 53

def extras
  @extras
end

#foundObject (readonly)

Returns the value of attribute found.



53
54
55
# File 'lib/squared/workspace/project/support/class.rb', line 53

def found
  @found
end

#pathObject (readonly)

Returns the value of attribute path.



53
54
55
# File 'lib/squared/workspace/project/support/class.rb', line 53

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



53
54
55
# File 'lib/squared/workspace/project/support/class.rb', line 53

def project
  @project
end

#targetObject (readonly)

Returns the value of attribute target.



53
54
55
# File 'lib/squared/workspace/project/support/class.rb', line 53

def target
  @target
end

#valuesObject (readonly)

Returns the value of attribute values.



53
54
55
# File 'lib/squared/workspace/project/support/class.rb', line 53

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 args.empty?

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

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

Returns:

  • (Boolean)


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

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', timeout: 30)

  raise_error 'user cancelled'
end

Instance Method Details

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



177
178
179
180
# File 'lib/squared/workspace/project/support/class.rb', line 177

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

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

Returns:

  • (Boolean)


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

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

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



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

def clear(opts = nil, **kwargs)
  opts ||= (kwargs[:errors] ? errors : extras)
  styles = project.theme[:inline] if project
  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: false, pass: ['='], &blk) ⇒ Object



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/squared/workspace/project/support/class.rb', line 69

def parse(list, opts = extras, no: nil, single: nil, args: false, first: false, pass: ['='], &blk)
  @extras = []
  @values = []
  bare = []
  e = []
  b = []
  m = []
  p = []
  q = []
  qq = []
  i = []
  f = []
  si = []
  list.map do |val|
    x, y = val.split('|')
    if y
      if (n = val.index('='))
        x += val[n..-1]
      end
      [x, y]
    else
      x
    end
  end
  .flatten
  .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
      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)
  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
        match = ->(flag, pat) { flag.include?(key) && pat.match?(val) }
        if e.include?(key)
          target << shell_option(key, val)
        elsif q.include?(key)
          target << quote_option(key, val, double: qq.include?(key))
        elsif p.include?(key) && path
          target << quote_option(key, path.join(val))
        elsif m.include?(key)
          target << basic_option(key, val, merge: true)
        elsif b.include?(key) || match.(i, /^\d+$/) || match.(f, /^\d*(?:\.\d+)?$/) || match.(si, /^-?\d+$/)
          target << basic_option(key, val)
        else
          @extras << opt
        end
        opt = key
      else
        @extras << opt
        skip = true if args
      end
      skip = true if first && pass.none? { |s| opt.include?(s) }
    end
  end
  @values = @values.empty? ? /\A\s+\z/ : /\A(#{@values.join('|')})=(.+)\z/
  @extras.each_with_index(&blk) if block_given?
  self
end

#swap(opts = nil) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/squared/workspace/project/support/class.rb', line 168

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