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