From cf049aed2fa0c9094d6bea02b76cc005409a3426 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 22 Jan 2013 13:54:22 -0500 Subject: [PATCH] process configured short options --- lib/yawpa.rb | 38 +++++++++++++++++++++++++++++++++++++- spec/yawpa_spec.rb | 10 ++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/yawpa.rb b/lib/yawpa.rb index 431aefb..fe81cb1 100644 --- a/lib/yawpa.rb +++ b/lib/yawpa.rb @@ -36,13 +36,41 @@ module Yawpa 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_name}'") + raise InvalidArgumentsException.new("Not enough arguments supplied for option '#{param_key}'") end end if opts[param_key].length == 1 opts[param_key] = opts[param_key].first 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 args << params[i] end @@ -72,7 +100,15 @@ module Yawpa nargs = v[:nargs] || 0 nargs = (nargs..nargs) if nargs.class == Fixnum newopts[newkey][:nargs] = nargs + newopts[newkey][:short] = v[:short] || '' 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 diff --git a/spec/yawpa_spec.rb b/spec/yawpa_spec.rb index 08f2eb4..f648d92 100644 --- a/spec/yawpa_spec.rb +++ b/spec/yawpa_spec.rb @@ -97,5 +97,15 @@ describe Yawpa do opts['crazy-option'].should eq('yyy') args.should eq(['xxx', 'zzz']) 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