lib/samovar/options.rb in samovar-1.2.0 vs lib/samovar/options.rb in samovar-1.3.0
- old
+ new
@@ -32,29 +32,47 @@
@key = @flags.first.key
end
@default = default
+ # If the value is given, it overrides the user specified input.
@value = value
@value ||= true if @flags.boolean?
- @type = block_given? ? block : type
+ @type = type
+ @block = block
end
attr :flags
attr :description
attr :type
attr :default
attr :key
+ def coerce_type(result)
+ if @type == Integer
+ Integer(result)
+ elsif @type == Float
+ Float(result)
+ elsif @type.respond_to? :call
+ @type.call(result)
+ elsif @type.respond_to? :new
+ @type.new(result)
+ end
+ end
+
def coerce(result)
if @type
- @type.call(result)
- else
- result
+ result = coerce_type(result)
end
+
+ if @block
+ result = @block.call(result)
+ end
+
+ return result
end
def parse(input)
if result = @flags.parse(input)
@value.nil? ? coerce(result) : @value