diff options
author | Naoki Mizuno <nigorojr@gmail.com> | 2015-09-08 08:40:29 -0400 |
---|---|---|
committer | Naoki Mizuno <nigorojr@gmail.com> | 2015-09-08 08:42:07 -0400 |
commit | f8012062596783e0e518e3f11d94bb2916a5f84f (patch) | |
tree | 06c9f81e297b023bed3485440060d5ac08c0e51e | |
parent | 380903aa76757fd66dc1b95e193fcc5a5ab7256e (diff) | |
download | slop-f8012062596783e0e518e3f11d94bb2916a5f84f.tar.gz |
Fix arguments removed with option arguments
See #181.
-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 |