commander-rb / commander

The complete solution for Ruby command-line executables
MIT License
822 stars 74 forks source link

Allow --trace option without the error message prompt #77

Closed WilliamMcCumstie closed 5 years ago

WilliamMcCumstie commented 5 years ago

Description:

Allow --trace to toggle the traceback without modifying the error messages. The option still appears in the global help text, as expected.

Motivation:

A lot of my application rely on valid input data (e.g. a file path). This results in user errors which get appended with: Use --trace to view backtrace

By disabling the message, the user can focus on the rest of the message.

ggilder commented 5 years ago

Thanks for the pull request, but I think there is actually a way to achieve the behavior you want without modifying commander. In your command you can simply use "abort" rather than "raise", and the error message you provide will be printed without the message about tracing.

If you have specific exceptions generated in another module that you want to treat this way, I would suggest catching them in a rescue block in your command, e.g. your command's action block would look something like:

c.action do |args, options|
  # run your module code
  # module does something like:
  # raise OopsieException, "oh no!"
rescue OopsieException => e
  abort e.message
end

Let me know if that addresses your use case.

ggilder commented 5 years ago

I'll close the PR for now but feel free to reopen if my suggestion misses something.