Class: MyGetoptLong

Inherits:
GetoptLong
  • Object
show all
Defined in:
lib/refe/mygetopt.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(usage, descripter) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/refe/mygetopt.rb', line 13

def new( usage, descripter )
  parser = orignew( * descripter.map {|line|
      line.strip!
      next if line.empty?
      display, shortopt, longopt, takearg, doc = line.split(/\s+/, 5)
      a = []
      a.push longopt unless longopt == '-'
      a.push shortopt unless shortopt == '-'
      a.push takearg == '-' ?
             GetoptLong::NO_ARGUMENT : GetoptLong::REQUIRED_ARGUMENT
      a
  }.compact )
  parser.quiet = true
  parser.instance_eval {
    @usage = usage
    @descripter = descripter.strip
  }
  parser
end

.orignewObject



11
# File 'lib/refe/mygetopt.rb', line 11

alias orignew new

Instance Method Details



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/refe/mygetopt.rb', line 48

def print_options( f )
  @descripter.each do |line|
    line.strip!
    if line.empty?
      f.puts
      next
    end

    disp, sopt, lopt, takearg, doc = line.split(/\s+/, 5)
    if disp == 'o'
      sopt = nil if sopt == '-'
      lopt = nil if lopt == '-'
      opt = [sopt, lopt].compact.join(',')

      takearg = nil if takearg == '-'
      opt = [opt, takearg].compact.join(' ')

      f.printf "  %-27s %s\n", opt, doc
    end
  end
end

#usage(status, errmsg = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/refe/mygetopt.rb', line 34

def usage( status, errmsg = nil )
  f = (status == 0 ? $stdout : $stderr)

  f.puts "#{File.basename $0}: #{errmsg}" if errmsg
  @usage.each do |uline|
    if /%%options%%/ === uline
      print_options f
    else
      f.print uline
    end
  end
  exit status
end