Extended maintenance of Ruby 1.9.3 ended on February 23, 2015. Read more

In Files

  • optparse.rb

OptionParser::Completion

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

Public Class Methods

candidate(key, icase = false, pat = nil, &block) click to toggle source
 
               # File optparse.rb, line 231
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) click to toggle source
 
               # File optparse.rb, line 227
def self.regexp(key, icase)
  Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'), icase)
end
            

Public Instance Methods

candidate(key, icase = false, pat = nil) click to toggle source
 
               # File optparse.rb, line 248
def candidate(key, icase = false, pat = nil)
  Completion.candidate(key, icase, pat, &method(:each))
end
            
complete(key, icase = false, pat = nil) click to toggle source
 
               # File optparse.rb, line 253
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, *) click to toggle source
 
               # File optparse.rb, line 278
def convert(opt = nil, val = nil, *)
  val
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.