def self.parse_argv(argv, single_options=nil, argneed_options=nil)
options = {}
toppings = {}
return options, toppings if !argv
opttypes = {}
single_options.each_byte do |ch|
opttypes[ch] = :single
end if single_options
argneed_options.each_byte do |ch|
opttypes[ch] = :arg_need
end if argneed_options
opttypes[?-] = :topping
while argv[0] && argv[0][0] == ?- do
optstr = argv.shift
optstr = optstr[1, optstr.length-1]
while optstr && !optstr.empty? do
optchar = optstr[0]
optstr[0] = ''
optstr = nil if optstr && optstr.empty?
case opttypes[optchar]
when :single
options[optchar] = true
when :arg_need
arg = optstr || argv.shift
raise CommandOptionError.new("-#{optchar.chr}: argument reguired.") unless arg
options[optchar] = arg
optstr = nil
when :topping
if optstr == 'help' then
options[?h] = true
elsif optstr =~ /^(\w+)(=.*)?/ then
key = $1.intern
value = $2
if !value || value.empty? then
value = true
else
value.sub!(/^=/, '')
value = str2value(value)
end
toppings[key] = value
end
optstr = nil
else
raise CommandOptionError.new("-#{optchar.chr}: invalid option.")
end
end
end
return options, toppings
end