Skip to content

Commit

Permalink
Merge pull request #23 from at-grandpa/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
at-grandpa authored Mar 17, 2018
2 parents 8a8cabf + 962e176 commit 31fc594
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
20 changes: 8 additions & 12 deletions src/clim/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ class Clim
end

macro alias_name(*names)
{% if @type == Command_Main_command_of_clim_library %}
{% raise "'alias_name' is not supported on main command." %}
{% end %}
{% raise "'alias_name' is not supported on main command." if @type == Command_Main_command_of_clim_library %}
def alias_name : Array(String)
{{ names }}.to_a
end
Expand Down Expand Up @@ -64,9 +62,7 @@ class Clim
end

macro main_command
{% if @type.superclass.id.stringify == "Clim::Command" %}
{% raise "Can not be declared 'main_command' as sub command." %}
{% end %}
{% raise "Can not be declared 'main_command' as sub command." if @type.superclass.id.stringify == "Clim::Command" %}
end

macro sub_command(name, &block)
Expand All @@ -89,7 +85,7 @@ class Clim

abstract def run(io : IO)

def find_sub_cmds_by(name)
private def find_sub_cmds_by(name)
@sub_commands.select do |cmd|
cmd.name == name || cmd.alias_name.includes?(name)
end
Expand All @@ -116,11 +112,11 @@ class Clim
find_sub_cmds_by(argv.first).first.recursive_parse(argv[1..-1])
end

def help
private def help
Help.new(self).display
end

def display_help? : Bool
private def display_help? : Bool
@display_help_flag
end

Expand All @@ -132,7 +128,7 @@ class Clim
{% option_name = base_option_name.id.stringify.gsub(/\=/, " ").split(" ").first.id.stringify.gsub(/^-+/, "").gsub(/-/, "_").id %}
class OptionsForEachCommand
class Option_{{option_name}} < Option
option_by_clim_macro({{type}}, {{default}})
define_option_macro({{type}}, {{default}})
end

{% default = false if type.id.stringify == "Bool" %}
Expand Down Expand Up @@ -165,7 +161,7 @@ class Clim

alias OptionsForEachCommand = Options_{{ name.id.capitalize }}

def parse_by_parser(argv)
private def parse_by_parser(argv)
@parser.on("--help", "Show this help.") { @display_help_flag = true }
define_version(@parser)
@parser.invalid_option { |opt_name| raise ClimInvalidOptionException.new "Undefined option. \"#{opt_name}\"" }
Expand All @@ -188,7 +184,7 @@ class Clim
\{% end %}
end

def required_validate!
private def required_validate!
raise "Required options. \"#{@options.invalid_required_names.join("\", \"")}\"" unless @options.invalid_required_names.empty?
end

Expand Down
2 changes: 1 addition & 1 deletion src/clim/command/options/option.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Clim
{% end %}
end

macro option_by_clim_macro(type, default)
macro define_option_macro(type, default)
{% value_type = default == nil ? type.stringify + "?" : type.stringify %}
property default : {{value_type.id}} = {{default}}
property value : {{value_type.id}} = {{default}}
Expand Down

0 comments on commit 31fc594

Please sign in to comment.