From c026ee99247c33130addc01d1399e71b14879f38 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Mon, 24 Jun 2019 19:01:13 +0900 Subject: Fix a bug that flag value is processed as flag If flag value starts with "-", unknown option error is raised. The current flag value check is "orig_arg == opt.value.to_s". There are some objects such as Regexp and Time that input value and its #to_s aren't same. --- lib/slop/parser.rb | 9 ++++++++- test/parser_test.rb | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb index 27e1c50..c6462ea 100644 --- a/lib/slop/parser.rb +++ b/lib/slop/parser.rb @@ -64,7 +64,7 @@ module Slop if opt.expects_argument? # if we consumed the argument, remove the next pair - if orig_arg == opt.value.to_s + if consume_next_argument?(orig_flag) pairs.delete_at(idx + 1) end @@ -106,6 +106,13 @@ module Slop private + def consume_next_argument?(flag) + return false if flag.include?("=") + return true if flag.start_with?("--") + return true if /\A-[a-zA-Z]\z/ === flag + false + end + # We've found an option, process and return it def process(option, arg) option.ensure_call(arg) diff --git a/test/parser_test.rb b/test/parser_test.rb index caee0dd..ef09714 100644 --- a/test/parser_test.rb +++ b/test/parser_test.rb @@ -35,6 +35,12 @@ describe Slop::Parser do assert_equal "--sometext", @result[:text] end + it "parses regexp arg with leading -" do + @options.regexp "--pattern" + @result.parser.parse %w(--pattern -x) + assert_equal(/-x/, @result[:pattern]) + end + it "parses negative integer" do @options.integer "-p", "--port" @result.parser.parse %w(--name=bob --port -123) -- cgit v1.2.1