Class 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   each_option   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 520
520:     def initialize
521:       @atype = {}
522:       @short = OptionMap.new
523:       @long = OptionMap.new
524:       @list = []
525:     end

Public Instance methods

See OptionParser.accept.

[Source]

     # File lib/optparse.rb, line 530
530:     def accept(t, pat = /.*/nm, &block)
531:       if pat
532:         pat.respond_to?(:match) or raise TypeError, "has no `match'"
533:       else
534:         pat = t if t.respond_to?(:match)
535:       end
536:       unless block
537:         block = pat.method(:convert).to_proc if pat.respond_to?(:convert)
538:       end
539:       @atype[t] = [pat, block]
540:     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 594
594:     def append(*args)
595:       update(*args)
596:       @list.push(args[0])
597:     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 616
616:     def complete(id, opt, icase = false, *pat, &block)
617:       __send__(id).complete(opt, icase, *pat, &block)
618:     end

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

[Source]

     # File lib/optparse.rb, line 623
623:     def each_option(&block)
624:       list.each(&block)
625:     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 578
578:     def prepend(*args)
579:       update(*args)
580:       @list.unshift(args[0])
581:     end

See OptionParser.reject.

[Source]

     # File lib/optparse.rb, line 545
545:     def reject(t)
546:       @atype.delete(t)
547:     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 603
603:     def search(id, key)
604:       if list = __send__(id)
605:         val = list.fetch(key) {return nil}
606:         block_given? ? yield(val) : val
607:       end
608:     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 632
632:     def summarize(*args, &block)
633:       sum = []
634:       list.reverse_each do |opt|
635:         if opt.respond_to?(:summarize) # perhaps OptionParser::Switch
636:           s = []
637:           opt.summarize(*args) {|l| s << l}
638:           sum.concat(s.reverse)
639:         elsif !opt or opt.empty?
640:           sum << ""
641:         elsif opt.respond_to?(:each_line)
642:           sum.concat([*opt.each_line].reverse)
643:         else
644:           sum.concat([*opt.each].reverse)
645:         end
646:       end
647:       sum.reverse_each(&block)
648:     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 557
557:     def update(sw, sopts, lopts, nsw = nil, nlopts = nil)
558:       o = nil
559:       sopts.each {|o| @short[o] = sw} if sopts
560:       lopts.each {|o| @long[o] = sw} if lopts
561:       nlopts.each {|o| @long[o] = nsw} if nsw and nlopts
562:       used = @short.invert.update(@long.invert)
563:       @list.delete_if {|o| Switch === o and !used[o]}
564:     end

[Validate]