summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLee Jarvis <leejarvis@fastmail.com>2021-05-27 19:08:38 +0100
committerLee Jarvis <leejarvis@fastmail.com>2021-05-27 19:08:38 +0100
commitd3e644fea146c815787d2889b459c16fd4810995 (patch)
tree0ce05408dda2465f8cf503b228819a865fd7c265
parent09e7db5ba53c94d32c57a100ec6bbf64b64d3afe (diff)
downloadslop-d3e644fea146c815787d2889b459c16fd4810995.tar.gz
Add support for --opt="" for blank arguments
This is especially useful when your default option value is a non-blank value and you want users to be able to overwrite it with a blank value Closes #266
-rw-r--r--lib/slop/parser.rb2
-rw-r--r--test/parser_test.rb9
2 files changed, 10 insertions, 1 deletions
diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb
index 00b7e33..49d0230 100644
--- a/lib/slop/parser.rb
+++ b/lib/slop/parser.rb
@@ -52,7 +52,7 @@ module Slop
# support `foo=bar`
orig_flag = flag.dup
- if match = flag.match(/([^=]+)=([^=]+)/)
+ if match = flag.match(/([^=]+)=([^=]*)/)
flag, arg = match.captures
end
diff --git a/test/parser_test.rb b/test/parser_test.rb
index ef09714..8ac9345 100644
--- a/test/parser_test.rb
+++ b/test/parser_test.rb
@@ -28,6 +28,15 @@ describe Slop::Parser do
assert_equal %w(=), @result.args
end
+ it "parses flag=''" do
+ @options.string "--str"
+ @options.array "--arr", default: ["array"]
+ @result.parser.parse %(--str="" --arr="").shellsplit
+
+ assert_equal "", @result[:str]
+ assert_equal [], @result[:arr]
+ end
+
it "parses arg with leading -" do
@options.string "-t", "--text"
@result.parser.parse %w(--name=bob --text --sometext)