diff options
author | Lee Jarvis <leejarvis@fastmail.com> | 2015-09-10 09:43:08 +0100 |
---|---|---|
committer | Lee Jarvis <leejarvis@fastmail.com> | 2015-09-10 09:43:08 +0100 |
commit | 2c532b9a153ffbb982d67d1297e0e94fb08f7040 (patch) | |
tree | c18a13bad73e41a4b6b5b4abc85b2f9a30df72fa | |
parent | 380903aa76757fd66dc1b95e193fcc5a5ab7256e (diff) | |
parent | 3df82ee30f7330d8137001c39737c5d7ebf6fcab (diff) | |
download | slop-2c532b9a153ffbb982d67d1297e0e94fb08f7040.tar.gz |
Merge pull request #182 from NigoroJr/option_args
Fix arguments removed with option arguments
-rw-r--r-- | lib/slop/parser.rb | 12 | ||||
-rw-r--r-- | test/parser_test.rb | 10 |
2 files changed, 20 insertions, 2 deletions
diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb index 1e810d8..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,8 +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) - arguments.delete(flag) - arguments.delete(arg) if opt.expects_argument? + # delete argument first while we can find its index. + if opt.expects_argument? + arguments.each_with_index do |argument, i| + if argument == orig_flag && !orig_flag.include?("=") + arguments.delete_at(i + 1) + end + end + end + arguments.delete(orig_flag) end end diff --git a/test/parser_test.rb b/test/parser_test.rb index 85ba7e0..1099871 100644 --- a/test/parser_test.rb +++ b/test/parser_test.rb @@ -66,5 +66,15 @@ 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 + + it "correctly removes options that use =" do + @parser.parse %w(lee --name=lee lee) + assert_equal %w(lee lee), @parser.arguments + end end end |