类 OptionParser::Switch

单个开关类。对用户来说不重要。

Switch 中定义了几个 Switch 派生类:NoArgumentRequiredArgument 等。

属性

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

公共类方法

guess(arg) 点击切换源代码

arg 中猜测参数样式。返回相应的 OptionParser::Switch 类 (OptionalArgument 等)。

# File lib/optparse.rb, line 519
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 534
def self.incompatible_argument_styles(arg, t)
  raise(ArgumentError, "#{arg}: incompatible argument styles\n  #{self}, #{t}",
        ParseError.filter_backtrace(caller(2)))
end
new(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, desc = ([] if short or long), block = nil, &_block) 点击切换源代码
# File lib/optparse.rb, line 543
def initialize(pattern = nil, conv = nil,
               short = nil, long = nil, arg = nil,
               desc = ([] if short or long), block = nil, &_block)
  raise if Array === pattern
  block ||= _block
  @pattern, @conv, @short, @long, @arg, @desc, @block =
    pattern, conv, short, long, arg, desc, block
end
pattern() 点击切换源代码
# File lib/optparse.rb, line 539
def self.pattern
  NilClass
end

公共实例方法

summarize(sdone = {}, ldone = {}, width = 1, max = width - 1, indent = "") { |indent| ... } 点击切换源代码

生成摘要文本。摘要的每一行都会被传递给块(不带换行符)。

sdone

已经总结的短样式选项键值对哈希。

ldone

已经总结的长样式选项键值对哈希。

width

左侧(选项部分)的宽度。换句话说,右侧(描述部分)从第 width 列开始。

max

左侧最大宽度 -> 选项将在 max 列内填充。

indent

前缀字符串缩进所有摘要行。

# File lib/optparse.rb, line 603
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() 点击切换源代码

开关的主要名称。

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