Class: Gem::OptionParser::List
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubygems/optparse/lib/optparse.rb |
Overview
Simple option list providing mapping from short and/or long option string to Switch
and mapping from acceptable argument to matching pattern and converter pair. Also provides summary feature.
Class Method Summary
-
.new ⇒ List
constructor
Just initializes all instance variables.
Instance Attribute Summary
Instance Method Summary
-
#accept(t, pat = /.*/m, &block)
See accept.
-
#append(*args)
Appends
switch
at the tail of the list, and associates short, long and negated long options. -
#complete(id, opt, icase = false, *pat, &block)
Searches list
id
foropt
and the optional patterns for completionpat
. -
#each_option(&block)
Iterates over each option, passing the option to the
block
. - #get_candidates(id) {|__send__(id).keys| ... }
-
#prepend(*args)
Inserts
switch
at the head of the list, and associates short, long and negated long options. -
#reject(t)
See reject.
-
#search(id, key)
Searches
key
inid
list. -
#summarize(*args, &block)
Creates the summary table, passing each line to the
block
(without newline). - #add_banner(to) Internal use only
- #compsys(*args, &block) Internal use only
-
#update(sw, sopts, lopts, nsw = nil, nlopts = nil)
private
Internal use only
Adds
sw
according tosopts
,lopts
andnlopts
.
Constructor Details
.new ⇒ List
Just initializes all instance variables.
Instance Attribute Details
#atype (readonly)
Map from acceptable argument types to pattern and converter pairs.
# File 'lib/rubygems/optparse/lib/optparse.rb', line 763
attr_reader :atype
#list (readonly)
List
of all switches and summary string.
# File 'lib/rubygems/optparse/lib/optparse.rb', line 772
attr_reader :list
#long (readonly)
Map from long style option switches to actual switch objects.
# File 'lib/rubygems/optparse/lib/optparse.rb', line 769
attr_reader :long
#short (readonly)
Map from short style option switches to actual switch objects.
# File 'lib/rubygems/optparse/lib/optparse.rb', line 766
attr_reader :short
Instance Method Details
#accept(t, pat = /.*/m, &block)
[ GitHub ]# 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)
# File 'lib/rubygems/optparse/lib/optparse.rb', line 911
def (to) # :nodoc: list.each do |opt| if opt.respond_to?(: ) opt. (to) end end to end
#append(*args)
Appends switch
at the tail of the list, and associates short, long and negated long options. Arguments are:
switch
-
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)
# 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)
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.
# 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)
# 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)
Iterates over each option, passing the option to the block
.
# File 'lib/rubygems/optparse/lib/optparse.rb', line 884
def each_option(&block) list.each(&block) end
#get_candidates(id) {|__send__(id).keys| ... }
# File 'lib/rubygems/optparse/lib/optparse.rb', line 877
def get_candidates(id) yield __send__(id).keys end
#prepend(*args)
Inserts switch
at the head of the list, and associates short, long and negated long options. Arguments are:
switch
-
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)
# File 'lib/rubygems/optparse/lib/optparse.rb', line 835
def prepend(*args) update(*args) @list.unshift(args[0]) end
#reject(t)
[ GitHub ]# File 'lib/rubygems/optparse/lib/optparse.rb', line 803
def reject(t) @atype.delete(t) end
#search(id, key)
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)
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.
# 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
#update(sw, sopts, lopts, nsw = nil, nlopts = nil) (private)
Adds sw
according to sopts
, lopts
and nlopts
.
sw
-
Switch
instance to be added. sopts
-
Short style option list.
lopts
-
Long style option list.
nlopts
-
Negated long style options list.
# File 'lib/rubygems/optparse/lib/optparse.rb', line 815
def update(sw, sopts, lopts, nsw = nil, nlopts = nil) # :nodoc: 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