Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
at-grandpa committed Aug 1, 2017
1 parent 59a952f commit bc36c3d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
15 changes: 9 additions & 6 deletions src/clim/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand All @@ -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 }
Expand All @@ -42,6 +43,10 @@ class Clim
end
end

def display_help?
@display_help_flag
end

def base_help
<<-HELP_MESSAGE
Expand Down Expand Up @@ -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
Expand All @@ -126,6 +128,7 @@ class Clim
def prepare_parse
opts.reset
@args = [] of String
@display_help_flag = false
end

end
Expand Down
6 changes: 0 additions & 6 deletions src/clim/input_args.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bc36c3d

Please sign in to comment.