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
Constant Summary collapse
- Error =
Class.new(StandardError)
- VERSION =
"0.1.0"
Class Method Summary collapse
- .included(base) ⇒ Object private
Instance Method Summary collapse
-
#exit_code(name_or_code = :ok) ⇒ Integer
Provide exit code for a name or status.
-
#exit_codes ⇒ Object
Provide a list of reserved codes.
-
#exit_message(name_or_code = :ok) ⇒ Object
A user friendly explanation of the exit code.
-
#exit_messages ⇒ Object
Provide a list of reserved status messages.
-
#exit_reserved?(code) ⇒ Boolean
Check if an exit code is already defined by Unix system.
-
#exit_success?(code) ⇒ Boolean
Check if the exit status was successful.
-
#exit_valid?(code) ⇒ Boolean
Check if an exit code is valid, that it’s within the 0-255 (inclusive).
-
#exit_with(name_or_code = :ok, message = nil, io: $stderr) ⇒ nil
Exit this process with a given status code.
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
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_codes ⇒ Object
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
156 157 158 159 |
# File 'lib/tty/exit.rb', line 156 def (name_or_code = :ok) (Registry.exits[name_or_code] || {})[:message] || CODE_TO_EXIT_MESSAGE[exit_code(name_or_code)] || "" end |
#exit_messages ⇒ Object
Provide a list of reserved status messages
164 165 166 |
# File 'lib/tty/exit.rb', line 164 def CODE_TO_EXIT_MESSAGE end |
#exit_reserved?(code) ⇒ Boolean
Check if an exit code is already defined by Unix system
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.
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)
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
217 218 219 220 221 222 223 |
# File 'lib/tty/exit.rb', line 217 def exit_with(name_or_code = :ok, = nil, io: $stderr) if == :default = "ERROR(#{exit_code(name_or_code)}): #{(name_or_code)}" end io.print() if ::Kernel.exit(exit_code(name_or_code)) end |