class Upoj::Opts
Customized version of ruby's OptionParser.
Attributes
funnel[RW]
Hash that will be filled with the values of all options that were defined without a block.
Examples¶ ↑
opts = Upoj::Opts.new opts.on('--option'){ # do whatever } opts.on('-f', '--fubar') opts.on('--value VALUE') ARGV #=> [ '--option', '-f', '--value', 43 ] opts.parse! # retrieve options in funnel by default opts.funnel #=> { 'fubar' => true, 'value' => 43 } # a funnel with initial values can be given at construction funnel = { 'foo' => false } opts = Upoj::Opts.new :funnel => funnel
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
# File lib/upoj-rb/opts.rb, line 37 def initialize *args options = args.extract_options! @funnel = options[:funnel] || HashWithIndifferentAccess.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
section_title(title)
click to toggle source
# File lib/upoj-rb/opts.rb, line 29 def self.section_title title Paint[title, :bold] end
section_title_ref(ref)
click to toggle source
# File lib/upoj-rb/opts.rb, line 33 def self.section_title_ref ref Paint[ref, :underline] end
Public Instance Methods
help!()
click to toggle source
# File lib/upoj-rb/opts.rb, line 69 def help! self.on('-h', '--help', 'show this help and exit'){ puts self; exit 0 } end
on(*args)
click to toggle source
Calls superclass method
# File lib/upoj-rb/opts.rb, line 51 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
program_name()
click to toggle source
# File lib/upoj-rb/opts.rb, line 61 def program_name @program_name || File.basename($0) end
to_s()
click to toggle source
# File lib/upoj-rb/opts.rb, line 65 def to_s "#{super}#{summary_examples_section}#{@footer}" end
usage!()
click to toggle source
# File lib/upoj-rb/opts.rb, line 73 def usage! self.on('-u', '--usage', 'show this help and exit'){ puts self; exit 0 } end
Private Instance Methods
summary_examples_section()
click to toggle source
# File lib/upoj-rb/opts.rb, line 94 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()
click to toggle source
# File lib/upoj-rb/opts.rb, line 79 def summary_program_name Paint[program_name, :bold] end