summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLee Jarvis <leejarvis@fastmail.com>2017-04-29 16:48:47 +0100
committerLee Jarvis <leejarvis@fastmail.com>2017-04-29 16:48:47 +0100
commited049cb2aa7ce30a67661449e75b883f9969722f (patch)
treeb4ddb3b130e05f702eabe51b759ec219afa19e64
parent57af528a96d27732202effc039f183b1733cb64d (diff)
downloadslop-ed049cb2aa7ce30a67661449e75b883f9969722f.tar.gz
Avoid deleting args after '--'
Everything after -- is sacred and shouldn't be mutated. So lets just remove it all straight away and add it to our arguments list after we're done parsing Closes #194
-rw-r--r--lib/slop/parser.rb20
-rw-r--r--test/parser_test.rb3
2 files changed, 16 insertions, 7 deletions
diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb
index 142540a..2b20d36 100644
--- a/lib/slop/parser.rb
+++ b/lib/slop/parser.rb
@@ -36,6 +36,9 @@ module Slop
def parse(strings)
reset # reset before every parse
+ # ignore everything after "--"
+ strings, ignored_args = partition(strings)
+
pairs = strings.each_cons(2).to_a
# this ensures we still support the last string being a flag,
# otherwise it'll only be used as an argument.
@@ -47,12 +50,6 @@ module Slop
flag, arg = pair
break if !flag
- # ignore everything after '--', flag or not
- if flag == '--'
- arguments.delete(flag)
- break
- end
-
# support `foo=bar`
orig_flag = flag.dup
orig_arg = arg
@@ -81,6 +78,8 @@ module Slop
end
end
+ @arguments += ignored_args
+
Result.new(self).tap do |result|
used_options.each { |o| o.finish(result) }
end
@@ -145,5 +144,14 @@ module Slop
def matching_option(flag)
options.find { |o| o.flags.include?(flag) }
end
+
+ private def partition(strings)
+ if strings.include?("--")
+ partition_idx = strings.index("--")
+ [strings[0..partition_idx-1], strings[partition_idx+1..-1]]
+ else
+ [strings, []]
+ end
+ end
end
end
diff --git a/test/parser_test.rb b/test/parser_test.rb
index fa372cf..dcc3661 100644
--- a/test/parser_test.rb
+++ b/test/parser_test.rb
@@ -11,8 +11,9 @@ describe Slop::Parser do
end
it "ignores everything after --" do
- @parser.parse %w(-v -- --name lee)
+ @parser.parse %w(-v -- -v --name lee)
assert_equal [@verbose], @parser.used_options
+ assert_equal ["-v", "--name", "lee"], @parser.arguments
end
it "parses flag=argument" do