module ProgramExitFromError

Idiomatic support for dealing with errors during CLI use

Rather than allowing an error to propagate to the top level of the interpreter, this module provides the exit_program! method, which prints a message to STDERR and exits with an exit code of 1 (unless otherwise specified with .exit_with_code).

For debugging support, this module also exposes the exit_code it would use to exit the program were exit_program! called.

Public Class Methods

included(klass) click to toggle source
# File lib/program_exit_from_error.rb, line 32
def self.included(klass)
  klass.extend(ClassMethods)
end

Public Instance Methods

exit_code() click to toggle source
# File lib/program_exit_from_error.rb, line 44
def exit_code
  self.class.instance_variable_get(:@exit_code) || 1
end
exit_program!() click to toggle source
# File lib/program_exit_from_error.rb, line 48
def exit_program!
  print_error_message
  Kernel.exit(exit_code)
end
print_error_message() click to toggle source