summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNaoki Mizuno <nigorojr@gmail.com>2015-09-08 15:40:07 -0400
committerNaoki Mizuno <nigorojr@gmail.com>2015-09-08 15:40:07 -0400
commit3df82ee30f7330d8137001c39737c5d7ebf6fcab (patch)
treec18a13bad73e41a4b6b5b4abc85b2f9a30df72fa
parentebe678b1639654604b3e54b08ca710c38682b83e (diff)
downloadslop-3df82ee30f7330d8137001c39737c5d7ebf6fcab.tar.gz
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.
-rw-r--r--lib/slop/parser.rb9
-rw-r--r--test/parser_test.rb5
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