W3cubDocs

/Ruby 3

class OptionParser::List

Parent:
Object

Simple option list providing mapping from short and/or long option string to OptionParser::Switch and mapping from acceptable argument to matching pattern and converter pair. Also provides summary feature.

Attributes

atype[R]

Map from acceptable argument types to pattern and converter pairs.

list[R]

List of all switches and summary string.

long[R]

Map from long style option switches to actual switch objects.

short[R]

Map from short style option switches to actual switch objects.

Public Class Methods

new() Show source
# File lib/optparse.rb, line 771
def initialize
  @atype = {}
  @short = OptionMap.new
  @long = OptionMap.new
  @list = []
end

Just initializes all instance variables.

Public Instance Methods

accept(t, pat = /.*/m, &block) Show source
# File lib/optparse.rb, line 781
def accept(t, pat = /.*/m, &block)
  if pat
    pat.respond_to?(:match) or
      raise TypeError, "has no `match'", ParseError.filter_backtrace(caller(2))
  else
    pat = t if t.respond_to?(:match)
  end
  unless block
    block = pat.method(:convert).to_proc if pat.respond_to?(:convert)
  end
  @atype[t] = [pat, block]
end

See OptionParser.accept.

append(*args) Show source
# File lib/optparse.rb, line 845
def append(*args)
  update(*args)
  @list.push(args[0])
end

Appends switch at the tail of the list, and associates short, long and negated long options. Arguments are:

switch

OptionParser::Switch instance to be inserted.

short_opts

List of short style options.

long_opts

List of long style options.

nolong_opts

List of long style options with “no-” prefix.

append(switch, short_opts, long_opts, nolong_opts)
complete(id, opt, icase = false, *pat, &block) Show source
# File lib/optparse.rb, line 867
def complete(id, opt, icase = false, *pat, &block)
  __send__(id).complete(opt, icase, *pat, &block)
end

Searches list id for opt and the optional patterns for completion pat. If icase is true, the search is case insensitive. The result is returned or yielded if a block is given. If it isn't found, nil is returned.

each_option(&block) Show source
# File lib/optparse.rb, line 878
def each_option(&block)
  list.each(&block)
end

Iterates over each option, passing the option to the block.

get_candidates(id) { |__send__(id).keys| ... } Show source
# File lib/optparse.rb, line 871
def get_candidates(id)
  yield __send__(id).keys
end
prepend(*args) Show source
# File lib/optparse.rb, line 829
def prepend(*args)
  update(*args)
  @list.unshift(args[0])
end

Inserts switch at the head of the list, and associates short, long and negated long options. Arguments are:

switch

OptionParser::Switch instance to be inserted.

short_opts

List of short style options.

long_opts

List of long style options.

nolong_opts

List of long style options with “no-” prefix.

prepend(switch, short_opts, long_opts, nolong_opts)
reject(t) Show source
# File lib/optparse.rb, line 797
def reject(t)
  @atype.delete(t)
end

See OptionParser.reject.

# File lib/optparse.rb, line 854
def search(id, key)
  if list = __send__(id)
    val = list.fetch(key) {return nil}
    block_given? ? yield(val) : val
  end
end

Searches key in id list. The result is returned or yielded if a block is given. If it isn't found, nil is returned.

summarize(*args, &block) Show source
# File lib/optparse.rb, line 887
def summarize(*args, &block)
  sum = []
  list.reverse_each do |opt|
    if opt.respond_to?(:summarize) # perhaps OptionParser::Switch
      s = []
      opt.summarize(*args) {|l| s << l}
      sum.concat(s.reverse)
    elsif !opt or opt.empty?
      sum << ""
    elsif opt.respond_to?(:each_line)
      sum.concat([*opt.each_line].reverse)
    else
      sum.concat([*opt.each].reverse)
    end
  end
  sum.reverse_each(&block)
end

Creates the summary table, passing each line to the block (without newline). The arguments args are passed along to the summarize method which is called on every option.

Private Instance Methods

update(sw, sopts, lopts, nsw = nil, nlopts = nil) Show source
# File lib/optparse.rb, line 809
def update(sw, sopts, lopts, nsw = nil, nlopts = nil)
  sopts.each {|o| @short[o] = sw} if sopts
  lopts.each {|o| @long[o] = sw} if lopts
  nlopts.each {|o| @long[o] = nsw} if nsw and nlopts
  used = @short.invert.update(@long.invert)
  @list.delete_if {|o| Switch === o and !used[o]}
end

Adds sw according to sopts, lopts and nlopts.

sw

OptionParser::Switch instance to be added.

sopts

Short style option list.

lopts

Long style option list.

nlopts

Negated long style options list.

Ruby Core © 1993–2020 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.