類別 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