Class: Upoj::Opts
- Inherits:
-
OptionParser
- Object
- OptionParser
- Upoj::Opts
- Defined in:
- lib/upoj-rb/opts.rb
Instance Attribute Summary collapse
-
#funnel ⇒ Object
Returns the value of attribute funnel.
Class Method Summary collapse
Instance Method Summary collapse
- #help! ⇒ Object
-
#initialize(*args) ⇒ Opts
constructor
A new instance of Opts.
- #on(*args) ⇒ Object
- #parse_or_exit! ⇒ Object
- #program_name ⇒ Object
- #summary_banner_section(*args) ⇒ Object
- #summary_examples_section ⇒ Object
- #summary_program_name ⇒ Object
- #to_s ⇒ Object
- #usage! ⇒ Object
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 = args. @funnel = [:funnel] || {} @footer = [:footer] @examples = [:examples] width = [:width] || 32 indent = [:indent] || (' ' * 2) super nil, width, indent @banner = [:banner].kind_of?(Hash) ? ([:banner]) : [:banner] end |
Instance Attribute Details
#funnel ⇒ Object
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.}", :yellow] puts puts self exit 2 else exit err.status end end end |
#program_name ⇒ Object
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 *args = args. %|#{summary_program_name} #{[:description]} #{self.class.section_title :USAGE} #{@summary_indent}#{summary_program_name} #{[:usage]} #{self.class.section_title :OPTIONS} | end |
#summary_examples_section ⇒ Object
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_name ⇒ Object
66 67 68 |
# File 'lib/upoj-rb/opts.rb', line 66 def summary_program_name Paint[program_name, :bold] end |
#to_s ⇒ Object
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 |