Check if a command is 'hidden' before adding length to usage

This commit is contained in:
Víctor Martínez Bevià 2022-04-29 10:55:10 +02:00
parent 4f0facbcee
commit 6e6f85e119
2 changed files with 47 additions and 23 deletions

View file

@ -1162,18 +1162,20 @@ func (c *Command) AddCommand(cmds ...*Command) {
panic("Command can't be a child of itself") panic("Command can't be a child of itself")
} }
cmds[i].parent = c cmds[i].parent = c
// update max lengths if !cmds[i].Hidden {
usageLen := len(x.Use) // update max lengths
if usageLen > c.commandsMaxUseLen { usageLen := len(x.Use)
c.commandsMaxUseLen = usageLen if usageLen > c.commandsMaxUseLen {
} c.commandsMaxUseLen = usageLen
commandPathLen := len(x.CommandPath()) }
if commandPathLen > c.commandsMaxCommandPathLen { commandPathLen := len(x.CommandPath())
c.commandsMaxCommandPathLen = commandPathLen if commandPathLen > c.commandsMaxCommandPathLen {
} c.commandsMaxCommandPathLen = commandPathLen
nameLen := len(x.Name()) }
if nameLen > c.commandsMaxNameLen { nameLen := len(x.Name())
c.commandsMaxNameLen = nameLen if nameLen > c.commandsMaxNameLen {
c.commandsMaxNameLen = nameLen
}
} }
// If global normalization function exists, update all children // If global normalization function exists, update all children
if c.globNormFunc != nil { if c.globNormFunc != nil {
@ -1203,17 +1205,19 @@ main:
c.commandsMaxCommandPathLen = 0 c.commandsMaxCommandPathLen = 0
c.commandsMaxNameLen = 0 c.commandsMaxNameLen = 0
for _, command := range c.commands { for _, command := range c.commands {
usageLen := len(command.Use) if !command.Hidden {
if usageLen > c.commandsMaxUseLen { usageLen := len(command.Use)
c.commandsMaxUseLen = usageLen if usageLen > c.commandsMaxUseLen {
} c.commandsMaxUseLen = usageLen
commandPathLen := len(command.CommandPath()) }
if commandPathLen > c.commandsMaxCommandPathLen { commandPathLen := len(command.CommandPath())
c.commandsMaxCommandPathLen = commandPathLen if commandPathLen > c.commandsMaxCommandPathLen {
} c.commandsMaxCommandPathLen = commandPathLen
nameLen := len(command.Name()) }
if nameLen > c.commandsMaxNameLen { nameLen := len(command.Name())
c.commandsMaxNameLen = nameLen if nameLen > c.commandsMaxNameLen {
c.commandsMaxNameLen = nameLen
}
} }
} }
} }

View file

@ -2161,3 +2161,23 @@ func TestSetContextPersistentPreRun(t *testing.T) {
t.Error(err) t.Error(err)
} }
} }
func TestCommandsLengthWithoutHiddenCommands(t *testing.T) {
rootCmd := &Command{Use: "root", TraverseChildren: true}
rootCmd.Flags().String("foo", "", "foo things")
// add a normal command
childCmd := &Command{Use: "child"}
childCmd.Flags().String("str", "", "")
rootCmd.AddCommand(childCmd)
//add a hidden command
child2Cmd := &Command{Use: "reallyLongCommand"}
child2Cmd.Flags().String("str", "", "")
child2Cmd.Hidden = true
rootCmd.AddCommand(child2Cmd)
//hidden commands should not be taken into account for usage length
expectedmaxLen := 5
fmt.Println(rootCmd.commandsMaxUseLen)
if rootCmd.commandsMaxUseLen != expectedmaxLen {
t.Errorf("Expected value: \n %v\nGot:\n %v\n", expectedmaxLen, rootCmd.commandsMaxUseLen)
}
}