From 3df82ee30f7330d8137001c39737c5d7ebf6fcab Mon Sep 17 00:00:00 2001 From: Naoki Mizuno Date: Tue, 8 Sep 2015 15:40:07 -0400 Subject: Fix bug regarding --foo=bar style options This fixes the bug introduced in ebe678b1639654604b3e54b08ca710c38682b83e where the argument after a flag is naively deleted. For example, with --foo=bar baz , both `--foo=bar' and `baz' are deleted when only `--foo=bar' should be deleted. --- lib/slop/parser.rb | 9 ++++++--- test/parser_test.rb | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb index 553c2ff..ea1869e 100644 --- a/lib/slop/parser.rb +++ b/lib/slop/parser.rb @@ -53,6 +53,7 @@ module Slop end # support `foo=bar` + orig_flag = flag.dup if flag.include?("=") flag, arg = flag.split("=") end @@ -60,13 +61,15 @@ 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 + # delete argument first while we can find its index. if opt.expects_argument? arguments.each_with_index do |argument, i| - arguments.delete_at(i + 1) if argument == flag + if argument == orig_flag && !orig_flag.include?("=") + arguments.delete_at(i + 1) + end end end - arguments.delete(flag) + arguments.delete(orig_flag) end end diff --git a/test/parser_test.rb b/test/parser_test.rb index bfc4f7e..1099871 100644 --- a/test/parser_test.rb +++ b/test/parser_test.rb @@ -71,5 +71,10 @@ describe Slop::Parser do @parser.parse %w(lee --name lee lee) assert_equal %w(lee lee), @parser.arguments end + + it "correctly removes options that use =" do + @parser.parse %w(lee --name=lee lee) + assert_equal %w(lee lee), @parser.arguments + end end end -- cgit v1.2.1