From 56a4f6edaa6ae7d8f3d3cec37dc48d7f30c2ffce Mon Sep 17 00:00:00 2001 From: Sally O'Malley Date: Sun, 1 Sep 2019 10:22:13 -0400 Subject: [PATCH] kubectl: remove usage info from bad flag error msg Kubernetes-commit: e36dc3adb4045373eefc002e4afe5298ec715ce9 --- pkg/util/templates/templater.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/util/templates/templater.go b/pkg/util/templates/templater.go index a7036f0ec6dd1..18901348acb4b 100644 --- a/pkg/util/templates/templater.go +++ b/pkg/util/templates/templater.go @@ -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