Class OptionParser::List
In: lib/optparse.rb
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.

Methods

accept   append   complete   new   prepend   reject   search   summarize   update  

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

Just initializes all instance variables.

[Source]

     # File lib/optparse.rb, line 505
505:     def initialize
506:       @atype = {}
507:       @short = OptionMap.new
508:       @long = OptionMap.new
509:       @list = []
510:     end

Public Instance methods

See OptionParser.accept.

[Source]

     # File lib/optparse.rb, line 515
515:     def accept(t, pat = /.*/nm, &block)
516:       if pat
517:         pat.respond_to?(:match) or raise TypeError, "has no `match'"
518:       else
519:         pat = t if t.respond_to?(:match)
520:       end
521:       unless block
522:         block = pat.method(:convert).to_proc if pat.respond_to?(:convert)
523:       end
524:       @atype[t] = [pat, block]
525:     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)

[Source]

     # File lib/optparse.rb, line 579
579:     def append(*args)
580:       update(*args)
581:       @list.push(args[0])
582:     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.

[Source]

     # File lib/optparse.rb, line 602
602:     def complete(id, opt, icase = false, *pat, &block)
603:       __send__(id).complete(opt, icase, *pat, &block)
604:     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)

[Source]

     # File lib/optparse.rb, line 563
563:     def prepend(*args)
564:       update(*args)
565:       @list.unshift(args[0])
566:     end

See OptionParser.reject.

[Source]

     # File lib/optparse.rb, line 530
530:     def reject(t)
531:       @atype.delete(t)
532:     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.

[Source]

     # File lib/optparse.rb, line 588
588:     def search(id, key)
589:       if list = __send__(id)
590:         val = list.fetch(key) {return nil}
591:         return val unless block_given?
592:         yield(val)
593:       end
594:     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.

[Source]

     # File lib/optparse.rb, line 611
611:     def summarize(*args, &block)
612:       list.each do |opt|
613:         if opt.respond_to?(:summarize) # perhaps OptionParser::Switch
614:           opt.summarize(*args, &block)
615:         elsif opt.empty?
616:           yield("")
617:         else
618:           opt.each(&block)
619:         end
620:       end
621:     end

Private Instance methods

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.

[Source]

     # File lib/optparse.rb, line 542
542:     def update(sw, sopts, lopts, nsw = nil, nlopts = nil)
543:       o = nil
544:       sopts.each {|o| @short[o] = sw} if sopts
545:       lopts.each {|o| @long[o] = sw} if lopts
546:       nlopts.each {|o| @long[o] = nsw} if nsw and nlopts
547:       used = @short.invert.update(@long.invert)
548:       @list.delete_if {|o| Switch === o and !used[o]}
549:     end

[Validate]