add _massage_options() to simplify parse()
This commit is contained in:
parent
ef860a8eee
commit
40ecadd056
42
lib/yawpa.rb
42
lib/yawpa.rb
@ -13,38 +13,29 @@ module Yawpa
|
|||||||
|
|
||||||
module_function
|
module_function
|
||||||
def parse(params, options)
|
def parse(params, options)
|
||||||
|
options = _massage_options(options)
|
||||||
opts = {}
|
opts = {}
|
||||||
args = []
|
args = []
|
||||||
i = 0
|
i = 0
|
||||||
while i < params.length
|
while i < params.length
|
||||||
param = params[i]
|
param = params[i]
|
||||||
if param =~ /^(--?)([^=]+)(?:=(.+))?$/
|
if param =~ /^--([^=]+)(?:=(.+))?$/
|
||||||
leader, param_name, val = $1, $2, $3
|
param_name, val = $1, $2
|
||||||
case leader.length
|
if options[param_name].nil?
|
||||||
when 2
|
|
||||||
param_key = if options[param_name]
|
|
||||||
param_name
|
|
||||||
elsif options[param_name.to_sym]
|
|
||||||
param_name.to_sym
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
if param_key.nil?
|
|
||||||
raise UnknownOptionException.new("Unknown option '#{param_name}'")
|
raise UnknownOptionException.new("Unknown option '#{param_name}'")
|
||||||
end
|
end
|
||||||
opt_config = options[param_key]
|
opt_config = options[param_name]
|
||||||
nargs = opt_config[:nargs] || 0
|
param_key = opt_config[:key]
|
||||||
if nargs == 0
|
if opt_config[:nargs].last == 0
|
||||||
opts[param_key] = true
|
opts[param_key] = true
|
||||||
else
|
else
|
||||||
nargs = (nargs..nargs) if nargs.class == Fixnum
|
|
||||||
opts[param_key] = []
|
opts[param_key] = []
|
||||||
opts[param_key] << val if val
|
opts[param_key] << val if val
|
||||||
if opts[param_key].length < nargs.last
|
if opts[param_key].length < opt_config[:nargs].last
|
||||||
gathered = _gather(i + 1, nargs.last - opts[param_key].length, params)
|
gathered = _gather(i + 1, opt_config[:nargs].last - opts[param_key].length, params)
|
||||||
i += gathered.length
|
i += gathered.length
|
||||||
opts[param_key] += gathered
|
opts[param_key] += gathered
|
||||||
if opts[param_key].length < nargs.first
|
if opts[param_key].length < opt_config[:nargs].first
|
||||||
raise InvalidArgumentsException.new("Not enough arguments supplied for option '#{param_name}'")
|
raise InvalidArgumentsException.new("Not enough arguments supplied for option '#{param_name}'")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -52,7 +43,6 @@ module Yawpa
|
|||||||
opts[param_key] = opts[param_key].first
|
opts[param_key] = opts[param_key].first
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
else
|
else
|
||||||
args << params[i]
|
args << params[i]
|
||||||
end
|
end
|
||||||
@ -73,4 +63,16 @@ module Yawpa
|
|||||||
end
|
end
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def _massage_options(options)
|
||||||
|
{}.tap do |newopts|
|
||||||
|
options.each_pair do |k, v|
|
||||||
|
newkey = k.to_s
|
||||||
|
newopts[newkey] = {key: k}
|
||||||
|
nargs = v[:nargs] || 0
|
||||||
|
nargs = (nargs..nargs) if nargs.class == Fixnum
|
||||||
|
newopts[newkey][:nargs] = nargs
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user