class Ki::SimpleOptionParser
simplified OptionParser, which supports unknown options by ignoring them
-
supports options in two formats: “-f file.txt” and “-f=file.txt”
-
supports short and long form: “–file” and “-f”
-
supports multiple parameters for options
Does not support
-
parameters with spaces
-
type conversions
-
optional parameter values
Public Class Methods
new(&block)
click to toggle source
# File lib/util/simple_optparse.rb, line 31 def initialize(&block) block.call(self) end
Public Instance Methods
find_option(a)
click to toggle source
# File lib/util/simple_optparse.rb, line 89 def find_option(a) options.each do |o| if a.start_with?(o[:opt] + "=") return o, a[o[:opt].size+1..-1] elsif a.start_with?(o[:opt]) return o,nil end end nil end
on(*args, &block)
click to toggle source
# File lib/util/simple_optparse.rb, line 35 def on(*args, &block) if block.nil? raise "Option without parser block: " + args.join(", ") end if args.size == 2 || args.size == 3 short = args.size == 3 ? args.delete_at(0) : nil long, *params = args.delete_at(0).split(" ") comment = args.delete_at(0) options_for_to_s << {short: short, long: long, comment: comment, params: params, block: block } if short options << {opt: short, comment: comment, params: params, block: block } end options << {opt: long, comment: comment, params: params, block: block } else raise "unsupported option configuration size: " + args.join(", ") end end
parse(args)
click to toggle source
# File lib/util/simple_optparse.rb, line 53 def parse(args) ret = [] open = nil collected_params = nil collect_count = nil args.each do |a| if open collected_params << a if collect_count == collected_params.size open[:block].call(*collected_params) open = nil end else found_option, rest_of_a = find_option(a) if found_option collect_count = found_option[:params].size if collect_count == 0 # no parameters found_option[:block].call elsif collect_count == 1 && rest_of_a && rest_of_a.size > 0 # single parameter was defined with opt=value found_option[:block].call rest_of_a else open = found_option collected_params = [] end else ret << a end end end if open raise "requires #{collect_count} parameters for '#{open[:opt]}', found only #{collected_params.size}: #{collected_params.join(", ")}" end ret end
to_s()
click to toggle source
# File lib/util/simple_optparse.rb, line 99 def to_s options_for_to_s.map do |o| format(" %2s%s %-29s%s",o[:short], o[:short] && o[:long]? ",": " ", o[:long], o[:comment] ) end.join("\n") end