diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | lib/slop/types.rb | 7 | ||||
-rw-r--r-- | test/types_test.rb | 18 |
3 files changed, 26 insertions, 0 deletions
@@ -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 |