refactor using _gather()

This commit is contained in:
Josh Holtrop 2013-01-21 16:45:00 -05:00
parent 1a5de331d9
commit 91846e741e

View File

@ -18,7 +18,7 @@ module Yawpa
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 leader, param_name, val = $1, $2, $3
case leader.length case leader.length
when 2 when 2
@ -37,22 +37,18 @@ module Yawpa
if nargs == 0 if nargs == 0
opts[param_key] = true opts[param_key] = true
elsif nargs.class == Fixnum elsif nargs.class == Fixnum
n_gathered = 0
opts[param_key] = [] opts[param_key] = []
if val opts[param_key] << val if val
opts[param_key] << val[1, val.length] if opts[param_key].length < nargs
n_gathered += 1 gathered = _gather(i + 1, nargs - opts[param_key].length, params)
end i += gathered.length
while n_gathered < nargs opts[param_key] += gathered
if i + 1 >= params.length if opts[param_key].length < nargs
raise InvalidArgumentsException.new("Not enough arguments supplied for option '#{param_name}'") raise InvalidArgumentsException.new("Not enough arguments supplied for option '#{param_name}'")
end end
i += 1
opts[param_key] << params[i]
n_gathered += 1
end end
if n_gathered == 1 if opts[param_key].length == 1
opts[param_key] = opts[param_key][0] opts[param_key] = opts[param_key].first
end end
elsif nargs.class == Range elsif nargs.class == Range
end end
@ -64,4 +60,17 @@ module Yawpa
end end
return [opts, args] return [opts, args]
end end
def _gather(start_idx, max, params)
result = []
index = start_idx
loop do
break if index >= params.length
break if params[index][0] == '-'
result << params[index]
index += 1
break if result.length == max
end
result
end
end end