Class: OptionParser::Switch
| Relationships & Source Files | |
| Namespace Children | |
|
Classes:
| |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
|
|
| Inherits: | Object |
| Defined in: | lib/optparse.rb |
Overview
Individual switch class. Not important to the user.
Defined within Switch are several Switch-derived classes: NoArgument,
RequiredArgument, etc.
Class Method Summary
Instance Attribute Summary
Instance Method Summary
-
#summarize(sdone = {}, ldone = {}, width = 1, max = width - 1, indent = "")
Produces the summary text.
-
#switch_name
Main name of the switch.
- #add_banner(to) Internal use only
- #compsys(sdone, ldone) Internal use only
- #match_nonswitch?(str) ⇒ Boolean Internal use only
- #omitted_argument(val) Internal use only
- #pretty_print(q) Internal use only
- #pretty_print_contents(q) Internal use only
- #conv_arg(arg, val = []) private Internal use only
- #parse_arg(arg) {|InvalidArgument, arg| ... } private Internal use only
Constructor Details
.new(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, desc = ([] if short or long), block = nil, values = nil, &_block) ⇒ Switch
# File 'lib/optparse.rb', line 551
def initialize(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, desc = ([] if short or long), block = nil, values = nil, &_block) raise if Array === pattern block ||= _block @pattern, @conv, @short, @long, @arg, @desc, @block, @values = pattern, conv, short, long, arg, desc, block, values end
Class Method Details
.guess(arg)
Guesses argument style from #arg. Returns corresponding
Switch class (OptionalArgument, etc.).
# File 'lib/optparse.rb', line 527
def self.guess(arg) case arg when "" t = self when /\A=?\[/ t = Switch::OptionalArgument when /\A\s+\[/ t = Switch::PlacedArgument else t = Switch::RequiredArgument end self >= t or incompatible_argument_styles(arg, t) t end
.incompatible_argument_styles(arg, t)
# File 'lib/optparse.rb', line 542
def self.incompatible_argument_styles(arg, t) raise(ArgumentError, "#{arg}: incompatible argument styles\n #{self}, #{t}", ParseError.filter_backtrace(caller(2))) end
.pattern
[ GitHub ]# File 'lib/optparse.rb', line 547
def self.pattern NilClass end
Instance Attribute Details
#arg (readonly)
This method is for internal use only.
[ GitHub ]
#block (readonly)
This method is for internal use only.
[ GitHub ]
#conv (readonly)
This method is for internal use only.
[ GitHub ]
#desc (readonly)
This method is for internal use only.
[ GitHub ]
#long (readonly)
This method is for internal use only.
[ GitHub ]
#pattern (readonly)
This method is for internal use only.
[ GitHub ]
#short (readonly)
This method is for internal use only.
[ GitHub ]
Instance Method Details
#add_banner(to)
This method is for internal use only.
[ GitHub ]
# File 'lib/optparse.rb', line 650
def (to) # :nodoc: unless @short or @long s = desc.join to << " [" + s + "]..." unless s.empty? end to end
#compsys(sdone, ldone)
This method is for internal use only.
[ GitHub ]
# File 'lib/optparse.rb', line 669
def compsys(sdone, ldone) # :nodoc: sopts, lopts = [], [] @short.each {|s| sdone.fetch(s) {sopts << s}; sdone[s] = true} if @short @long.each {|s| ldone.fetch(s) {lopts << s}; ldone[s] = true} if @long return if sopts.empty? and lopts.empty? # completely hidden (sopts+lopts).each do |opt| # "(-x -c -r)-l[left justify]" if /\A--\[no-\](.+)$/ =~ opt o = $1 yield("--#{o}", desc.join("")) yield("--no-#{o}", desc.join("")) else yield("#{opt}", desc.join("")) end end end
#conv_arg(arg, val = []) (private)
This method is for internal use only.
#match_nonswitch?(str) ⇒ Boolean
This method is for internal use only.
# File 'lib/optparse.rb', line 658
def match_nonswitch?(str) # :nodoc: @pattern =~ str unless @short or @long end
#omitted_argument(val)
This method is for internal use only.
[ GitHub ]
# File 'lib/optparse.rb', line 710
def omitted_argument(val) # :nodoc: val.pop if val.size == 3 and val.last.nil? val end
#parse_arg(arg) {|InvalidArgument, arg| ... } (private)
This method is for internal use only.
# File 'lib/optparse.rb', line 564
private def parse_arg(arg) # :nodoc: pattern or return nil, [arg] unless m = pattern.match(arg) yield(InvalidArgument, arg) return arg, [] end if String === m m = [s = m] else m = m.to_a s = m[0] return nil, m unless String === s end raise InvalidArgument, arg unless arg.rindex(s, 0) return nil, m if s.length == arg.length yield(InvalidArgument, arg) # didn't match whole arg return arg[s.length..-1], m end
#pretty_print(q)
This method is for internal use only.
[ GitHub ]
# File 'lib/optparse.rb', line 706
def pretty_print(q) # :nodoc: q.object_group(self) {pretty_print_contents(q)} end
#pretty_print_contents(q)
This method is for internal use only.
[ GitHub ]
# File 'lib/optparse.rb', line 687
def pretty_print_contents(q) # :nodoc: if @block q.text ":" + @block.source_location.join(":") + ":" first = false else first = true end [@short, @long].each do |list| list.each do |opt| if first q.text ":" first = false end q.breakable q.text opt end end end
#summarize(sdone = {}, ldone = {}, width = 1, max = width - 1, indent = "")
Produces the summary text. Each line of the summary is yielded to the block (without newline).
sdoneAlready summarized short style options keyed hash.
ldoneAlready summarized long style options keyed hash.
widthWidth of left side (option part). In other words, the right side (description part) starts after
widthcolumns.maxMaximum width of left side -> the options are filled within
maxcolumns.indentPrefix string indents all summarized lines.
# File 'lib/optparse.rb', line 613
def summarize(sdone = {}, ldone = {}, width = 1, max = width - 1, indent = "") sopts, lopts = [], [], nil @short.each {|s| sdone.fetch(s) {sopts << s}; sdone[s] = true} if @short @long.each {|s| ldone.fetch(s) {lopts << s}; ldone[s] = true} if @long return if sopts.empty? and lopts.empty? # completely hidden left = [sopts.join(', ')] right = desc.dup while s = lopts.shift l = left[-1].length + s.length l += arg.length if left.size == 1 && arg l < max or sopts.empty? or left << +'' left[-1] << (left[-1].empty? ? ' ' * 4 : ', ') << s end if arg left[0] << (left[1] ? arg.sub(/\A(\[?)=/, '\1') + ',' : arg) end mlen = left.collect {|ss| ss.length}.max.to_i while mlen > width and l = left.shift mlen = left.collect {|ss| ss.length}.max.to_i if l.length == mlen if l.length < width and (r = right[0]) and !r.empty? l = l.to_s.ljust(width) + ' ' + r right.shift end yield(indent + l) end while begin l = left.shift; r = right.shift; l or r end l = l.to_s.ljust(width) + ' ' + r if r and !r.empty? yield(indent + l) end self end
#switch_name
Main name of the switch.