Class: OptionParser::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/optparse.rb

Overview

Individual switch class. Not important to the user.

Defined within Switch are several Switch-derived classes: NoArgument, RequiredArgument, etc.

Defined Under Namespace

Classes: NoArgument, OptionalArgument, PlacedArgument, RequiredArgument

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, desc = ([] if short or long), block = Proc.new) ⇒ Switch

Returns a new instance of Switch.



364
365
366
367
368
369
370
# File 'lib/optparse.rb', line 364

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

Instance Attribute Details

#argObject (readonly)

Returns the value of attribute arg



334
335
336
# File 'lib/optparse.rb', line 334

def arg
  @arg
end

#blockObject (readonly)

Returns the value of attribute block



334
335
336
# File 'lib/optparse.rb', line 334

def block
  @block
end

#convObject (readonly)

Returns the value of attribute conv



334
335
336
# File 'lib/optparse.rb', line 334

def conv
  @conv
end

#descObject (readonly)

Returns the value of attribute desc



334
335
336
# File 'lib/optparse.rb', line 334

def desc
  @desc
end

#longObject (readonly)

Returns the value of attribute long



334
335
336
# File 'lib/optparse.rb', line 334

def long
  @long
end

#patternObject (readonly)

Returns the value of attribute pattern



334
335
336
# File 'lib/optparse.rb', line 334

def pattern
  @pattern
end

#shortObject (readonly)

Returns the value of attribute short



334
335
336
# File 'lib/optparse.rb', line 334

def short
  @short
end

Class Method Details

.guess(arg) ⇒ Object

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



340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/optparse.rb', line 340

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) ⇒ Object

Raises:

  • (ArgumentError)


355
356
357
358
# File 'lib/optparse.rb', line 355

def self.incompatible_argument_styles(arg, t)
  raise(ArgumentError, "#{arg}: incompatible argument styles\n  #{self}, #{t}",
        ParseError.filter_backtrace(caller(2)))
end

.patternObject



360
361
362
# File 'lib/optparse.rb', line 360

def self.pattern
  NilClass
end

Instance Method Details

#add_banner(to) ⇒ Object

:nodoc:



460
461
462
463
464
465
466
# File 'lib/optparse.rb', line 460

def add_banner(to)  # :nodoc:
  unless @short or @long
    s = desc.join
    to << " [" + s + "]..." unless s.empty?
  end
  to
end

#compsys(sdone, ldone) ⇒ Object

:nodoc:



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/optparse.rb', line 479

def compsys(sdone, ldone)   # :nodoc:
  sopts, lopts = [], []
  @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

  (sopts+lopts).each do |opt|
    # "(-x -c -r)-l[left justify]" \
    if /^--\[no-\](.+)$/ =~ opt
      o = $1
      yield("--#{o}", desc.join(""))
      yield("--no-#{o}", desc.join(""))
    else
      yield("#{opt}", desc.join(""))
    end
  end
end

#match_nonswitch?(str) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


468
469
470
# File 'lib/optparse.rb', line 468

def match_nonswitch?(str)  # :nodoc:
  @pattern =~ str unless @short or @long
end

#summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "") ⇒ Object

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.



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/optparse.rb', line 423

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] << if left[-1].empty? then ' ' * 4 else ', ' end << 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_nameObject

Main name of the switch.



475
476
477
# File 'lib/optparse.rb', line 475

def switch_name
  (long.first || short.first).sub(/\A-+(?:\[no-\])?/, '')
end