Class: Iopts

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

Overview

Customized version of ruby’s OptionParser.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Iopts

Returns a new instance of Iopts.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/iopts.rb', line 18

def initialize *args
  options = args.last.kind_of?(Hash) ? args.pop : {}

  @funnel = options[:funnel] || OpenStruct.new
  @footer = options[:footer]
  @examples = options[:examples]

  width = options[:width] || 32
  indent = options[:indent] || (' ' * 2)
  super nil, width, indent

  @banner = options[:banner].kind_of?(Hash) ? summary_banner_section(options[:banner]) : options[:banner]
end

Instance Attribute Details

#funnelObject

Returns the value of attribute funnel.



8
9
10
# File 'lib/iopts.rb', line 8

def funnel
  @funnel
end

Class Method Details

.section_title(title) ⇒ Object



10
11
12
# File 'lib/iopts.rb', line 10

def self.section_title title
  Paint[title, :bold]
end

.section_title_ref(ref) ⇒ Object



14
15
16
# File 'lib/iopts.rb', line 14

def self.section_title_ref ref
  Paint[ref, :underline]
end

Instance Method Details

#help!Object



54
55
56
# File 'lib/iopts.rb', line 54

def help!
  self.on('-h', '--help', 'show this help and exit'){ puts self; exit 0 }
end

#on(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/iopts.rb', line 32

def on *args
  if block_given?
    super(*args)
  else
    sw = make_switch(args)
    return super(*args) if !sw || sw.empty? || !sw.first.respond_to?(:long)
    name = sw.first.long.first || sw.first.short.first
    return super(*args) unless name
    name = name.sub /^\-+/, ''
    block = lambda{ |val| fill_funnel name, val }
    super(*args, &block)
  end
end

#program_nameObject



46
47
48
# File 'lib/iopts.rb', line 46

def program_name
  @program_name || File.basename($0)
end

#to_sObject



50
51
52
# File 'lib/iopts.rb', line 50

def to_s
  "#{super}#{summary_examples_section}#{@footer}"
end

#usage!Object



58
59
60
# File 'lib/iopts.rb', line 58

def usage!
  self.on('-u', '--usage', 'show this help and exit'){ puts self; exit 0 }
end