summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSutou Kouhei <kou@clear-code.com>2019-06-24 19:01:13 +0900
committerSutou Kouhei <kou@clear-code.com>2019-06-29 06:18:09 +0900
commitc026ee99247c33130addc01d1399e71b14879f38 (patch)
treeb50c90fbcefadd3d2e3b73792eba68ac37bd9617
parentaa233ababf00cd32f46a278ca030d5c40281e7c1 (diff)
downloadslop-c026ee99247c33130addc01d1399e71b14879f38.tar.gz
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.
-rw-r--r--lib/slop/parser.rb9
-rw-r--r--test/parser_test.rb6
2 files changed, 14 insertions, 1 deletions
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)