Print usage only for a command called wrongly (closes #41)
check / check (push) Failing after 2s
check / check (push) Failing after 2s
A failed command printed the whole usage text after its error, burying it. The root command's PersistentPreRunE now turns usage off once cobra has checked the arguments and flags, so an error from running the command is printed once on its own. Wrong arity, an unknown flag, a bad flag value and a missing required flag still get usage; cobra checks required flags after that hook, so the hook checks them first. Root SilenceUsage was not used: in this cobra version it hides usage for argument and flag errors too. Cobra still prints the error; Entry is unchanged. Model: opus-5-5
This commit is contained in:
+17
-2
@@ -46,9 +46,24 @@ func newRootCmd() *cobra.Command {
|
||||
Short: "A simple secrets manager",
|
||||
Long: `A simple secrets manager to store and retrieve sensitive ` +
|
||||
`information securely.`,
|
||||
// Ensure usage is shown after errors
|
||||
SilenceUsage: false,
|
||||
// Cobra prints the error a command returns; Entry does not.
|
||||
SilenceErrors: false,
|
||||
// Usage belongs only to a command called wrongly. Cobra has
|
||||
// checked its arguments and flags before this runs, except for
|
||||
// required flags, which are checked here so they still get usage.
|
||||
// An error after that comes from running the command, and usage
|
||||
// would only bury it. A subcommand that sets its own
|
||||
// PersistentPreRun replaces this one.
|
||||
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
|
||||
err := cmd.ValidateRequiredFlags()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.SilenceUsage = true
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
secret.Debug("Adding subcommands to root command")
|
||||
|
||||
Reference in New Issue
Block a user