summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pastore <mike@oobak.org>2016-02-17 18:04:00 -0600
committerMike Pastore <mike@oobak.org>2016-02-18 02:54:05 -0600
commit082e69de977ac58d0981e807a1cb33a1ed2958c3 (patch)
tree0df093b014357756788bb8a1ca7117024c23cc6c
parent2a66aadabc37b512d449aaf45d34cb3b2e0cfc7a (diff)
downloadslop-082e69de977ac58d0981e807a1cb33a1ed2958c3.tar.gz
Allow disabling the delimiter for array arguments
-rw-r--r--README.md3
-rw-r--r--lib/slop/types.rb8
-rw-r--r--test/types_test.rb6
3 files changed, 15 insertions, 2 deletions
diff --git a/README.md b/README.md
index a172a39..4cb690d 100644
--- a/README.md
+++ b/README.md
@@ -132,6 +132,9 @@ end
# --files foo.txt --files bar.rb
```
+If you want to disable the built-in string-splitting, set the delimiter to
+`nil`.
+
Custom option types
-------------------
diff --git a/lib/slop/types.rb b/lib/slop/types.rb
index 3848b5e..7e80544 100644
--- a/lib/slop/types.rb
+++ b/lib/slop/types.rb
@@ -62,7 +62,11 @@ module Slop
class ArrayOption < Option
def call(value)
@value ||= []
- @value.concat value.split(delimiter, limit)
+ if delimiter
+ @value.concat value.split(delimiter, limit)
+ else
+ @value << value
+ end
end
def default_value
@@ -70,7 +74,7 @@ module Slop
end
def delimiter
- config[:delimiter] || ","
+ config.fetch(:delimiter, ",")
end
def limit
diff --git a/test/types_test.rb b/test/types_test.rb
index f806c48..43c356e 100644
--- a/test/types_test.rb
+++ b/test/types_test.rb
@@ -67,6 +67,7 @@ describe Slop::ArrayOption do
before do
@options = Slop::Options.new
@files = @options.array "--files"
+ @multi = @options.array "-M", delimiter: nil
@delim = @options.array "-d", delimiter: ":"
@limit = @options.array "-l", limit: 2
@result = @options.parse %w(--files foo.txt,bar.rb)
@@ -85,6 +86,11 @@ describe Slop::ArrayOption do
assert_equal %w(foo.txt bar.rb), @result[:files]
end
+ it "collects multiple option values with no delimiter" do
+ @result.parser.parse %w(-M foo,bar -M bar,qux)
+ assert_equal %w(foo,bar bar,qux), @result[:M]
+ end
+
it "can use a custom delimiter" do
@result.parser.parse %w(-d foo.txt:bar.rb)
assert_equal %w(foo.txt bar.rb), @result[:d]