summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLee Jarvis <leejarvis@fastmail.com>2019-09-09 11:57:11 +0100
committerGitHub <noreply@github.com>2019-09-09 11:57:11 +0100
commit494c184de62242e0bdb5c2f879647628bae32e3f (patch)
tree74221993bfd0ca0745c657b743be870df672ace5
parent4e60891d9bed6d08b8ab46634b0239f11f2ca4bf (diff)
parent4289f4c62d546ea5af8b3a526e4606d7ed01e64c (diff)
downloadslop-494c184de62242e0bdb5c2f879647628bae32e3f.tar.gz
Merge pull request #243 from jylitalo/plus_prefix
Support '+' as prefix for integer
-rw-r--r--lib/slop/types.rb2
-rw-r--r--test/types_test.rb6
2 files changed, 6 insertions, 2 deletions
diff --git a/lib/slop/types.rb b/lib/slop/types.rb
index b7e41c8..4cd6136 100644
--- a/lib/slop/types.rb
+++ b/lib/slop/types.rb
@@ -44,7 +44,7 @@ module Slop
# Cast the option argument to an Integer.
class IntegerOption < Option
def call(value)
- value =~ /\A-?\d+\z/ && value.to_i
+ value =~ /\A[+-]?\d+\z/ && value.to_i
end
end
IntOption = IntegerOption
diff --git a/test/types_test.rb b/test/types_test.rb
index c1aabaa..f6b928e 100644
--- a/test/types_test.rb
+++ b/test/types_test.rb
@@ -32,11 +32,15 @@ describe Slop::IntegerOption do
before do
@options = Slop::Options.new
@age = @options.integer "--age"
- @result = @options.parse %w(--age 20)
+ @minus = @options.integer "--minus"
+ @plus = @options.integer "--plus"
+ @result = @options.parse %w(--age 20 --minus -10 --plus +30)
end
it "returns the value as an integer" do
assert_equal 20, @result[:age]
+ assert_equal -10, @result[:minus]
+ assert_equal 30, @result[:plus]
end
it "returns nil for non-numbers by default" do