Module: Sysexits
- Defined in:
- lib/sysexits.rb
Overview
sysexits
Exit status codes for system programs.
Usage
To support running on a Mac running OS X 10.7 or later, you’ll need to force the gem first in the load path to avoid Apple’s own helpfully vendored ‘sysexits’ library:
gem 'sysexits'
require 'sysexits'
You can look up the appropriate code yourself, and exit using the regular exit method, of course:
status_code = Sysexits.exit_status( :success )
exit( status_code )
Or, just use Sysexits::exit with the code name:
Sysexits.exit( :usage )
Or, mix the enhanced ::exit into your namespace:
include Sysexits
exit( :unavailable )
It also supports #exit! in all the same ways as above.
License
This file was derived almost entirely from the BSD sysexits.h, which is distributed under the following license:
Copyright © 1987, 1993 The Regents of the University of California. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-
All advertising materials mentioning features or use of this software must display the following acknowledgement:
This product includes software developed by the University of California, Berkeley and its contributors.
-
Neither the name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Constant Summary collapse
- VERSION =
The library version
'1.2.0'
- REVISION =
The library’s revision id
%q$Revision: 8d7b699637cf $
- EX_OK =
:ok, :success - Successful termination
0
- EX__BASE =
:_base - The base value for sysexit codes
64
- EX_USAGE =
:usage - The command was used incorrectly, e.g., with the wrong number of arguments, a bad flag, a bad syntax in a parameter, or whatever.
64
- EX_DATAERR =
:dataerr, :data_error - The input data was incorrect in some way. This should only be used for user data, not system files.
65
- EX_NOINPUT =
:noinput, :input_missing - An input file (not a system file) did not exist or was not readable. This could also include errors like “No message” to a mailer (if it cared to catch it).
66
- EX_NOUSER =
:nouser, :no_such_user - The user specified did not exist. This might be used for mail addresses or remote logins.
67
- EX_NOHOST =
:nohost, :no_such_host - The host specified did not exist. This is used in mail addresses or network requests.
68
- EX_UNAVAILABLE =
:unavailable, :service_unavailable - A service is unavailable. This can occur if a support program or file does not exist. This can also be used as a catchall message when something you wanted to do doesn’t work, but you don’t know why.
69
- EX_SOFTWARE =
:software, :software_error - An internal software error has been detected. This should be limited to non-operating system related errors.
70
- EX_OSERR =
:oserr, :operating_system_error - An operating system error has been detected. This is intended to be used for such things as “cannot fork”, “cannot create pipe”, or the like. It includes things like getuid returning a user that does not exist in the passwd file.
71
- EX_OSFILE =
:osfile, :operating_system_file_error - Some system file (e.g., /etc/passwd, /etc/utmp, etc.) does not exist, cannot be opened, or has some sort of error (e.g., syntax error).
72
- EX_CANTCREAT =
:cantcreat, :cant_create_output - A (user specified) output file cannot be created.
73
- EX_IOERR =
:ioerr - An error occurred while doing I/O on a file.
74
- EX_TEMPFAIL =
:tempfail, :temporary_failure, :try_again - Temporary failure, indicating something that is not really a serious error. In sendmail, this means that a mailer (e.g.) could not create a connection, and the request should be reattempted later.
75
- EX_PROTOCOL =
:protocol, :protocol_error - The remote system returned something that was “not possible” during a protocol exchange.
76
- EX_NOPERM =
:noperm, :permission_denied - You did not have sufficient permission to perform the operation. This is not intended for file system problems, which should use NOINPUT or CANTCREAT, but rather for higher level permissions.
77
- EX_CONFIG =
:config, :config_error - There was an error in a user-specified configuration value.
78
- EX__MAX =
:_max - The maximum listed value. Automatically determined because, well, we can and I’ll forget to update this if I ever add any codes.
constants. select {|name| name =~ /^EX_/ }. collect {|name| self.const_get(name) }.max
- STATUS_CODES =
Mapping of human-readable Symbols to statuses
{ :ok => EX_OK, :success => EX_OK, :_base => EX__BASE, :usage => EX_USAGE, :dataerr => EX_DATAERR, :data_error => EX_DATAERR, :noinput => EX_NOINPUT, :input_missing => EX_NOINPUT, :nouser => EX_NOUSER, :no_such_user => EX_NOUSER, :nohost => EX_NOHOST, :no_such_host => EX_NOHOST, :unavailable => EX_UNAVAILABLE, :service_unavailable => EX_UNAVAILABLE, :software => EX_SOFTWARE, :software_error => EX_SOFTWARE, :oserr => EX_OSERR, :operating_system_error => EX_OSERR, :osfile => EX_OSFILE, :operating_system_file_error => EX_OSFILE, :cantcreat => EX_CANTCREAT, :cant_create_output => EX_CANTCREAT, :ioerr => EX_IOERR, :tempfail => EX_TEMPFAIL, :temporary_failure => EX_TEMPFAIL, :try_again => EX_TEMPFAIL, :protocol => EX_PROTOCOL, :protocol_error => EX_PROTOCOL, :noperm => EX_NOPERM, :permission_denied => EX_NOPERM, :config => EX_CONFIG, :config_error => EX_CONFIG, :_max => EX__MAX, }
Class Method Summary collapse
-
.exit(status = EX_OK) ⇒ Object
Exit with the exit
status
, which can be either one of the keys of STATUS_CODES or a number. -
.exit!(status = EX_OK) ⇒ Object
Exit with the given
status
without running exit handlers. -
.exit_status(status) ⇒ Object
Turn
status
into a numeric exit status and return it.
Class Method Details
.exit(status = EX_OK) ⇒ Object
Exit with the exit status
, which can be either one of the keys of STATUS_CODES or a number.
242 243 244 245 |
# File 'lib/sysexits.rb', line 242 def exit( status=EX_OK ) status = exit_status( status ) ::Kernel.exit( status ) end |
.exit!(status = EX_OK) ⇒ Object
Exit with the given status
without running exit handlers.
249 250 251 252 |
# File 'lib/sysexits.rb', line 249 def exit!( status=EX_OK ) status = exit_status( status ) ::Kernel.exit!( status ) end |
.exit_status(status) ⇒ Object
Turn status
into a numeric exit status and return it.
230 231 232 233 234 235 236 237 |
# File 'lib/sysexits.rb', line 230 def exit_status( status ) case status when Symbol, String return STATUS_CODES[ status.to_sym ] || status else return status end end |