Class | OptionParser::Switch |
In: |
lib/optparse.rb
|
Parent: | Object |
Individual switch class. Not important to the user.
Defined within Switch are several Switch-derived classes: NoArgument, RequiredArgument, etc.
arg | [R] | |
block | [R] | |
conv | [R] | |
desc | [R] | |
long | [R] | |
pattern | [R] | |
short | [R] |
Guesses argument style from arg. Returns corresponding OptionParser::Switch class (OptionalArgument, etc.).
# File lib/optparse.rb, line 288 288: def self.guess(arg) 289: case arg 290: when "" 291: t = self 292: when /\A=?\[/ 293: t = Switch::OptionalArgument 294: when /\A\s+\[/ 295: t = Switch::PlacedArgument 296: else 297: t = Switch::RequiredArgument 298: end 299: self >= t or incompatible_argument_styles(arg, t) 300: t 301: end
# File lib/optparse.rb, line 303 303: def self.incompatible_argument_styles(arg, t) 304: raise ArgumentError, "#{arg}: incompatible argument styles\n #{self}, #{t}" 305: end
# File lib/optparse.rb, line 311 311: def initialize(pattern = nil, conv = nil, 312: short = nil, long = nil, arg = nil, 313: desc = ([] if short or long), block = Proc.new) 314: raise if Array === pattern 315: @pattern, @conv, @short, @long, @arg, @desc, @block = 316: pattern, conv, short, long, arg, desc, block 317: end
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. |
# File lib/optparse.rb, line 370 370: def summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "") 371: sopts, lopts, s = [], [], nil 372: @short.each {|s| sdone.fetch(s) {sopts << s}; sdone[s] = true} if @short 373: @long.each {|s| ldone.fetch(s) {lopts << s}; ldone[s] = true} if @long 374: return if sopts.empty? and lopts.empty? # completely hidden 375: 376: left = [sopts.join(', ')] 377: right = desc.dup 378: 379: while s = lopts.shift 380: l = left[-1].length + s.length 381: l += arg.length if left.size == 1 && arg 382: l < max or sopts.empty? or left << '' 383: left[-1] << if left[-1].empty? then ' ' * 4 else ', ' end << s 384: end 385: 386: left[0] << arg if arg 387: mlen = left.collect {|s| s.length}.max.to_i 388: while mlen > width and l = left.shift 389: mlen = left.collect {|s| s.length}.max.to_i if l.length == mlen 390: yield(indent + l) 391: end 392: 393: while begin l = left.shift; r = right.shift; l or r end 394: l = l.to_s.ljust(width) + ' ' + r if r and !r.empty? 395: yield(indent + l) 396: end
Parses argument, converts and returns arg, block and result of conversion. Yields at semi-error condition instead of raising an exception.
# File lib/optparse.rb, line 348 348: def conv_arg(arg, val = nil) 349: if conv 350: val = conv.call(*val) 351: else 352: val = proc {|val| val}.call(*val) 353: end 354: return arg, block, val 355: end
Parses arg and returns rest of arg and matched portion to the argument pattern. Yields when the pattern doesn‘t match substring.
# File lib/optparse.rb, line 323 323: def parse_arg(arg) 324: pattern or return nil, arg 325: unless m = pattern.match(arg) 326: yield(InvalidArgument, arg) 327: return arg, nil 328: end 329: if String === m 330: m = [s = m] 331: else 332: m = m.to_a 333: s = m[0] 334: return nil, m unless String === s 335: end 336: raise InvalidArgument, arg unless arg.rindex(s, 0) 337: return nil, m if s.length == arg.length 338: yield(InvalidArgument, arg) # didn't match whole arg 339: return arg[s.length..-1], m 340: end