From 6e6f85e119135b7fe316058e66ee53e2eaf2de7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez=20Bevi=C3=A0?= Date: Fri, 29 Apr 2022 10:55:10 +0200 Subject: [PATCH] Check if a command is 'hidden' before adding length to usage --- command.go | 50 ++++++++++++++++++++++++++----------------------- command_test.go | 20 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/command.go b/command.go index 0f4511f3..8fad336a 100644 --- a/command.go +++ b/command.go @@ -1162,18 +1162,20 @@ func (c *Command) AddCommand(cmds ...*Command) { panic("Command can't be a child of itself") } cmds[i].parent = c - // update max lengths - usageLen := len(x.Use) - if usageLen > c.commandsMaxUseLen { - c.commandsMaxUseLen = usageLen - } - commandPathLen := len(x.CommandPath()) - if commandPathLen > c.commandsMaxCommandPathLen { - c.commandsMaxCommandPathLen = commandPathLen - } - nameLen := len(x.Name()) - if nameLen > c.commandsMaxNameLen { - c.commandsMaxNameLen = nameLen + if !cmds[i].Hidden { + // update max lengths + usageLen := len(x.Use) + if usageLen > c.commandsMaxUseLen { + c.commandsMaxUseLen = usageLen + } + commandPathLen := len(x.CommandPath()) + if commandPathLen > c.commandsMaxCommandPathLen { + c.commandsMaxCommandPathLen = commandPathLen + } + nameLen := len(x.Name()) + if nameLen > c.commandsMaxNameLen { + c.commandsMaxNameLen = nameLen + } } // If global normalization function exists, update all children if c.globNormFunc != nil { @@ -1203,17 +1205,19 @@ main: c.commandsMaxCommandPathLen = 0 c.commandsMaxNameLen = 0 for _, command := range c.commands { - usageLen := len(command.Use) - if usageLen > c.commandsMaxUseLen { - c.commandsMaxUseLen = usageLen - } - commandPathLen := len(command.CommandPath()) - if commandPathLen > c.commandsMaxCommandPathLen { - c.commandsMaxCommandPathLen = commandPathLen - } - nameLen := len(command.Name()) - if nameLen > c.commandsMaxNameLen { - c.commandsMaxNameLen = nameLen + if !command.Hidden { + usageLen := len(command.Use) + if usageLen > c.commandsMaxUseLen { + c.commandsMaxUseLen = usageLen + } + commandPathLen := len(command.CommandPath()) + if commandPathLen > c.commandsMaxCommandPathLen { + c.commandsMaxCommandPathLen = commandPathLen + } + nameLen := len(command.Name()) + if nameLen > c.commandsMaxNameLen { + c.commandsMaxNameLen = nameLen + } } } } diff --git a/command_test.go b/command_test.go index d48fef1a..9c97d2e7 100644 --- a/command_test.go +++ b/command_test.go @@ -2161,3 +2161,23 @@ func TestSetContextPersistentPreRun(t *testing.T) { 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) + } +}