diff options
author | Lee Jarvis <leejarvis@fastmail.com> | 2016-02-18 15:28:33 +0000 |
---|---|---|
committer | Lee Jarvis <leejarvis@fastmail.com> | 2016-02-18 15:28:33 +0000 |
commit | 1f2b9fe1a5d2fd1df0832e2caae3cc390b16647e (patch) | |
tree | 0df093b014357756788bb8a1ca7117024c23cc6c | |
parent | 2a66aadabc37b512d449aaf45d34cb3b2e0cfc7a (diff) | |
parent | 082e69de977ac58d0981e807a1cb33a1ed2958c3 (diff) | |
download | slop-1f2b9fe1a5d2fd1df0832e2caae3cc390b16647e.tar.gz |
Merge pull request #189 from mwpastore/master
Allow disabling the delimiter for array arguments
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | lib/slop/types.rb | 8 | ||||
-rw-r--r-- | test/types_test.rb | 6 |
3 files changed, 15 insertions, 2 deletions
@@ -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] |