Module: OptionParser::Completion
- Included in:
- CompletingHash, OptionMap
- Defined in:
- lib/rubysl/optparse/optparse.rb
Overview
Keyword completion module. This allows partial arguments to be specified and resolved against a list of acceptable values.
Class Method Summary collapse
Instance Method Summary collapse
- #candidate(key, icase = false, pat = nil) ⇒ Object
- #complete(key, icase = false, pat = nil) ⇒ Object
- #convert(opt = nil, val = nil) ⇒ Object
Class Method Details
.candidate(key, icase = false, pat = nil, &block) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/rubysl/optparse/optparse.rb', line 234 def self.candidate(key, icase = false, pat = nil, &block) pat ||= Completion.regexp(key, icase) candidates = [] block.call do |k, *v| (if Regexp === k kn = nil k === key else kn = defined?(k.id2name) ? k.id2name : k pat === kn end) or next v << k if v.empty? candidates << [k, v, kn] end candidates end |
.regexp(key, icase) ⇒ Object
230 231 232 |
# File 'lib/rubysl/optparse/optparse.rb', line 230 def self.regexp(key, icase) Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'), icase) end |
Instance Method Details
#candidate(key, icase = false, pat = nil) ⇒ Object
251 252 253 |
# File 'lib/rubysl/optparse/optparse.rb', line 251 def candidate(key, icase = false, pat = nil) Completion.candidate(key, icase, pat, &method(:each)) end |
#complete(key, icase = false, pat = nil) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/rubysl/optparse/optparse.rb', line 256 def complete(key, icase = false, pat = nil) candidates = candidate(key, icase, pat, &method(:each)).sort_by {|k, v, kn| kn.size} if candidates.size == 1 canon, sw, * = candidates[0] elsif candidates.size > 1 canon, sw, cn = candidates.shift candidates.each do |k, v, kn| next if sw == v if String === cn and String === kn if cn.rindex(kn, 0) canon, sw, cn = k, v, kn next elsif kn.rindex(cn, 0) next end end throw :ambiguous, key end end if canon block_given? or return key, *sw yield(key, *sw) end end |
#convert(opt = nil, val = nil) ⇒ Object
281 282 283 |
# File 'lib/rubysl/optparse/optparse.rb', line 281 def convert(opt = nil, val = nil, *) val end |