diff options
-rw-r--r-- | lib/slop/parser.rb | 17 | ||||
-rw-r--r-- | test/parser_test.rb | 5 |
2 files changed, 21 insertions, 1 deletions
diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb index 1e810d8..d1410a3 100644 --- a/lib/slop/parser.rb +++ b/lib/slop/parser.rb @@ -60,8 +60,12 @@ module Slop if opt = try_process(flag, arg) # since the option was parsed, we remove it from our # arguments (plus the arg if necessary) + # delete argument first so that it doesn't mess up the index + if opt.expects_argument? + index = arg_index(flag, arg) + arguments.delete_at(index) if !index.nil? + end arguments.delete(flag) - arguments.delete(arg) if opt.expects_argument? end end @@ -116,5 +120,16 @@ module Slop def matching_option(flag) options.find { |o| o.flags.include?(flag) } end + + # Returns the index of the argument corresponding to a flag. + def arg_index(flag, arg) + flag_index = arguments.index(flag) + return nil if flag_index.nil? + + arg_index = arguments[flag_index..-1].index(arg) + return nil if arg_index.nil? + + flag_index + arg_index + end end end diff --git a/test/parser_test.rb b/test/parser_test.rb index 85ba7e0..bfc4f7e 100644 --- a/test/parser_test.rb +++ b/test/parser_test.rb @@ -66,5 +66,10 @@ describe Slop::Parser do @parser.parse %w(-v -- --name lee) assert_equal %w(--name lee), @parser.arguments end + + it "correctly removes the option argument" do + @parser.parse %w(lee --name lee lee) + assert_equal %w(lee lee), @parser.arguments + end end end |