diff --git a/src/cligen/flag.cr b/src/cligen/flag.cr index c301de2..45ddde7 100644 --- a/src/cligen/flag.cr +++ b/src/cligen/flag.cr @@ -248,16 +248,39 @@ module CliGen {% elsif T == Time %} parse_time(raw) {% elsif T <= Array %} - {% elem = T.type_vars.first %} - {% if elem == Int32 %} - raw.split(@delimiter).map(&.to_i) - {% elsif elem == String %} - raw.split(@delimiter) - {% elsif elem.class < CliGen::Coercable %} - raw.split(@delimiter).map{|i| {{elem}}.coerce(i)} - {% 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 %} + 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 %} + abort "ERROR : Flag({{T}}, long: #{@long_key}) : #{val} is not an integer" unless val =~ /^[[:digit:]]+$/ + val.to_i + {% elsif elem == String %} + val + {% elsif elem.class < CliGen::Coercable %} + {{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 %}