process configured short options
This commit is contained in:
parent
40ecadd056
commit
cf049aed2f
38
lib/yawpa.rb
38
lib/yawpa.rb
@ -36,13 +36,41 @@ module Yawpa
|
|||||||
i += gathered.length
|
i += gathered.length
|
||||||
opts[param_key] += gathered
|
opts[param_key] += gathered
|
||||||
if opts[param_key].length < opt_config[: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_key}'")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if opts[param_key].length == 1
|
if opts[param_key].length == 1
|
||||||
opts[param_key] = opts[param_key].first
|
opts[param_key] = opts[param_key].first
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
elsif param =~ /^-(.+)$/
|
||||||
|
short_flags = $1
|
||||||
|
short_idx = 0
|
||||||
|
while short_idx < short_flags.length
|
||||||
|
opt_config = _find_opt_config_by_short_name(options, short_flags[short_idx])
|
||||||
|
if opt_config.nil?
|
||||||
|
raise UnknownOptionException.new("Unknown option '-#{short_flags[short_idx]}'")
|
||||||
|
end
|
||||||
|
param_key = opt_config[:key]
|
||||||
|
if opt_config[:nargs].last == 0
|
||||||
|
opts[param_key] = true
|
||||||
|
else
|
||||||
|
opts[param_key] = []
|
||||||
|
if short_idx + 1 < short_flags.length
|
||||||
|
opts[param_key] << short_flags[short_idx + 1, short_flags.length]
|
||||||
|
end
|
||||||
|
if opts[param_key].length < opt_config[:nargs].last
|
||||||
|
gathered = _gather(i + 1, opt_config[:nargs].last - opts[param_key].length, params)
|
||||||
|
i += gathered.length
|
||||||
|
opts[param_key] += gathered
|
||||||
|
if opts[param_key].length < opt_config[:nargs].first
|
||||||
|
raise InvalidArgumentsException.new("Not enough arguments supplied for option '#{param_key}'")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
break
|
||||||
|
end
|
||||||
|
short_idx += 1
|
||||||
|
end
|
||||||
else
|
else
|
||||||
args << params[i]
|
args << params[i]
|
||||||
end
|
end
|
||||||
@ -72,7 +100,15 @@ module Yawpa
|
|||||||
nargs = v[:nargs] || 0
|
nargs = v[:nargs] || 0
|
||||||
nargs = (nargs..nargs) if nargs.class == Fixnum
|
nargs = (nargs..nargs) if nargs.class == Fixnum
|
||||||
newopts[newkey][:nargs] = nargs
|
newopts[newkey][:nargs] = nargs
|
||||||
|
newopts[newkey][:short] = v[:short] || ''
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def _find_opt_config_by_short_name(options, short_name)
|
||||||
|
options.each_pair do |k, v|
|
||||||
|
return v if v[:short] == short_name
|
||||||
|
end
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -97,5 +97,15 @@ describe Yawpa do
|
|||||||
opts['crazy-option'].should eq('yyy')
|
opts['crazy-option'].should eq('yyy')
|
||||||
args.should eq(['xxx', 'zzz'])
|
args.should eq(['xxx', 'zzz'])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "accepts short options corresponding to a long option" do
|
||||||
|
options = {
|
||||||
|
option: {short: 'o'},
|
||||||
|
}
|
||||||
|
params = ['-o', 'qqq']
|
||||||
|
opts, args = Yawpa.parse(params, options)
|
||||||
|
opts[:option].should be_true
|
||||||
|
args.should eq(['qqq'])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user