class PmgmtLib::OptionsParser
Allows parsing of command line options without requiring any subclassing.
Attributes
opts[R]
remaining[R]
Public Class Methods
new(command_name, args)
click to toggle source
# File lib/optionsparser.rb, line 21 def initialize(command_name, args) @opts = OpenStruct.new @parser = create_parser(command_name) @args = args @validators = [] end
Public Instance Methods
add_option(option, assign, help)
click to toggle source
# File lib/optionsparser.rb, line 28 def add_option(option, assign, help) @parser.on(option, help) {|v| assign.call(@opts, v)} self end
add_typed_option(option, type, assign, help)
click to toggle source
# File lib/optionsparser.rb, line 33 def add_typed_option(option, type, assign, help) @parser.on(option, type, help) {|v| assign.call(@opts, v)} self end
add_validator(fn)
click to toggle source
# File lib/optionsparser.rb, line 38 def add_validator(fn) @validators.push(fn) self end
parse()
click to toggle source
# File lib/optionsparser.rb, line 43 def parse() begin @remaining = @parser.parse @args rescue ArgumentError => e STDERR.puts e STDERR.puts STDERR.puts @parser.help exit 1 end self end
validate()
click to toggle source
# File lib/optionsparser.rb, line 55 def validate() @validators.each do |fn| begin fn.call(@opts) rescue ArgumentError => e STDERR.puts e STDERR.puts STDERR.puts @parser.help exit 1 end end self end