make string "help" a constant

This commit is contained in:
aarzilli 2024-12-06 16:38:53 +01:00 committed by Marc Khouzam
parent bed6de4451
commit beec32994a

View file

@ -32,6 +32,8 @@ import (
const ( const (
FlagSetByCobraAnnotation = "cobra_annotation_flag_set_by_cobra" FlagSetByCobraAnnotation = "cobra_annotation_flag_set_by_cobra"
CommandDisplayNameAnnotation = "cobra_annotation_command_display_name" CommandDisplayNameAnnotation = "cobra_annotation_command_display_name"
helpFlagName = "help"
) )
// FParseErrWhitelist configures Flag parse errors to be ignored // FParseErrWhitelist configures Flag parse errors to be ignored
@ -1170,7 +1172,7 @@ func (c *Command) checkCommandGroups() {
// If c already has help flag, it will do nothing. // If c already has help flag, it will do nothing.
func (c *Command) InitDefaultHelpFlag() { func (c *Command) InitDefaultHelpFlag() {
c.mergePersistentFlags() c.mergePersistentFlags()
if c.Flags().Lookup("help") == nil { if c.Flags().Lookup(helpFlagName) == nil {
usage := "help for " usage := "help for "
name := c.DisplayName() name := c.DisplayName()
if name == "" { if name == "" {
@ -1178,8 +1180,8 @@ func (c *Command) InitDefaultHelpFlag() {
} else { } else {
usage += name usage += name
} }
c.Flags().BoolP("help", "h", false, usage) c.Flags().BoolP(helpFlagName, "h", false, usage)
_ = c.Flags().SetAnnotation("help", FlagSetByCobraAnnotation, []string{"true"}) _ = c.Flags().SetAnnotation(helpFlagName, FlagSetByCobraAnnotation, []string{"true"})
} }
} }