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.

Methods

Classes and Modules

Class OptionParser::Switch::NoArgument
Class OptionParser::Switch::OptionalArgument
Class OptionParser::Switch::PlacedArgument
Class OptionParser::Switch::RequiredArgument

Attributes

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

Public Class methods

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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # File lib/optparse.rb, line 307
307:     def self.pattern
308:       NilClass
309:     end

Public Instance methods

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.

[Source]

     # File lib/optparse.rb, line 374
374:     def summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "")
375:       sopts, lopts, s = [], [], nil
376:       @short.each {|s| sdone.fetch(s) {sopts << s}; sdone[s] = true} if @short
377:       @long.each {|s| ldone.fetch(s) {lopts << s}; ldone[s] = true} if @long
378:       return if sopts.empty? and lopts.empty? # completely hidden
379: 
380:       left = [sopts.join(', ')]
381:       right = desc.dup
382: 
383:       while s = lopts.shift
384:         l = left[-1].length + s.length
385:         l += arg.length if left.size == 1 && arg
386:         l < max or left << ''
387:         left[-1] << if left[-1].empty? then ' ' * 4 else ', ' end << s
388:       end
389: 
390:       left[0] << arg if arg
391:       mlen = left.collect {|s| s.length}.max.to_i
392:       while mlen > width and l = left.shift
393:         mlen = left.collect {|s| s.length}.max.to_i if l.length == mlen
394:         yield(indent + l)
395:       end
396: 
397:       while (l = left.shift; r = right.shift; l or r)
398:         l = l.to_s.ljust(width) + ' ' + r if r and !r.empty?
399:         yield(indent + l)
400:       end
401: 
402:       self
403:     end

Private Instance methods

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

[Source]

     # File lib/optparse.rb, line 348
348:     def conv_arg(arg, val = nil)
349:       if block
350:         if conv
351:           val = conv.call(*val)
352:         else
353:           val = *val
354:         end
355:         return arg, block, val
356:       else
357:         return arg, nil
358:       end
359:     end

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

[Source]

     # 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

[Validate]