Skip to content

Commit

Permalink
Merge pull request #6 from at-grandpa/ref/refactoring
Browse files Browse the repository at this point in the history
Ref/refactoring
  • Loading branch information
at-grandpa authored Apr 7, 2017
2 parents ce3307f + 725c6b7 commit 2414b65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
11 changes: 5 additions & 6 deletions src/clim/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ class Clim
@@defining.usage = usage
end

macro difine_opts(type, base_default, &proc)
{% method_name = type.stringify.split("(").first.downcase.id %}
macro difine_opts(method_name, type, base_default, &proc)
{% for long_arg in ["long,", ""] %}
def {{method_name}}(short, {{long_arg.id}} default : {{type}} | Nil = nil, required = false, desc = "Option description.")
def {{method_name.id}}(short, {{long_arg.id}} default : {{type}} | Nil = nil, required = false, desc = "Option description.")
opt = Option({{type}}).new(
short: short,
long: {% if long_arg.empty? %} "", {% else %} {{long_arg.id}} {% end %}
Expand All @@ -51,9 +50,9 @@ class Clim
{% end %}
end

difine_opts(type: String, base_default: "") { |arg| opt.set_string(arg) }
difine_opts(type: Bool, base_default: false) { |arg| opt.set_bool(arg) }
difine_opts(type: Array(String), base_default: [] of String) { |arg| opt.add_to_array(arg) }
difine_opts(method_name: "string", type: String, base_default: "") { |arg| opt.set_string(arg) }
difine_opts(method_name: "bool", type: Bool, base_default: false) { |arg| opt.set_bool(arg) }
difine_opts(method_name: "array", type: Array(String), base_default: [] of String) { |arg| opt.add_to_array(arg) }

def run(&block : RunProc)
@@defining.run_proc = block
Expand Down
26 changes: 12 additions & 14 deletions src/clim/options.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,23 @@ class Clim
class Values
property help : String = ""

macro define_methods(*types)
{% for type in types %}
{% property_name = type.stringify.split("(").first.downcase %}
macro define_methods(property_name, type)
property {{property_name.id}} : Hash(String, {{type}}) = {} of String => {{type}}

property {{property_name.id}} : Hash(String, {{type.id}}) = {} of String => {{type.id}}
def {{property_name.split("").first.id}}
{{property_name.id}}
end

def {{property_name.split("").first.id}}
{{property_name.id}}
def merge!(hash : Hash(String, {{type}}))
{{property_name.id}}.merge!(hash) do |key, _, _|
raise ClimException.new "Duplicate {{property_name.id}} option. \"#{key}\""
end

def merge!(hash : Hash(String, {{type.id}}))
{{property_name.id}}.merge!(hash) do |key, _, _|
raise ClimException.new "Duplicate {{property_name.id}} option. \"#{key}\""
end
end
{% end %}
end
end

define_methods String, Bool, Array(String)
define_methods(property_name: "string", type: String)
define_methods(property_name: "bool", type: Bool)
define_methods(property_name: "array", type: Array(String))
end

def values
Expand Down

0 comments on commit 2414b65

Please sign in to comment.