Skip to content

Commit

Permalink
Merge pull request kubernetes#82423 from sallyom/kubectl-flagerror
Browse files Browse the repository at this point in the history
kubectl: remove usage info from bad flag msg, only print help tip
  • Loading branch information
k8s-ci-robot authored and ohsewon committed Oct 16, 2019
2 parents 41c2a0a + e36dc3a commit 610dab6
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions staging/src/k8s.io/kubectl/pkg/util/templates/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 610dab6

Please sign in to comment.