Had to fix some things in coerce

This commit is contained in:
Tristan Ancelet
2026-08-14 15:32:44 -05:00
parent 09389cb5e9
commit 2e7c1df7ed
+26 -3
View File
@@ -248,16 +248,39 @@ module CliGen
{% elsif T == Time %}
parse_time(raw)
{% elsif T <= Array %}
if raw.includes?(@delimiter)
{% elem = T.type_vars.first %}
raw.split(@delimiter).map do |val|
unless @format.nil?
abort "ERROR : Flag({{T}}, long: #{@long_key}) : #{val} does not match a valid format" unless val =~ @format
end
{% if elem == Int32 %}
raw.split(@delimiter).map(&.to_i)
abort "ERROR : Flag({{T}}, long: #{@long_key}) : #{val} is not an integer" unless val =~ /^[[:digit:]]+$/
val.to_i
{% elsif elem == String %}
raw.split(@delimiter)
val
{% elsif elem.class < CliGen::Coercable %}
raw.split(@delimiter).map{|i| {{elem}}.coerce(i)}
{{elem}}.coerce(val)
{% else %}
{% raise "ERROR : Flag(#{T}) : #{elem} is not a coercable type. If you wish to coerce it from a bare string extend CliGen::Coercable & implement the class method" %}
{% end %}
end
else
unless @format.nil?
abort "ERROR : Flag({{T}}, long: #{@long_key}) : #{raw} does not match a valid format" unless raw =~ @format
end
{% if elem == Int32 %}
abort "ERROR : Flag({{T}}, long: #{@long_key}) : #{raw} is not an integer" unless raw =~ /^[[:digit:]]+$/
[ raw.to_i ]
{% elsif elem == String %}
[ raw ]
{% elsif elem.class < CliGen::Coercable %}
[ {{elem}}.coerce(raw) ]
{% else %}
{% raise "ERROR : Flag(#{T}) : #{T} is not a coercable type. If you wish to coerce it from a bare string extend CliGen::Coercable & implement the class method" %}
{% end %}
end
{% elsif T == String %} # String
raw
{% elsif T.class < CliGen::Coercable %}