fix issues related to union types

This commit is contained in:
Chris Watson 2023-01-27 12:40:46 -07:00
parent 76c73977fc
commit 313440646a
2 changed files with 25 additions and 4 deletions

View File

@ -187,7 +187,7 @@ module ArgParser
#
# Note: You can override this method to change the way validation errors are handled.
def on_validation_error(key : String, value, errors : Array(String))
add_validation_error(key, erorrs)
add_validation_error(key, errors)
raise ValidationError.new(key, value, errors)
end
@ -198,9 +198,9 @@ module ArgParser
raise ConversionError.new(key, value, type)
end
def add_validation_error(key : String, value, errors : Array(String))
@errors[key] ||= [] of String
@errors[key].concat errors
def add_validation_error(key : String, errors : Array(String))
@_validation_errors[key] ||= [] of String
@_validation_errors[key].concat errors
end
# https://en.wikipedia.org/wiki/Quotation_mark#Summary_table

View File

@ -123,3 +123,24 @@ struct UUID
UUID.new(arg)
end
end
struct Nil
def self.from_arg(arg : String)
nil
end
end
def Union.from_arg(arg : String)
{% begin %}
{% for type in T %}
begin
%val = {{type}}.from_arg(arg)
return %val if %val.is_a?({{type}})
rescue
end
{% end %}
raise ArgParser::Error.new("Argument '#{arg}' cannot be converted to any of the union types: {{T}}")
{% end %}
end