Class: OptionParser::Switch

Inherits:
Object show all
Defined in:
opal/stdlib/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 || long), block = nil, &_block) ⇒ Switch

Returns a new instance of Switch.



545
546
547
548
549
550
551
552
# File 'opal/stdlib/optparse.rb', line 545

def initialize(pattern = nil, conv = nil,
  short = nil, long = nil, arg = nil,
  desc = ([] if short || 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

Instance Attribute Details

#argObject (readonly)

Returns the value of attribute arg.



514
515
516
# File 'opal/stdlib/optparse.rb', line 514

def arg
  @arg
end

#blockObject (readonly)

Returns the value of attribute block.



514
515
516
# File 'opal/stdlib/optparse.rb', line 514

def block
  @block
end

#convObject (readonly)

Returns the value of attribute conv.



514
515
516
# File 'opal/stdlib/optparse.rb', line 514

def conv
  @conv
end

#descObject (readonly)

Returns the value of attribute desc.



514
515
516
# File 'opal/stdlib/optparse.rb', line 514

def desc
  @desc
end

#longObject (readonly)

Returns the value of attribute long.



514
515
516
# File 'opal/stdlib/optparse.rb', line 514

def long
  @long
end

#patternObject (readonly)

Returns the value of attribute pattern.



514
515
516
# File 'opal/stdlib/optparse.rb', line 514

def pattern
  @pattern
end

#shortObject (readonly)

Returns the value of attribute short.



514
515
516
# File 'opal/stdlib/optparse.rb', line 514

def short
  @short
end

Class Method Details

.guess(arg) ⇒ Object

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



520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'opal/stdlib/optparse.rb', line 520

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) || incompatible_argument_styles(arg, t)
  t
end

.incompatible_argument_styles(arg, t) ⇒ Object

Raises:

  • (ArgumentError)


535
536
537
538
539
# File 'opal/stdlib/optparse.rb', line 535

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

.patternObject



541
542
543
# File 'opal/stdlib/optparse.rb', line 541

def self.pattern
  NilClass
end

Instance Method Details

#add_banner(to) ⇒ Object

:nodoc:



642
643
644
645
646
647
648
# File 'opal/stdlib/optparse.rb', line 642

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

#compsys(sdone, ldone) ⇒ Object

:nodoc:



661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'opal/stdlib/optparse.rb', line 661

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? && lopts.empty? # completely hidden

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

#match_nonswitch?(str) ⇒ Boolean

:nodoc:

Returns:



650
651
652
# File 'opal/stdlib/optparse.rb', line 650

def match_nonswitch?(str) # :nodoc:
  @pattern =~ str unless @short || @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.



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'opal/stdlib/optparse.rb', line 605

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? && 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) || sopts.empty? || left << +''
    left[-1] += (left[-1].empty? ? ' ' * 4 : ', ') + s
  end

  if arg
    left[0] += (left[1] ? arg.sub(/\A(\[?)=/, '\1') + ',' : arg)
  end
  mlen = left.collect(&:length).max.to_i
  while (mlen > width) && (l = left.shift)
    mlen = left.collect(&:length).max.to_i if l.length == mlen
    if (l.length < width) && (r = right[0]) && !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 || r end
    l = l.to_s.ljust(width) + ' ' + r if r && !r.empty?
    yield(indent + l)
  end

  self
end

#switch_nameObject

Main name of the switch.



657
658
659
# File 'opal/stdlib/optparse.rb', line 657

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