Skip to content

Misbehaviour of IsHelpCommand() #393

@n10v

Description

@n10v

I notice, what IsHelpCommand always returns false even on built-in help command and I found out why:
Look at the docs of IsHelpCommand:

IsHelpCommand determines if a command is a 'help' command; a help command is
determined by the fact that it is NOT runnable/hidden/deprecated, and has no
sub commands that are runnable/hidden/deprecated.

In the docs it says, what help is not runnable, but if you look in initHelpCmd definition

func (c *Command) initHelpCmd() {
	if c.helpCommand == nil {
		if !c.HasSubCommands() {
			return
		}

		c.helpCommand = &Command{
			Use:   "help [command]",
			Short: "Help about any command",
			Long: `Help provides help for any command in the application.
    Simply type ` + c.Name() + ` help [path to command] for full details.`,
			PersistentPreRun:  func(cmd *Command, args []string) {},
			PersistentPostRun: func(cmd *Command, args []string) {},

			Run: func(c *Command, args []string) {
				cmd, _, e := c.Root().Find(args)
				if cmd == nil || e != nil {
					c.Printf("Unknown help topic %#q.", args)
					c.Root().Usage()
				} else {
					cmd.Help()
				}
			},
		}
	}
	c.AddCommand(c.helpCommand)
}

, you can see, what built-in help command has Run function.
So the actual logic of IsHelpCommand is wrong. I think, the possible solution could be:

func (c *Command) IsHelpCommand() bool {
    helpCmd := c.findHelpCommand()
    if c == helpCmd{
        return true
    }
    return false
}

func (c Command) findHelpCommand() *Command {
    if c.helpCommand == nil {
        if c.HasParent() {
             return c.Parent().findHelpCommand()
        }
    }
    return c.helpCommand
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions