module TTY::Exit

Terminal exit codes for humans and machines

Constants

CODE_TO_EXIT_MESSAGE
Error
NAME_TO_EXIT_CODE
VERSION

Public Class Methods

included(base) click to toggle source

@api private

# File lib/tty/exit.rb, line 15
def self.included(base)
  base.instance_eval do
    def register_exit(*args)
      Registry.register_exit(*args)
    end
  end
end

Public Instance Methods

exit_code(name_or_code = :ok) click to toggle source

Provide exit code for a name or status

@example

TTY::Exit.exit_code(:usage_error)
# => 64

@param [String,Integer] name_or_code

@return [Integer]

the exit code

@api public

# File lib/tty/exit.rb, line 180
def exit_code(name_or_code = :ok)
  case name_or_code
  when String, Symbol
    (Registry.exits[name_or_code.to_sym] || {})[:code] ||
      NAME_TO_EXIT_CODE.fetch(name_or_code.to_sym) do
        raise Error, "Name '#{name_or_code}' isn't recognized."
      end
  when Numeric
    if exit_valid?(name_or_code.to_i)
      name_or_code.to_i
    else
      raise Error, "Provided code outside of the range (0 - 255)"
    end
  else
    raise Error, "Provide a name or a number as an exit code"
  end
end
exit_codes() click to toggle source

Provide a list of reserved codes

@api public

# File lib/tty/exit.rb, line 201
def exit_codes
  NAME_TO_EXIT_CODE
end
exit_message(name_or_code = :ok) click to toggle source

A user friendly explanation of the exit code

@example

TTY::Exit.exit_message(:usage_error)
# => "Command line usage error"

@param [String,Integer] name_or_code

@api public

# File lib/tty/exit.rb, line 156
def exit_message(name_or_code = :ok)
  (Registry.exits[name_or_code] || {})[:message] ||
    CODE_TO_EXIT_MESSAGE[exit_code(name_or_code)] || ""
end
exit_messages() click to toggle source

Provide a list of reserved status messages

@api public

# File lib/tty/exit.rb, line 164
def exit_messages
  CODE_TO_EXIT_MESSAGE
end
exit_reserved?(code) click to toggle source

Check if an exit code is already defined by Unix system

@param [Integer] code

the code to check

@return [Boolean]

@api public

# File lib/tty/exit.rb, line 132
def exit_reserved?(code)
  (code >= Code::SUCCESS && code <= Code::SHELL_MISUSE) ||
    (code >= Code::USAGE_ERROR && code <= Code::CONFIG_ERROR) ||
    (code >= Code::CANNOT_EXECUTE && code <= Code::USER2)
end
exit_success?(code) click to toggle source

Check if the exit status was successful.

@param [Integer] code

@api public

# File lib/tty/exit.rb, line 143
def exit_success?(code)
  code == Code::SUCCESS
end
exit_valid?(code) click to toggle source

Check if an exit code is valid, that it's within the 0-255 (inclusive)

@param [Integer] code

the code to check

@return [Boolean]

@api public

# File lib/tty/exit.rb, line 120
def exit_valid?(code)
  code >= 0 && code <= 255
end
exit_with(name_or_code = :ok, message = nil, io: $stderr) click to toggle source

Exit this process with a given status code

@param [String,Integer] name_or_code

The name for an exit code or code itself

@param [String] message

The message to print to io stream

@param [IO] io

The io to print message to

@return [nil]

@api public

# File lib/tty/exit.rb, line 217
def exit_with(name_or_code = :ok, message = nil, io: $stderr)
  if message == :default
    message = "ERROR(#{exit_code(name_or_code)}): #{exit_message(name_or_code)}"
  end
  io.print(message) if message
  ::Kernel.exit(exit_code(name_or_code))
end
register_exit(*args) click to toggle source
# File lib/tty/exit.rb, line 17
def register_exit(*args)
  Registry.register_exit(*args)
end