summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRick Hull <rick.hull@gmail.com>2014-12-29 01:36:32 -0500
committerRick Hull <rick.hull@gmail.com>2014-12-29 12:31:20 -0500
commit1ef4327cef9f74caaafc2dcb8201aa71f0eff17d (patch)
tree208fa3000a76e28b0fa51c2f701fe8f46c9149e9
parent6e0db709d5c77d9cca1559da8a118d358734275f (diff)
downloadslop-1ef4327cef9f74caaafc2dcb8201aa71f0eff17d.tar.gz
add Float support
- in lib/ - in test/ - in README
-rw-r--r--README.md1
-rw-r--r--lib/slop/types.rb7
-rw-r--r--test/types_test.rb18
3 files changed, 26 insertions, 0 deletions
diff --git a/README.md b/README.md
index a12491d..f5338dc 100644
--- a/README.md
+++ b/README.md
@@ -46,6 +46,7 @@ Built in Option types are as follows:
o.string #=> Slop::StringOption, expects an argument
o.bool #=> Slop::BoolOption, no argument, aliased to BooleanOption
o.integer #=> Slop::IntegerOption, expects an argument, aliased to IntOption
+o.float #=> Slop::FloatOption, expects an argument
o.array #=> Slop::ArrayOption, expects an argument
o.null #=> Slop::NullOption, no argument and ignored from `to_hash`
o.on #=> alias for o.null
diff --git a/lib/slop/types.rb b/lib/slop/types.rb
index c7db245..cc10e63 100644
--- a/lib/slop/types.rb
+++ b/lib/slop/types.rb
@@ -27,6 +27,13 @@ module Slop
end
IntOption = IntegerOption
+ class FloatOption < Option
+ def call(value)
+ # TODO: scientific notation, etc.
+ value =~ /\A\d*\.*\d+\z/ && value.to_f
+ end
+ end
+
class ArrayOption < Option
def call(value)
@value ||= []
diff --git a/test/types_test.rb b/test/types_test.rb
index 3696126..e060df5 100644
--- a/test/types_test.rb
+++ b/test/types_test.rb
@@ -34,6 +34,24 @@ describe Slop::IntegerOption do
end
end
+describe Slop::FloatOption do
+ before do
+ @options = Slop::Options.new
+ @apr = @options.float "--apr"
+ @apr_value = 2.9
+ @result = @options.parse %W(--apr #{@apr_value})
+ end
+
+ it "returns the value as a float" do
+ assert_equal @apr_value, @result[:apr]
+ end
+
+ it "returns nil for non-numbers by default" do
+ @result.parser.reset.parse %w(--apr hello)
+ assert_equal nil, @result[:apr]
+ end
+end
+
describe Slop::ArrayOption do
before do
@options = Slop::Options.new