Class: Squared::Workspace::Project::Support::OptionPartition
- Inherits:
-
Object
- Object
- Squared::Workspace::Project::Support::OptionPartition
- 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
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#extras ⇒ Object
readonly
Returns the value of attribute extras.
-
#found ⇒ Object
readonly
Returns the value of attribute found.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
- .append(target, *args, delim: false, escape: false, quote: true) ⇒ Object
- .arg?(target, *args, value: false) ⇒ Boolean
- .clear(target, opts, pass: true, styles: nil, **kwargs) ⇒ Object
- .strip(val) ⇒ Object
Instance Method Summary collapse
- #adjoin(*args, start: false) ⇒ Object
- #append(*args, **kwargs) ⇒ Object
- #arg?(*args, **kwargs) ⇒ Boolean
- #clear(opts = nil, errors: false, **kwargs) ⇒ Object
-
#initialize(opts, list, target = Set.new, project: nil, path: nil, **kwargs, &blk) ⇒ OptionPartition
constructor
A new instance of OptionPartition.
- #parse(list, opts = extras, no: nil, single: nil, args: false, first: nil, &blk) ⇒ Object
- #reset(errors: false) ⇒ Object
- #swap(opts = nil) ⇒ Object
Methods included from Common::Format
enable_aixterm, enable_drawing
Methods included from Common::Shell
basic_option, fill_option, line_width, 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.
68 69 70 71 72 73 74 75 |
# File 'lib/squared/workspace/project/support/class.rb', line 68 def initialize(opts, list, target = Set.new, project: nil, path: nil, **kwargs, &blk) @target = target.is_a?(Set) ? target : target.to_set @project = project @path = path || project&.path @errors = [] @found = [] parse(list, opts, **kwargs, &blk) end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
61 62 63 |
# File 'lib/squared/workspace/project/support/class.rb', line 61 def errors @errors end |
#extras ⇒ Object (readonly)
Returns the value of attribute extras.
61 62 63 |
# File 'lib/squared/workspace/project/support/class.rb', line 61 def extras @extras end |
#found ⇒ Object (readonly)
Returns the value of attribute found.
61 62 63 |
# File 'lib/squared/workspace/project/support/class.rb', line 61 def found @found end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
61 62 63 |
# File 'lib/squared/workspace/project/support/class.rb', line 61 def path @path end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
61 62 63 |
# File 'lib/squared/workspace/project/support/class.rb', line 61 def project @project end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
61 62 63 |
# File 'lib/squared/workspace/project/support/class.rb', line 61 def target @target end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
61 62 63 |
# File 'lib/squared/workspace/project/support/class.rb', line 61 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
50 51 52 53 54 55 56 57 58 |
# 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(val.start_with?('-') ? val : shell_option(val)) } r << /\A(?:#{s.join('|')})#{value ? '[ =].' : '(?: |=|\z)'}/ end s = target.to_a.compact r.any? { |pat| s.any?(pat) } 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 (Logger::WARN, opts.join(', '), pass: true, **kwargs) return if pass || confirm("Run? [#{sub_style(target, styles: styles)}]", '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
#adjoin(*args, start: false) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/squared/workspace/project/support/class.rb', line 211 def adjoin(*args, start: false) i = -1 (items = to_a).each_with_index do |val, index| if i == 0 next unless val.start_with?('-') i = index break elsif index > 0 && !val.start_with?('-') if start i = index + (start.is_a?(Numeric) ? start : 1) break end i = 0 end end if i > 0 if args.empty? args = dup reset end args = items[0...i] + args + items[i..-1] target.clear end merge(args) self end |
#append(*args, **kwargs) ⇒ Object
192 193 194 195 196 |
# File 'lib/squared/workspace/project/support/class.rb', line 192 def append(*args, **kwargs) args = extras if args.empty? OptionPartition.append(target, *args, **kwargs) self end |
#arg?(*args, **kwargs) ⇒ Boolean
245 246 247 |
# File 'lib/squared/workspace/project/support/class.rb', line 245 def arg?(*args, **kwargs) OptionPartition.arg?(target, *args, **kwargs) end |
#clear(opts = nil, errors: false, **kwargs) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/squared/workspace/project/support/class.rb', line 198 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
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 180 181 |
# File 'lib/squared/workspace/project/support/class.rb', line 77 def parse(list, opts = extras, no: nil, single: nil, args: false, first: nil, &blk) @extras = [] @values = [] = [] 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' << flag if val.end_with?('?') else << val end end no = (no || []).map { |val| (n = val.index('=')) ? val[0, n] : val } .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 skip = true if opt == '--' next @extras << opt if skip if single&.match?(opt) target << "-#{opt}" elsif .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 |
#reset(errors: false) ⇒ Object
239 240 241 242 243 |
# File 'lib/squared/workspace/project/support/class.rb', line 239 def reset(errors: false) extras.clear clear(errors: true) if errors self end |
#swap(opts = nil) ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/squared/workspace/project/support/class.rb', line 183 def swap(opts = nil) unless opts opts = found @found = [] end @extras = opts self end |