-
Notifications
You must be signed in to change notification settings - Fork 719
Early flags check before "PersistentPreRunE" #4149
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
base: master
Are you sure you want to change the base?
Conversation
Utilize Cobra's "MarkFlagsMutuallyExclusive()" on root command. This shifts validation to flag parsing and remove the manual check from "PersistentPreRunE", which can be applied to all subcommands before hooks fire. Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wish the Cobra message was a bit more readable by the enduser:
FATA[0000] if any flags in the group [tty yes] are set none of the others can be; [tty yes] were all set
Yes I agree, though I don't think we can change cobra message from our end. I can try to send patches to them for user-friendly message if you think this is worthy enough. |
I don't know if this is going to be worth it. I think the first step should be to catalog all conflicting options throughout all subcommands, and see how they are being handled (if at all). And then come up with a comprehensive strategy that can cover all situations. Just saving 3 lines of code for a singular instance doesn't seem to be worth it if it makes the error message less readable. Just off the top of my head:
There are probably more. So I think it would be more useful to define a common error message for this situation, and make sure we use the same words everywhere instead of creating ad-hoc error strings each time. I don't think Cobra is flexible enough to handle this, so we should have our own support functions, if that is possible. |
If we can extend the upstream cobra that would be better |
|
@jandubois - Thanks for so much detail, I'll look into them and try to come up with some strategy on how to handle them with a consistent way and also make the error message readable. @AkihiroSuda - No problem, I'll try to reach out to them at the same time, to see if we can extend the log/error message to be specified by ourselves. |
Summary
Utilize Cobra's "MarkFlagsMutuallyExclusive()" on root command. This shifts validation to flag parsing and remove the manual check from "PersistentPreRunE", which can be applied to all subcommands before hooks fire.
Test
Simple manual test:
Reference