Module: Brut::CLI::ExecutionResults
Overview
Included in commands to allow convienient ways to return structured information about what happened during command execution.
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
-
#abort_execution(message, exit_status: 1) ⇒ Object
Return this from Command#execute to stop execution and signal an error.
-
#as_execution_result(exit_status_or_execution_result) ⇒ Brut::CLI::ExecutionResults::Result
Coerce a value to the appropriate Result.
-
#cli_usage_error(message) ⇒ Object
Return this from Command#execute to stop execution because the user messed up the CLI invocation.
-
#continue_execution ⇒ Object
Return this from Command#execute to indicate any other execution may continue.
-
#show_cli_usage(command_klass = nil) ⇒ Object
Return this from Command#execute to stop execution, and show the user the CLI help, optionally for a specific command.
-
#stop_execution ⇒ Object
Return this from Command#execute to stop execution, without signaling an error.
Instance Method Details
#abort_execution(message, exit_status: 1) ⇒ Object
Return this from Command#execute to stop execution and signal an error
83 84 85 86 |
# File 'lib/brut/cli/execution_results.rb', line 83 def abort_execution(,exit_status:1) = Abort.new(message:,exit_status:) # Return this from {Brut::CLI::Command#execute} to stop execution because the user messed up the CLI invocation. # # @param [String] message the error message |
#as_execution_result(exit_status_or_execution_result) ⇒ Brut::CLI::ExecutionResults::Result
Coerce a value to the appropriate Result. Currently works as follows:
- If
exit_status_or_execution_result
is nil or true, returns a successful result - If
exit_status_or_execution_result
is an integer, returns a result with no message and that value as the exit status - If
exit_status_or_execution_result
is false, returns #abort_execution. - If
exit_status_or_execution_result
is a Result, returns that - Otherwise, raises
ArgumentError
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/brut/cli/execution_results.rb', line 104 def as_execution_result(exit_status_or_execution_result) if exit_status_or_execution_result.kind_of?(Numeric) || exit_status_or_execution_result.nil? Result.new(exit_status: exit_status_or_execution_result.to_i) elsif exit_status_or_execution_result == true Result.new(exit_status: 0) elsif exit_status_or_execution_result == false Abort.new elsif exit_status_or_execution_result.kind_of?(Result) exit_status_or_execution_result else raise ArgumentError,"Your method returned a #{exit_status_or_execution_result.class} when it should return an exit status or one of the methods from ExecutionResults" end end |
#cli_usage_error(message) ⇒ Object
Return this from Command#execute to stop execution because the user messed up the CLI invocation.
87 88 89 90 91 |
# File 'lib/brut/cli/execution_results.rb', line 87 def cli_usage_error() = CLIUsageError.new(message:) # Return this from {Brut::CLI::Command#execute} to stop execution, and show the user the CLI help, optionally for a specific # command. # # @param [Class] command_klass if given, this it he class whose help will be shown. |
#continue_execution ⇒ Object
Return this from Command#execute to indicate any other execution may continue. This is mostly useful when one command calls another e.g. with Command#delegate_to_commands.
78 79 80 81 82 |
# File 'lib/brut/cli/execution_results.rb', line 78 def continue_execution = Continue.new # Return this from {Brut::CLI::Command#execute} to stop execution and signal an error # # @param [String] message the error message # @param [Integer] exit_status the exit status (should ideally not be 0) |
#show_cli_usage(command_klass = nil) ⇒ Object
Return this from Command#execute to stop execution, and show the user the CLI help, optionally for a specific command.
92 |
# File 'lib/brut/cli/execution_results.rb', line 92 def show_cli_usage(command_klass=nil) = ShowCLIUsage.new(command_klass:) |
#stop_execution ⇒ Object
Return this from Command#execute to stop execution, without signaling an error
75 76 77 |
# File 'lib/brut/cli/execution_results.rb', line 75 def stop_execution = Stop.new # Return this from {Brut::CLI::Command#execute} to indicate any other execution may continue. This is mostly useful when one # command calls another e.g. with {Brut::CLI::Command#delegate_to_commands}. |