123456789_123456789_123456789_123456789_123456789_

Module: OptionParser::Completion

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/optparse.rb

Overview

Keyword completion module. This allows partial arguments to be specified and resolved against a list of acceptable values.

Class Method Summary

Instance Method Summary

Class Method Details

.candidate(key, icase = false, pat = nil, &block)

[ GitHub ]

  
# File 'lib/optparse.rb', line 447

def self.candidate(key, icase = false, pat = nil, &block)
  pat ||= Completion.regexp(key, icase)
  candidates = []
  block.call do |k, *v|
    (if Regexp === k
       kn = ""
       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)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/optparse.rb', line 443

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, &_)

[ GitHub ]

  
# File 'lib/optparse.rb', line 464

def candidate(key, icase = false, pat = nil, &_)
  Completion.candidate(key, icase, pat, &method(:each))
end

#complete(key, icase = false, pat = nil)

[ GitHub ]

  
# File 'lib/optparse.rb', line 469

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)

[ GitHub ]

  
# File 'lib/optparse.rb', line 494

def convert(opt = nil, val = nil, *)
  val
end