Module: Brut::CLI::ExecutionResults

Included in:
App, AppRunner, Command
Defined in:
lib/brut/cli/execution_results.rb

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

Instance Method Details

#abort_execution(message, exit_status: 1) ⇒ Object

Return this from Command#execute to stop execution and signal an error

Parameters:

  • message (String)

    the error message

  • exit_status (Integer) (defaults to: 1)

    the exit status (should ideally not be 0)



83
84
85
86
# File 'lib/brut/cli/execution_results.rb', line 83

def abort_execution(message,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:

  1. If exit_status_or_execution_result is nil or true, returns a successful result
  2. If exit_status_or_execution_result is an integer, returns a result with no message and that value as the exit status
  3. If exit_status_or_execution_result is false, returns #abort_execution.
  4. If exit_status_or_execution_result is a Result, returns that
  5. Otherwise, raises ArgumentError

Parameters:

Returns:



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.

Parameters:

  • message (String)

    the error message



87
88
89
90
91
# File 'lib/brut/cli/execution_results.rb', line 87

def cli_usage_error(message)               = 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_executionObject

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.

Parameters:

  • command_klass (Class) (defaults to: nil)

    if given, this it he class whose help will be shown.



92
# File 'lib/brut/cli/execution_results.rb', line 92

def show_cli_usage(command_klass=nil)      = ShowCLIUsage.new(command_klass:)

#stop_executionObject

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}.