Module: TTY::Exit

Extended by:
Exit
Included in:
Exit
Defined in:
lib/tty/exit.rb,
lib/tty/exit/code.rb,
lib/tty/exit/version.rb,
lib/tty/exit/registry.rb

Overview

Terminal exit codes for humans and machines

Defined Under Namespace

Modules: Code, Registry

Constant Summary collapse

Error =
Class.new(StandardError)
VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
18
19
20
21
# 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

Instance Method Details

#exit_code(name_or_code = :ok) ⇒ Integer

Provide exit code for a name or status

Examples:

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

Parameters:

  • name_or_code (String, Integer) (defaults to: :ok)

Returns:

  • (Integer)

    the exit code



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# 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_codesObject

Provide a list of reserved codes



201
202
203
# File 'lib/tty/exit.rb', line 201

def exit_codes
  NAME_TO_EXIT_CODE
end

#exit_message(name_or_code = :ok) ⇒ Object

A user friendly explanation of the exit code

Examples:

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

Parameters:

  • name_or_code (String, Integer) (defaults to: :ok)


156
157
158
159
# 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_messagesObject

Provide a list of reserved status messages



164
165
166
# File 'lib/tty/exit.rb', line 164

def exit_messages
  CODE_TO_EXIT_MESSAGE
end

#exit_reserved?(code) ⇒ Boolean

Check if an exit code is already defined by Unix system

Parameters:

  • code (Integer)

    the code to check

Returns:

  • (Boolean)


132
133
134
135
136
# 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) ⇒ Boolean

Check if the exit status was successful.

Parameters:

  • code (Integer)

Returns:

  • (Boolean)


143
144
145
# File 'lib/tty/exit.rb', line 143

def exit_success?(code)
  code == Code::SUCCESS
end

#exit_valid?(code) ⇒ Boolean

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

Parameters:

  • code (Integer)

    the code to check

Returns:

  • (Boolean)


120
121
122
# 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) ⇒ nil

Exit this process with a given status code

Parameters:

  • name_or_code (String, Integer) (defaults to: :ok)

    The name for an exit code or code itself

  • message (String) (defaults to: nil)

    The message to print to io stream

  • io (IO) (defaults to: $stderr)

    The io to print message to

Returns:

  • (nil)


217
218
219
220
221
222
223
# 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