diff options
-rw-r--r-- | lib/slop/types.rb | 2 | ||||
-rw-r--r-- | test/types_test.rb | 6 |
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 |