Class: OptionParser::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/shell/internal/optparse/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 = nil, values = nil, &_block) ⇒ Switch

Returns a new instance of Switch.



550
551
552
553
554
555
556
557
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 550

def initialize(pattern = nil, conv = nil,
               short = nil, long = nil, arg = nil,
               desc = ([] if short or long), block = nil, values = nil, &_block)
  raise if Array === pattern
  block ||= _block
  @pattern, @conv, @short, @long, @arg, @desc, @block, @values =
    pattern, conv, short, long, arg, desc, block, values
end

Instance Attribute Details

#argObject (readonly)

:nodoc:



520
521
522
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 520

def arg
  @arg
end

#blockObject (readonly)

:nodoc:



520
521
522
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 520

def block
  @block
end

#convObject (readonly)

:nodoc:



520
521
522
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 520

def conv
  @conv
end

#descObject (readonly)

:nodoc:



520
521
522
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 520

def desc
  @desc
end

#longObject (readonly)

:nodoc:



520
521
522
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 520

def long
  @long
end

#patternObject (readonly)

:nodoc:



520
521
522
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 520

def pattern
  @pattern
end

#shortObject (readonly)

:nodoc:



520
521
522
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 520

def short
  @short
end

Class Method Details

.guess(arg) ⇒ Object

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



526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 526

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)


541
542
543
544
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 541

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

.patternObject



546
547
548
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 546

def self.pattern
  NilClass
end

Instance Method Details

#add_banner(to) ⇒ Object

:nodoc:



651
652
653
654
655
656
657
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 651

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:



670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 670

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 /\A--\[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)


659
660
661
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 659

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

#omitted_argument(val) ⇒ Object

:nodoc:



711
712
713
714
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 711

def omitted_argument(val)   # :nodoc:
  val.pop if val.size == 3 and val.last.nil?
  val
end

#pretty_print(q) ⇒ Object

:nodoc:



707
708
709
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 707

def pretty_print(q)         # :nodoc:
  q.object_group(self) {pretty_print_contents(q)}
end

#pretty_print_contents(q) ⇒ Object

:nodoc:



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 688

def pretty_print_contents(q) # :nodoc:
  if @block
    q.text ":" + @block.source_location.join(":") + ":"
    first = false
  else
    first = true
  end
  [@short, @long].each do |list|
    list.each do |opt|
      if first
        q.text ":"
        first = false
      end
      q.breakable
      q.text opt
    end
  end
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.



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
641
642
643
644
645
646
647
648
649
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 614

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_nameObject

Main name of the switch.



666
667
668
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 666

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