Class: Gem::OptionParser::List

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/optparse/lib/optparse.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Just initializes all instance variables.



777
778
779
780
781
782
# File 'lib/rubygems/optparse/lib/optparse.rb', line 777

def initialize
  @atype = {}
  @short = OptionMap.new
  @long = OptionMap.new
  @list = []
end

Instance Attribute Details

#atypeObject (readonly)

Map from acceptable argument types to pattern and converter pairs.



763
764
765
# File 'lib/rubygems/optparse/lib/optparse.rb', line 763

def atype
  @atype
end

#listObject (readonly)

List of all switches and summary string.



772
773
774
# File 'lib/rubygems/optparse/lib/optparse.rb', line 772

def list
  @list
end

#longObject (readonly)

Map from long style option switches to actual switch objects.



769
770
771
# File 'lib/rubygems/optparse/lib/optparse.rb', line 769

def long
  @long
end

#shortObject (readonly)

Map from short style option switches to actual switch objects.



766
767
768
# File 'lib/rubygems/optparse/lib/optparse.rb', line 766

def short
  @short
end

Instance Method Details

#accept(t, pat = /.*/m, &block) ⇒ Object

See Gem::OptionParser.accept.



787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/rubygems/optparse/lib/optparse.rb', line 787

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

#add_banner(to) ⇒ Object

:nodoc:



911
912
913
914
915
916
917
918
# File 'lib/rubygems/optparse/lib/optparse.rb', line 911

def add_banner(to)  # :nodoc:
  list.each do |opt|
    if opt.respond_to?(:add_banner)
      opt.add_banner(to)
    end
  end
  to
end

#append(*args) ⇒ Object

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

switch

Gem::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)


851
852
853
854
# File 'lib/rubygems/optparse/lib/optparse.rb', line 851

def append(*args)
  update(*args)
  @list.push(args[0])
end

#complete(id, opt, icase = false, *pat, &block) ⇒ Object

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.



873
874
875
# File 'lib/rubygems/optparse/lib/optparse.rb', line 873

def complete(id, opt, icase = false, *pat, &block)
  __send__(id).complete(opt, icase, *pat, &block)
end

#compsys(*args, &block) ⇒ Object

:nodoc:



920
921
922
923
924
925
926
# File 'lib/rubygems/optparse/lib/optparse.rb', line 920

def compsys(*args, &block)  # :nodoc:
  list.each do |opt|
    if opt.respond_to?(:compsys)
      opt.compsys(*args, &block)
    end
  end
end

#each_option(&block) ⇒ Object

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



884
885
886
# File 'lib/rubygems/optparse/lib/optparse.rb', line 884

def each_option(&block)
  list.each(&block)
end

#get_candidates(id) {|__send__(id).keys| ... } ⇒ Object

Yields:

  • (__send__(id).keys)


877
878
879
# File 'lib/rubygems/optparse/lib/optparse.rb', line 877

def get_candidates(id)
  yield __send__(id).keys
end

#prepend(*args) ⇒ Object

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

switch

Gem::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)


835
836
837
838
# File 'lib/rubygems/optparse/lib/optparse.rb', line 835

def prepend(*args)
  update(*args)
  @list.unshift(args[0])
end

#reject(t) ⇒ Object

See Gem::OptionParser.reject.



803
804
805
# File 'lib/rubygems/optparse/lib/optparse.rb', line 803

def reject(t)
  @atype.delete(t)
end

#search(id, key) ⇒ Object

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



860
861
862
863
864
865
# File 'lib/rubygems/optparse/lib/optparse.rb', line 860

def search(id, key)
  if list = __send__(id)
    val = list.fetch(key) {return nil}
    block_given? ? yield(val) : val
  end
end

#summarize(*args, &block) ⇒ Object

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.



893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/rubygems/optparse/lib/optparse.rb', line 893

def summarize(*args, &block)
  sum = []
  list.reverse_each do |opt|
    if opt.respond_to?(:summarize) # perhaps Gem::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