Skip to content

Commit

Permalink
make string "help" a constant more
Browse files Browse the repository at this point in the history
  • Loading branch information
aarzilli committed Dec 26, 2024
1 parent 45cac89 commit e7c6c98
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const (
FlagSetByCobraAnnotation = "cobra_annotation_flag_set_by_cobra"
CommandDisplayNameAnnotation = "cobra_annotation_command_display_name"

helpFlagName = "help"
helpFlagName = "help"
helpCommandName = "help"
)

// FParseErrWhitelist configures Flag parse errors to be ignored
Expand Down Expand Up @@ -873,7 +874,7 @@ func (c *Command) execute(a []string) (err error) {

// If help is called, regardless of other flags, return we want help.
// Also say we need help if the command isn't runnable.
helpVal, err := c.Flags().GetBool("help")
helpVal, err := c.Flags().GetBool(helpFlagName)
if err != nil {
// should be impossible to get here as we always declare a help
// flag in InitDefaultHelpFlag()
Expand Down Expand Up @@ -1940,23 +1941,23 @@ func defaultUsageFunc(w io.Writer, in interface{}) error {
if len(c.Groups()) == 0 {
fmt.Fprintf(w, "\n\nAvailable Commands:")
for _, subcmd := range cmds {
if subcmd.IsAvailableCommand() || subcmd.Name() == "help" {
if subcmd.IsAvailableCommand() || subcmd.Name() == helpCommandName {
fmt.Fprintf(w, "\n %s %s", rpad(subcmd.Name(), subcmd.NamePadding()), subcmd.Short)
}
}
} else {
for _, group := range c.Groups() {
fmt.Fprintf(w, "\n\n%s", group.Title)
for _, subcmd := range cmds {
if subcmd.GroupID == group.ID && (subcmd.IsAvailableCommand() || subcmd.Name() == "help") {
if subcmd.GroupID == group.ID && (subcmd.IsAvailableCommand() || subcmd.Name() == helpCommandName) {
fmt.Fprintf(w, "\n %s %s", rpad(subcmd.Name(), subcmd.NamePadding()), subcmd.Short)
}
}
}
if !c.AllChildCommandsHaveGroup() {
fmt.Fprintf(w, "\n\nAdditional Commands:")
for _, subcmd := range cmds {
if subcmd.GroupID == "" && (subcmd.IsAvailableCommand() || subcmd.Name() == "help") {
if subcmd.GroupID == "" && (subcmd.IsAvailableCommand() || subcmd.Name() == helpCommandName) {
fmt.Fprintf(w, "\n %s %s", rpad(subcmd.Name(), subcmd.NamePadding()), subcmd.Short)
}
}
Expand Down

0 comments on commit e7c6c98

Please sign in to comment.