Allowed delim in macros
This commit is contained in:
@@ -29,7 +29,8 @@ module CliGen
|
||||
default: {% unless var.default_value.nil? %} {{var.default_value}} {% else %} nil {% end %},
|
||||
validate: {% if anno[:validation] %} {{anno[:validation]}} {% else %} nil {% end %},
|
||||
on_match: {% if anno[:on_match] %} {{anno[:on_match]}} {% else %} nil {% end %},
|
||||
options: {% if anno[:options] %} {{anno[:options]}} {% else %} nil {% end %}
|
||||
options: {% if anno[:options] %} {{anno[:options]}} {% else %} nil {% end %},
|
||||
delimiter: {% if anno[:delimiter %} {{anno[:delimiter]}} {% else %} nil {% end %}
|
||||
)
|
||||
{% debug if env("DEBUG") %}
|
||||
{% end %}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
module CliGen
|
||||
class Command
|
||||
macro argument(variable, description, long = nil, short = nil, validation = nil, on_match = nil, def_setter = false, def_getter = false, options = nil)
|
||||
macro argument(variable, description, long = nil, short = nil, validation = nil, on_match = nil, def_setter = false, def_getter = false, options = nil, delimiter = nil)
|
||||
{% raise "ERROR : CliGen::Command.argument : def_setter must be a Bool" unless def_setter.is_a? BoolLiteral %}
|
||||
{% raise "ERROR : CliGen::Command.argument : First argument (#{variable}) must be a TypeDeclaration (ex: '<var> : <type> [= val]')" unless variable.is_a? TypeDeclaration %}
|
||||
{% name = variable.var %}
|
||||
{% type = variable.type %}
|
||||
{% if type <= Array %}
|
||||
{% delimiter = "," unless delimiter.is_a? StringLiteral %}
|
||||
{% end %}
|
||||
{% if short %}
|
||||
{% raise "ERROR : CliGen::Command.argument(#{name}) : Provided short must be a string" unless short.is_a? StringLiteral %}
|
||||
{% end %}
|
||||
@@ -37,7 +40,7 @@ module CliGen
|
||||
{% raise "ERROR : CliGen::Command.argument(#{name}) : Provided options must be an ArrayLiteral" unless options.is_a? ArrayLiteral %}
|
||||
{% end %}
|
||||
|
||||
@[CliGen::Argument(short: {{short}}, long: {{long}}, description: {{description}}, validation: {{validation}}, on_match: {{on_match}}, options: {{options}})]
|
||||
@[CliGen::Argument(short: {{short}}, long: {{long}}, description: {{description}}, validation: {{validation}}, on_match: {{on_match}}, options: {{options}}, delimiter: {{delimiter}})]
|
||||
@{{variable}}
|
||||
|
||||
{% if def_getter %}
|
||||
|
||||
+9
-8
@@ -8,13 +8,15 @@ module CliGen
|
||||
getter long_key : String
|
||||
getter env_var : String
|
||||
getter description : String
|
||||
getter delimiter : String
|
||||
|
||||
def initialize(
|
||||
@var : String,
|
||||
@short : String?,
|
||||
@long : String,
|
||||
@env_var : String,
|
||||
@description : String
|
||||
@description : String,
|
||||
@delimiter : String
|
||||
)
|
||||
# if the user provides just a "--long" I want the @long_key to match it
|
||||
if @long =~ /\s|=/
|
||||
@@ -47,6 +49,7 @@ module CliGen
|
||||
long : String,
|
||||
env_var : String,
|
||||
description : String,
|
||||
delimiter : String = ",",
|
||||
@default : T? = nil,
|
||||
@options : Array(T)? = nil,
|
||||
@validate : (T -> Bool)? = nil,
|
||||
@@ -79,9 +82,8 @@ module CliGen
|
||||
{% elsif elem == String %}
|
||||
(@value ||= [] of String) << arg.value
|
||||
{% elsif elem.class < CliGen::Coercable %}
|
||||
{% delim = elem.has_constant?("DELIMITER") ? elem.constant("DELIMITER") : ',' %}
|
||||
if arg.value.includes?({{delim}})
|
||||
@value = (@value || [] of {{elem}}) + arg.value.split({{delim}}).map{|i| {{elem}}.coerce(i)}
|
||||
if arg.value.includes?(@delimiter)
|
||||
@value = (@value || [] of {{elem}}) + arg.value.split(@delimiter).map{|i| {{elem}}.coerce(i)}
|
||||
else
|
||||
@value = (@value || [] of {{elem}}) + [({{elem}}.coerce(arg.value))]
|
||||
end
|
||||
@@ -181,12 +183,11 @@ module CliGen
|
||||
{% elsif T <= Array %}
|
||||
{% elem = T.type_vars.first %}
|
||||
{% if elem == Int32 %}
|
||||
raw.split(',').map(&.to_i)
|
||||
raw.split(@delimiter).map(&.to_i)
|
||||
{% elsif elem == String %}
|
||||
raw.split(',')
|
||||
raw.split(@delimiter)
|
||||
{% elsif elem.class < CliGen::Coercable %}
|
||||
{% delim = elem.has_constant?("DELIMITER") ? elem.constant("DELIMITER") : ',' %}
|
||||
raw.split({{delim}}).map{|i| {{elem}}.coerce(i)}
|
||||
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 %}
|
||||
|
||||
Reference in New Issue
Block a user