-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Silence some usage and error logging in Execute #168
Conversation
`RunE` was introduced on `Command` to support centralized error handling. The errors returned from `RunE` is, however, still logged by Cobra, and there is no easy way to toogle the usage logging on a case-by-case basis (other than the brutal `os.Exit(-1)`. This commit introduces two small interfaces that enables end users to signal the behavior they want from Cobra in this area.
@eparis I have no idea what got that circleci test to fail. About the PR: This is my suggestion. I have started the job of getting rid of all the I know I could add some configuration, and that would maybe work for the error logging case, but the need for usage logging is on a "case-by-case" basis ... So this is my best current solution. Open to alternative suggestions. |
Personally, I would rather see a field in the command struct vs creating interfaces. I think it should be on each command and subcommands should inherit from the root cmd. For consistency. |
I like the |
I can add SilenceUsage today and reimplement the PR. I didn't want to step on this PR, that's why I closed it. Unless @bep wants to do it. |
My use use cases are as follow:
The |
@bep Can you give an example please? I am trying to understand because the way that I coded it, it should be controlled from each individual command? |
The thing is, I don't know when I create the command what happens in the command's A made up example: A root command with some stock quote operations: Command
|
If thats the case, could you show the usage before you return the error using |
Yea, that is the case today. Hugo is sprinkled with |
Okay cool. We are on the same page now. 👍 So I don't know how I feel about that to be honest. I have mixed feelings because I like the explicit calling of Usage for readability reasons but I can see the case on both sides. |
Yea, programming is a lot about taste :-) Cobra laid the foundation for centralized error handling with the |
It would be good to get a running list of what needs to be complete that. :) I like lists 👍 |
I created this pull request as a suggestion. It looks funky, but I have no better suggestion. The thing is, if a command implements |
OK; to solve my problem in another way: Is there a way to get the
|
@bep what if you propagated as part of the error or create custom errors? |
@apriendeau My main point in all of this is that I want my error handling in one place. Having some extra error wrapping logic in all of my commands does not help with that dream. |
I completely agree. I meant that as a solution to get that. I would |
I understand what you mean, but as I said, it wouldn't help. |
Okay. So thats my best suggestion because thats what I did and it works for me. |
@bep I think we are good to close this PR right? |
RunE
was introduced onCommand
to support centralized error handling.The errors returned from
RunE
is, however, still logged by Cobra, and there is no easy way to toggle the usage logging on a case-by-case basis (other than the brutalos.Exit(-1)
.This commit introduces two small interfaces that enables end users to signal the behavior they want from Cobra in this area.