Skip to content

Commit

Permalink
🎨 Traditional begin / rescue instead of concise
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarlesky committed Aug 28, 2024
1 parent 66fbfa1 commit 9c60120
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions bin/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,31 @@ def self.extended(base)

# Redefine the Thor CLI entrypoint and exception handling
def start(args, config={})
# Copy args as Thor changes them within the call chain of dispatch()
_args = args.clone()

# Call Thor's handlers as it does in start()
config[:shell] ||= Thor::Base.shell.new
dispatch(nil, args, nil, config)

# Handle undefined commands at top-level and for `help <command>`
rescue Thor::UndefinedCommandError => ex
# Handle `help` for an argument that is not an application command such as `new` or `build`
if _args[0].downcase() == 'help'

# Raise fatal StandardError to differentiate from UndefinedCommandError
msg = "Argument '#{_args[1]}' is not a recognized application command with detailed help. " +
"It may be a build / plugin task without detailed help or simply a goof."
raise( msg )

# Otherwise, eat unhandled command errors
else
# - No error message
# - No `exit()`
# - Re-raise to allow special, external CLI handling logic
raise ex
begin
# Copy args as Thor changes them within the call chain of dispatch()
_args = args.clone()

# Call Thor's handlers as it does in start()
config[:shell] ||= Thor::Base.shell.new
dispatch(nil, args, nil, config)

# Handle undefined commands at top-level and for `help <command>`
rescue Thor::UndefinedCommandError => ex
# Handle `help` for an argument that is not an application command such as `new` or `build`
if _args[0].downcase() == 'help'

# Raise fatal StandardError to differentiate from UndefinedCommandError
msg = "Argument '#{_args[1]}' is not a recognized application command with detailed help. " +
"It may be a build / plugin task without detailed help or simply a goof."
raise( msg )

# Otherwise, eat unhandled command errors
else
# - No error message
# - No `exit()`
# - Re-raise to allow special, external CLI handling logic
raise ex
end
end
end
end
Expand Down

0 comments on commit 9c60120

Please sign in to comment.