Class: OptionParser::List
- Inherits:
-
Object
- Object
- OptionParser::List
- Defined in:
- lib/optparse.rb
Overview
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.
Instance Attribute Summary collapse
-
#atype ⇒ Object
readonly
Map from acceptable argument types to pattern and converter pairs.
-
#list ⇒ Object
readonly
List of all switches and summary string.
-
#long ⇒ Object
readonly
Map from long style option switches to actual switch objects.
-
#short ⇒ Object
readonly
Map from short style option switches to actual switch objects.
Instance Method Summary collapse
-
#accept(t, pat = /.*/nm, &block) ⇒ Object
See OptionParser.accept.
-
#add_banner(to) ⇒ Object
:nodoc:.
-
#append(*args) ⇒ Object
Appends
switch
at the tail of the list, and associates short, long and negated long options. -
#complete(id, opt, icase = false, *pat, &block) ⇒ Object
Searches list
id
foropt
and the optional patterns for completionpat
. -
#each_option(&block) ⇒ Object
Iterates over each option, passing the option to the
block
. -
#initialize ⇒ List
constructor
Just initializes all instance variables.
-
#prepend(*args) ⇒ Object
Inserts
switch
at the head of the list, and associates short, long and negated long options. -
#reject(t) ⇒ Object
See OptionParser.reject.
-
#search(id, key) ⇒ Object
Searches
key
inid
list. -
#summarize(*args, &block) ⇒ Object
Creates the summary table, passing each line to the
block
(without newline).
Constructor Details
Instance Attribute Details
#atype ⇒ Object (readonly)
Map from acceptable argument types to pattern and converter pairs.
506 507 508 |
# File 'lib/optparse.rb', line 506 def atype @atype end |
#list ⇒ Object (readonly)
List of all switches and summary string.
515 516 517 |
# File 'lib/optparse.rb', line 515 def list @list end |
#long ⇒ Object (readonly)
Map from long style option switches to actual switch objects.
512 513 514 |
# File 'lib/optparse.rb', line 512 def long @long end |
#short ⇒ Object (readonly)
Map from short style option switches to actual switch objects.
509 510 511 |
# File 'lib/optparse.rb', line 509 def short @short end |
Instance Method Details
#accept(t, pat = /.*/nm, &block) ⇒ Object
See OptionParser.accept.
530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/optparse.rb', line 530 def accept(t, pat = /.*/nm, &block) if pat pat.respond_to?(:match) or raise TypeError, "has no `match'" 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:
648 649 650 651 652 653 654 655 |
# File 'lib/optparse.rb', line 648 def (to) # :nodoc: list.each do |opt| if opt.respond_to?(:add_banner) opt.(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
-
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)
594 595 596 597 |
# File 'lib/optparse.rb', line 594 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.
616 617 618 |
# File 'lib/optparse.rb', line 616 def complete(id, opt, icase = false, *pat, &block) __send__(id).complete(opt, icase, *pat, &block) end |
#each_option(&block) ⇒ Object
Iterates over each option, passing the option to the block
.
623 624 625 |
# File 'lib/optparse.rb', line 623 def each_option(&block) list.each(&block) end |
#prepend(*args) ⇒ Object
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)
578 579 580 581 |
# File 'lib/optparse.rb', line 578 def prepend(*args) update(*args) @list.unshift(args[0]) end |
#reject(t) ⇒ Object
See OptionParser.reject.
545 546 547 |
# File 'lib/optparse.rb', line 545 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.
603 604 605 606 607 608 |
# File 'lib/optparse.rb', line 603 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.
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
# File 'lib/optparse.rb', line 632 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 << "" else sum.concat(opt.to_a.reverse) end end sum.reverse_each(&block) end |