-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Thor as the basis for generated CLI (#148)
* Hide stack trace unless VERBOSE is set * Mention Thor in the README
- Loading branch information
1 parent
5333a6e
commit e63618c
Showing
7 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require "example" | ||
Example::CLI.new.call(ARGV) | ||
Example::CLI.start(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
module Example | ||
autoload :CLI, "example/cli" | ||
autoload :VERSION, "example/version" | ||
autoload :ThorExt, "example/thor_ext" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
require "thor" | ||
|
||
module Example | ||
class CLI | ||
def call(_argv) | ||
class CLI < Thor | ||
extend ThorExt::Start | ||
|
||
map %w[-v --version] => "version" | ||
|
||
desc "version", "Display example version", hide: true | ||
def version | ||
say "example/#{VERSION} #{RUBY_DESCRIPTION}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module Example | ||
module ThorExt | ||
# 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 (instead of being silently ignored). | ||
# - Error messages will be printed in red to stderr, without stack trace. | ||
# - Full stack traces can be enabled by setting the VERBOSE environment variable. | ||
# - 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) | ||
# | ||
module Start | ||
def self.extended(base) | ||
super | ||
base.check_unknown_options! | ||
end | ||
|
||
def start(given_args=ARGV, config={}) | ||
config[:shell] ||= Thor::Base.shell.new | ||
handle_help_switches(given_args) do |args| | ||
dispatch(nil, args, nil, config) | ||
end | ||
rescue StandardError => e | ||
handle_exception_on_start(e, config) | ||
end | ||
|
||
private | ||
|
||
def handle_help_switches(given_args) | ||
yield(given_args.dup) | ||
rescue Thor::UnknownArgumentError => e | ||
retry_with_args = [] | ||
|
||
if given_args.first == "help" | ||
retry_with_args = ["help"] if given_args.length > 1 | ||
elsif (e.unknown & %w[-h --help]).any? | ||
retry_with_args = ["help", (given_args - e.unknown).first] | ||
end | ||
raise unless retry_with_args.any? | ||
|
||
yield(retry_with_args) | ||
end | ||
|
||
def handle_exception_on_start(error, config) | ||
return if error.is_a?(Errno::EPIPE) | ||
raise if ENV["VERBOSE"] || !config.fetch(:exit_on_failure, true) | ||
|
||
message = error.message.to_s | ||
message.prepend("[#{error.class}] ") if message.empty? || !error.is_a?(Thor::Error) | ||
|
||
config[:shell]&.say_error(message, :red) | ||
exit(false) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters