kubectl: remove usage info from bad flag error msg

This commit is contained in:
Sally O'Malley 2019-09-01 10:22:13 -04:00
parent 246df354a5
commit e36dc3adb4
No known key found for this signature in database
GPG Key ID: 9AD4DAB10C905B8C

View File

@ -44,6 +44,7 @@ func ActsAsRootCommand(cmd *cobra.Command, filters []string, groups ...CommandGr
CommandGroups: groups,
Filtered: filters,
}
cmd.SetFlagErrorFunc(templater.FlagErrorFunc())
cmd.SetUsageFunc(templater.UsageFunc())
cmd.SetHelpFunc(templater.HelpFunc())
return templater
@ -66,6 +67,18 @@ type templater struct {
Filtered []string
}
func (templater *templater) FlagErrorFunc(exposedFlags ...string) func(*cobra.Command, error) error {
return func(c *cobra.Command, err error) error {
c.SilenceUsage = true
switch c.CalledAs() {
case "options":
return fmt.Errorf("%s\nRun '%s' without flags.", err, c.CommandPath())
default:
return fmt.Errorf("%s\nSee '%s --help' for usage.", err, c.CommandPath())
}
}
}
func (templater *templater) ExposeFlags(cmd *cobra.Command, flags ...string) FlagExposer {
cmd.SetUsageFunc(templater.UsageFunc(flags...))
return templater