From bc36c3dc8b28c452e09bd91e829e04facc7d458b Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Tue, 1 Aug 2017 13:13:16 +0900 Subject: [PATCH] refactoring --- src/clim/command.cr | 15 +++++++++------ src/clim/input_args.cr | 6 ------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/clim/command.cr b/src/clim/command.cr index e1cd922d..15d61e23 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -11,6 +11,7 @@ class Clim property parser : OptionParser = OptionParser.new property sub_cmds : Array(self) = [] of self property help_proc : RunProc = RunProc.new { } + property display_help_flag : Bool = false def initialize(@name) @usage = "#{name} [options] [arguments]" @@ -19,7 +20,7 @@ class Clim end def initialize_parser - parser.on("--help", "Show this help.") { } + parser.on("--help", "Show this help.") { @run_proc = help_proc; @display_help_flag = true } parser.invalid_option { |opt_name| raise ClimException.new "Undefined option. \"#{opt_name}\"" } parser.missing_option { |opt_name| raise ClimException.new "Option that requires an argument. \"#{opt_name}\"" } parser.unknown_args { |unknown_args| @args = unknown_args } @@ -42,6 +43,10 @@ class Clim end end + def display_help? + @display_help_flag + end + def base_help <<-HELP_MESSAGE @@ -112,12 +117,9 @@ class Clim prepare_parse parser.parse(input_args.to_be_exec.dup) + # parser.parse(argv.dup) - if input_args.include_help_arg? - @run_proc = help_proc - else - opts.validate! - end + opts.validate! unless display_help? opts.help = help self @@ -126,6 +128,7 @@ class Clim def prepare_parse opts.reset @args = [] of String + @display_help_flag = false end end diff --git a/src/clim/input_args.cr b/src/clim/input_args.cr index 735ebd4d..596b6a1d 100644 --- a/src/clim/input_args.cr +++ b/src/clim/input_args.cr @@ -12,11 +12,5 @@ class Clim def select_help_arg argv.select { |arg| arg == "--help" } end - - def include_help_arg? - return false if to_be_exec.empty? - other_arg = to_be_exec.reject { |arg| arg == "--help" } - other_arg.empty? - end end end