Class | OptionParser::Switch
In: | optparse.rb
Parent: | Object

Individual switch class. Not important to the user.

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

### Methods

guess incompatible_argument_styles new pattern summarize

### Classes and Modules

Class OptionParser::Switch::NoArgument
Class OptionParser::Switch::OptionalArgument
Class OptionParser::Switch::PlacedArgument
Class OptionParser::Switch::RequiredArgument

### Attributes

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

### Public Class methods

guess(arg)

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

[Source]

    # File optparse.rb, line 277
        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)

[Source]

    # File optparse.rb, line 292
        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)

[Source]

    # File optparse.rb, line 300
        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()

[Source]

    # File optparse.rb, line 296
        def self.pattern
          NilClass
        end

### Public Instance methods

summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "") {|indent + l| ...}

OptionParser::Switch#summarize(sdone, ldone, width, max, indent)

Makes summary strings.

     :Parameters:
       : ((|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 word, right side,
         description part strings start at ((|width|)) column.
       : ((|max|))
         maximum width of left side, options are filled within ((|max|)) columns.
       : ((|indent|))
         prefix string indents each summarized lines.
       : (({block}))
         to be passed each lines(without newline).

[Source]

    # File optparse.rb, line 388
        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 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 (l = left.shift; r = right.shift; l or r)
            l = l.to_s.ljust(width) + ' ' + r if r and !r.empty?
            yield(indent + l)
          end

          self
        end

[Validate]