summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNaoki Mizuno <nigorojr@gmail.com>2015-09-08 08:40:29 -0400
committerNaoki Mizuno <nigorojr@gmail.com>2015-09-08 08:42:07 -0400
commitf8012062596783e0e518e3f11d94bb2916a5f84f (patch)
tree06c9f81e297b023bed3485440060d5ac08c0e51e /lib
parent380903aa76757fd66dc1b95e193fcc5a5ab7256e (diff)
downloadslop-f8012062596783e0e518e3f11d94bb2916a5f84f.tar.gz
Fix arguments removed with option arguments
See #181.
Diffstat (limited to 'lib')
-rw-r--r--lib/slop/parser.rb17
1 files changed, 16 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