lib/slop/option.rb in slop-4.9.3 vs lib/slop/option.rb in slop-4.10.0
- old
+ new
@@ -54,11 +54,15 @@
@value = default_value
elsif !suppress_errors?
raise Slop::MissingArgument.new("missing argument for #{flag}", flags)
end
else
- @value = call(value)
+ if validate_type? && !valid?(value) && !suppress_errors?
+ raise Slop::InvalidOptionValue.new("invalid value for #{flag}", flags)
+ end
+
+ @value = valid?(value) && call(value)
end
block.call(@value) if block.respond_to?(:call)
end
@@ -105,19 +109,33 @@
# Returns true if an exception should be raised when this option isn't supplied.
def required?
config[:required]
end
+ # Returns true if an exception should be raised when this option value can't
+ # be parsed into the desired type or does not conform to the expected type's
+ # format
+ def validate_type?
+ config[:validate_type] || config[:validate_types]
+ end
+
# Returns all flags joined by a comma. Used by the help string.
def flag
flags.join(", ")
end
# Returns the last key as a symbol. Used in Options.to_hash.
def key
key = config[:key] || flags.last.sub(/\A--?/, '')
key = key.tr '-', '_' if underscore_flags?
key.to_sym
+ end
+
+ # Override this if you want to provide a custom validator for a type. This
+ # method must return whether the provided value is valid for the current
+ # argument's type
+ def valid?(value)
+ true
end
# Returns true if this option should be displayed with dashes transformed into underscores.
def underscore_flags?
config[:underscore_flags]