Class: Upoj::Opts

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/upoj-rb/opts.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Opts

Returns a new instance of Opts.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/upoj-rb/opts.rb', line 17

def initialize *args
  options = args.extract_options!

  @funnel = options[:funnel] || {}
  @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.



7
8
9
# File 'lib/upoj-rb/opts.rb', line 7

def funnel
  @funnel
end

Class Method Details

.section_title(title) ⇒ Object



9
10
11
# File 'lib/upoj-rb/opts.rb', line 9

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

.section_title_ref(ref) ⇒ Object



13
14
15
# File 'lib/upoj-rb/opts.rb', line 13

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

Instance Method Details

#help!Object



90
91
92
# File 'lib/upoj-rb/opts.rb', line 90

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

#on(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/upoj-rb/opts.rb', line 31

def on *args
  if block_given?
    super(*args)
  else
    sw = make_switch(args)[0]
    name = sw.long.first.sub /^\-+/, ''
    block = lambda{ |val| @funnel[name] = val }
    super(*args, &block)
  end
end

#parse_or_exit!Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/upoj-rb/opts.rb', line 70

def parse_or_exit!
  begin
    parse!
  rescue Exception => err
    unless err.kind_of?(SystemExit)
      puts
      Kernel.warn Paint["Error: #{err.message}", :yellow]
      puts
      puts self
      exit 2
    else
      exit err.status
    end
  end
end

#program_nameObject



62
63
64
# File 'lib/upoj-rb/opts.rb', line 62

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

#summary_banner_section(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/upoj-rb/opts.rb', line 42

def summary_banner_section *args
  options = args.extract_options!
  %|#{summary_program_name} #{options[:description]}

#{self.class.section_title :USAGE}
#{@summary_indent}#{summary_program_name} #{options[:usage]}

#{self.class.section_title :OPTIONS}
|
end

#summary_examples_sectionObject



53
54
55
56
57
58
59
60
# File 'lib/upoj-rb/opts.rb', line 53

def summary_examples_section
  return nil unless @examples
  String.new("\n#{self.class.section_title :EXAMPLES}").tap do |s|
    @examples.each do |example|
      s << "\n#{@summary_indent}#{summary_program_name} #{example}"
    end
  end
end

#summary_program_nameObject



66
67
68
# File 'lib/upoj-rb/opts.rb', line 66

def summary_program_name
  Paint[program_name, :bold]
end

#to_sObject



86
87
88
# File 'lib/upoj-rb/opts.rb', line 86

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

#usage!Object



94
95
96
# File 'lib/upoj-rb/opts.rb', line 94

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