Skip to content

Commit

Permalink
refactor(command, executor): deprecate pre_run Bool? overload
Browse files Browse the repository at this point in the history
  • Loading branch information
devnote-dev committed Jan 25, 2024
1 parent 978c401 commit ab076ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 0 additions & 3 deletions src/cling/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ module Cling
# This has access to the parsed arguments and options from the command line. This is useful if
# you want to implement checks for specific flags outside of the main `run` method, such as
# `-v`/`--version` flags or `-h`/`--help` flags.
#
# Accepts a `Bool` or `nil` argument as a return to specify whether the command should continue
# to run once finished (`true` or `nil` to continue, `false` to stop).
def pre_run(arguments : Arguments, options : Options) : Bool?
end

Expand Down
13 changes: 12 additions & 1 deletion src/cling/executor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ module Cling::Executor
end

begin
resolved_command.pre_run executed.parsed_arguments, executed.parsed_options
deprecation_helper(
resolved_command,
resolved_command.pre_run(executed.parsed_arguments, executed.parsed_options)
)
rescue ex : ExitProgram
return handle_exit ex
rescue ex
Expand All @@ -69,6 +72,14 @@ module Cling::Executor
end
end

private def self.deprecation_helper(type : T, value : Bool?) : Nil forall T
{% T.warning "#{T}#pre_run : Bool? is deprecated. Use `pre_run(arguments : Arguments, options : Options) : Nil` instead" %}
raise ExitProgram.new 1 if value == false
end

private def self.deprecation_helper(type : T, value : Nil) : Nil forall T
end

private def self.resolve_command(command : Command, results : Array(Parser::Result)) : Command?
arguments = results.select { |r| r.kind.argument? && !r.string? }
return command if arguments.empty? || command.children.empty?
Expand Down

0 comments on commit ab076ee

Please sign in to comment.