123456789_123456789_123456789_123456789_123456789_

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

Constructor Details

.new(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, desc = ([] if short or long), block = Proc.new) ⇒ Switch

[ GitHub ]

  
# File 'lib/optparse.rb', line 509

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

Class Method Details

.guess(arg)

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

[ GitHub ]

  
# File 'lib/optparse.rb', line 485

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)

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/optparse.rb', line 500

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 505

def self.pattern
  NilClass
end

Instance Attribute Details

#arg (readonly)

[ GitHub ]

  
# File 'lib/optparse.rb', line 479

attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block

#block (readonly)

[ GitHub ]

  
# File 'lib/optparse.rb', line 479

attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block

#conv (readonly)

[ GitHub ]

  
# File 'lib/optparse.rb', line 479

attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block

#desc (readonly)

[ GitHub ]

  
# File 'lib/optparse.rb', line 479

attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block

#long (readonly)

[ GitHub ]

  
# File 'lib/optparse.rb', line 479

attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block

#pattern (readonly)

[ GitHub ]

  
# File 'lib/optparse.rb', line 479

attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block

#short (readonly)

[ GitHub ]

  
# File 'lib/optparse.rb', line 479

attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block

Instance Method Details

#conv_arg(arg, val = []) (private)

Parses argument, converts and returns #arg, #block and result of conversion. Yields at semi-error condition instead of raising an exception.

[ GitHub ]

  
# File 'lib/optparse.rb', line 546

def conv_arg(arg, val = [])
  if conv
    val = conv.call(*val)
  else
    val = proc {|v| v}.call(*val)
  end
  return arg, block, val
end

#parse_arg(arg) {|InvalidArgument, arg| ... } (private)

Parses #arg and returns rest of #arg and matched portion to the argument pattern. Yields when the pattern doesn't match substring.

Yields:

Raises:

[ GitHub ]

  
# File 'lib/optparse.rb', line 521

def parse_arg(arg)
  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

#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).

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.

[ GitHub ]

  
# File 'lib/optparse.rb', line 568

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] << if left[-1].empty? then ' ' * 4 else ', ' end << 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.

[ GitHub ]

  
# File 'lib/optparse.rb', line 620

def switch_name
  (long.first || short.first).sub(/\A-+(?:\[no-\])?/, '')
end