Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • optparse.rb

OptionParser::Switch

Individual switch class. Not important to the user.

Defined within Switch are several Switch-derived classes: NoArgument, RequiredArgument, etc.

Attributes

arg[R]
block[R]
conv[R]
desc[R]
long[R]
pattern[R]
short[R]

Public Class Methods

guess(arg) click to toggle source

Guesses argument style from arg. Returns corresponding OptionParser::Switch class (OptionalArgument, etc.).

 
               # File optparse.rb, line 288
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) click to toggle source
 
               # File optparse.rb, line 303
def self.incompatible_argument_styles(arg, t)
  raise ArgumentError, "#{arg}: incompatible argument styles\n  #{self}, #{t}"
end
            
new(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, desc = ([] if short or long), block = Proc.new) click to toggle source
 
               # File optparse.rb, line 311
def initialize(pattern = nil, conv = nil,
               short = nil, long = nil, arg = nil,
               desc = ([] if short or long), block = Proc.new)
  raise if Array === pattern
  @pattern, @conv, @short, @long, @arg, @desc, @block =
    pattern, conv, short, long, arg, desc, block
end
            
pattern() click to toggle source
 
               # File optparse.rb, line 307
def self.pattern
  NilClass
end
            

Public Instance Methods

summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "") click to toggle source

Produces the summary text. Each line of the summary is yielded to the block (without newline).

sdone

Already summarized short style options keyed hash.

ldone

Already summarized long style options keyed hash.

width

Width of left side (option part). In other words, the right side (description part) starts after width columns.

max

Maximum width of left side -> the options are filled within max columns.

indent

Prefix string indents all summarized lines.

 
               # File optparse.rb, line 370
def summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "")
  sopts, lopts, s = [], [], 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] << if left[-1].empty? then ' ' * 4 else ', ' end << s
  end

  left[0] << arg if arg
  mlen = left.collect {|s| s.length}.max.to_i
  while mlen > width and l = left.shift
    mlen = left.collect {|s| s.length}.max.to_i if l.length == mlen
    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() click to toggle source

Main name of the switch.

 
               # File optparse.rb, line 416
def switch_name
  (long.first || short.first).sub(/\A-+(?:\[no-\])?/, '')
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus