From 90d2a938d558ad974b04e4b2eea5528ac12b946e Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 22 Jan 2013 14:03:27 -0500 Subject: [PATCH] return option value for short option also --- lib/yawpa.rb | 11 ++++++++--- spec/yawpa_spec.rb | 10 ++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/yawpa.rb b/lib/yawpa.rb index fe81cb1..6fa04d2 100644 --- a/lib/yawpa.rb +++ b/lib/yawpa.rb @@ -39,9 +39,6 @@ module Yawpa 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 @@ -76,6 +73,14 @@ module Yawpa end i += 1 end + + # Condense 1-element arrays of option values to just the element itself + opts.each_key do |k| + if opts[k].class == Array and opts[k].length == 1 + opts[k] = opts[k].first + end + end + return [opts, args] end diff --git a/spec/yawpa_spec.rb b/spec/yawpa_spec.rb index f648d92..35a9c75 100644 --- a/spec/yawpa_spec.rb +++ b/spec/yawpa_spec.rb @@ -107,5 +107,15 @@ describe Yawpa do opts[:option].should be_true args.should eq(['qqq']) end + + it "returns option argument at next position for a short option" do + options = { + option: {nargs: 1, short: 'o'}, + } + params = ['-o', 'val', 'rrr'] + opts, args = Yawpa.parse(params, options) + opts[:option].should eq('val') + args.should eq(['rrr']) + end end end