Module: Fontist::ThorExt::Start
- Included in:
- CLI
- Defined in:
- lib/fontist/cli/thor_ext.rb
Overview
Sources:
- https://github.com/mattbrictson/gem/blob/main/lib/example/thor_ext.rb
- https://mattbrictson.com/blog/fixing-thor-cli-behavior
Configures Thor to behave more like a typical CLI, with better help and error handling.
- Passing -h or --help to a command will show help for that command.
- Unrecognized options will be treated as errors.
- Error messages will be printed in red to stderr, without stack trace.
- Errors will cause Thor to exit with a non-zero status.
To take advantage of this behavior, your CLI should subclass Thor and extend this module.
class CLI < Thor
extend ThorExt::Start
end
Start your CLI with:
CLI.start
In tests, prevent Kernel.exit from being called when an error occurs, like this:
CLI.start(args, exit_on_failure: false)
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.extended(base) ⇒ Object
31 32 33 34 |
# File 'lib/fontist/cli/thor_ext.rb', line 31 def self.extended(base) super base. end |
Instance Method Details
#start(given_args = ARGV, config = {}) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/fontist/cli/thor_ext.rb', line 36 def start(given_args = ARGV, config = {}) config[:shell] ||= Thor::Base.shell.new handle_version_and_help_switches(given_args) do |args| dispatch(nil, args, nil, config) end rescue StandardError => e handle_exception_on_start(e, config) end |