Class: OptionParser::List
- Inherits:
-
Object
- Object
- OptionParser::List
- Defined in:
- lib/llm/shell/internal/optparse/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 = /.*/m, &block) ⇒ Object
See OptionParser.accept.
-
#add_banner(to) ⇒ Object
:nodoc:.
-
#append(*args) ⇒ Object
Appends
switchat the tail of the list, and associates short, long and negated long options. -
#complete(id, opt, icase = false, *pat, &block) ⇒ Object
Searches list
idforoptand the optional patterns for completionpat. -
#compsys(*args, &block) ⇒ Object
:nodoc:.
-
#each_option(&block) ⇒ Object
Iterates over each option, passing the option to the
block. - #get_candidates(id) {|__send__(id).keys| ... } ⇒ Object
-
#initialize ⇒ List
constructor
Just initializes all instance variables.
-
#prepend(*args) ⇒ Object
Inserts
switchat the head of the list, and associates short, long and negated long options. -
#pretty_print(q) ⇒ Object
:nodoc:.
-
#reject(t) ⇒ Object
See OptionParser.reject.
-
#search(id, key) ⇒ Object
Searches
keyinidlist. -
#summarize(*args, &block) ⇒ Object
Creates the summary table, passing each line to the
block(without newline).
Constructor Details
#initialize ⇒ List
Just initializes all instance variables.
835 836 837 838 839 840 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 835 def initialize @atype = {} @short = OptionMap.new @long = OptionMap.new @list = [] end |
Instance Attribute Details
#atype ⇒ Object (readonly)
Map from acceptable argument types to pattern and converter pairs.
821 822 823 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 821 def atype @atype end |
#list ⇒ Object (readonly)
List of all switches and summary string.
830 831 832 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 830 def list @list end |
#long ⇒ Object (readonly)
Map from long style option switches to actual switch objects.
827 828 829 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 827 def long @long end |
#short ⇒ Object (readonly)
Map from short style option switches to actual switch objects.
824 825 826 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 824 def short @short end |
Instance Method Details
#accept(t, pat = /.*/m, &block) ⇒ Object
See OptionParser.accept.
856 857 858 859 860 861 862 863 864 865 866 867 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 856 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:
980 981 982 983 984 985 986 987 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 980 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)
920 921 922 923 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 920 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.
942 943 944 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 942 def complete(id, opt, icase = false, *pat, &block) __send__(id).complete(opt, icase, *pat, &block) end |
#compsys(*args, &block) ⇒ Object
:nodoc:
989 990 991 992 993 994 995 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 989 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.
953 954 955 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 953 def each_option(&block) list.each(&block) end |
#get_candidates(id) {|__send__(id).keys| ... } ⇒ Object
946 947 948 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 946 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-
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)
904 905 906 907 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 904 def prepend(*args) update(*args) @list.unshift(args[0]) end |
#pretty_print(q) ⇒ Object
:nodoc:
842 843 844 845 846 847 848 849 850 851 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 842 def pretty_print(q) # :nodoc: q.group(1, "(", ")") do @list.each do |sw| next unless Switch === sw q.group(1, "(" + sw.pretty_head, ")") do sw.pretty_print_contents(q) end end end end |
#reject(t) ⇒ Object
See OptionParser.reject.
872 873 874 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 872 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.
929 930 931 932 933 934 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 929 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.
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 962 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 << "" 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 |