Ruby 2.4

OptionParser::Switch

class OptionParser::Switch

父类:对象

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

在Switch中定义了几个Switch派生类:NoArgument,RequiredArgument等

属性

argR

blockR

convR

descR

longR

patternR

shortR

公共类方法

guess(arg) Show source

猜测论证的风格arg。返回相应的OptionParser :: Switch类(OptionalArgument等)。

# File lib/optparse.rb, line 484 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) Show source

# File lib/optparse.rb, line 499 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 = Proc.new) Show source

# File lib/optparse.rb, line 508 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() Show source

# File lib/optparse.rb, line 504 def self.pattern NilClass end

公共实例方法

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

产生摘要文本。摘要的每一行都被赋予块(不含换行符)。

sdone

已经总结了简短风格选项的密钥散列。

ldone

已经总结了长键哈希的风格选项。

width

左侧宽度(选项部分)。换句话说,右侧(描述部分)在width列之后开始。

max

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

indent

前缀字符串缩进所有汇总行。

# File lib/optparse.rb, line 567 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() Show source

开关的主要名称。

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

私有实例方法

conv_arg(arg, val = []) Show source

解析参数,转换和回报argblock以及转换的结果。在半错误条件下产生而不是引发异常。

# File lib/optparse.rb, line 545 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| ... } Show source

解析arg并返回arg参数模式的剩余部分和匹配部分。当模式与子串不匹配时产生。

# File lib/optparse.rb, line 520 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