-
Notifications
You must be signed in to change notification settings - Fork 8
Using Exiting Methods
To invoke an explicit exit, use one of the exiting methods:
- exit!
- error!
- help!
- version!
The exit! method is a general way to invoke an explicit exit.
class Mytrue < Cli::Command
def run
exit!
end
end
Mytrue.runOutput:
$ mytrue
$ echo $?
0exit! internally calls the standard exit method.
class FinallySmile < Cli::Command
def run
puts ":("
exit!
end
end
at_exit { puts ":)" }
FinallySmile.runOutput:
$ finally-smile
:(
:)[WIP]
class Command < Cli::Command
def run
help!
end
end
Command.run # => 0This command just ends after printing its help message. Command.run returns 0.
To print a message to STDERR and exit with an error code, use :error option.
help! error: trueIf the :error option is true, run method returns 1. To specify a number, use the :code option.
help! code: 22You can also let a command exit with an additional message:
help! message: "You passed an illegal option! See help!"Or simply:
help! "You passed an illegal option! See help!"Calling help! with the :message argument implies that the :error option is true. To exit normally, set false to :error.
exit! is more general purpose than help!.
class Command < Cli::Command
def run
exit!
end
end
Command.run # => 0It just ends and returns 0 without a message.
To print a message:
exit! "bye."Or more variations:
exit! help: true # equivalent to help!
exit! error: true # returns 1 as an exit code
exit! "message", error: true, help: true # equivalent to help!("message")error! is similar to exit!, but the :error option is true as default.
error! # ends with 1 as an exit code
error! "message" # equivalent to exit!("message", error: true)
error! code: 22 # specifies exit code
error! help: true # equivalent to help!(error: true)
error! "message", help: true # equivalent to help!("message")