From 2c1b2fed74662c4312cadc4a62295d0cd8e86523 Mon Sep 17 00:00:00 2001 From: Alistair Scott Date: Tue, 15 Mar 2022 16:57:42 +0000 Subject: [PATCH] Fix hidden commands changing padding in usage template --- command.go | 29 +++++++++++++++++------------ command_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/command.go b/command.go index ee5365bc..ab4060f8 100644 --- a/command.go +++ b/command.go @@ -1158,18 +1158,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 len(x.Deprecated) == 0 && !x.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 { @@ -1199,6 +1201,9 @@ main: c.commandsMaxCommandPathLen = 0 c.commandsMaxNameLen = 0 for _, command := range c.commands { + if len(command.Deprecated) != 0 || command.Hidden { + continue + } usageLen := len(command.Use) if usageLen > c.commandsMaxUseLen { c.commandsMaxUseLen = usageLen diff --git a/command_test.go b/command_test.go index d48fef1a..f176a4de 100644 --- a/command_test.go +++ b/command_test.go @@ -2161,3 +2161,42 @@ func TestSetContextPersistentPreRun(t *testing.T) { t.Error(err) } } + +func TestPadding(t *testing.T) { + rootCmd := &Command{Use: "root", Run: emptyRun} + childCmd := &Command{Use: "child", Run: emptyRun} + longChildCmd := &Command{Use: "long-name-child-abcdefghijklmnopqrstuvwxyz", Run: emptyRun} + // For this test to be useful the hiddenChildCmd and deprecatedChildCmd commands need to have a longer `Use` field than the other commands. + hiddenChildCmd := &Command{Use: longChildCmd.Use + "-hidden", Hidden: true, Run: emptyRun} + deprecatedChildCmd := &Command{Use: longChildCmd.Use + "-deprecated", Deprecated: "deprecated", Run: emptyRun} + + rootCmd.AddCommand(childCmd) + rootCmd.AddCommand(longChildCmd) + rootCmd.AddCommand(hiddenChildCmd) + rootCmd.AddCommand(deprecatedChildCmd) + + expectedUsePad := len(longChildCmd.Use) + expectedPathPad := len(longChildCmd.CommandPath()) + expectedNamePad := len(longChildCmd.Name()) + + if childCmd.UsagePadding() != expectedUsePad { + t.Errorf("Expected usage padding '%d', got '%d'", expectedUsePad, childCmd.UsagePadding()) + } + if longChildCmd.UsagePadding() != expectedUsePad { + t.Errorf("Expected usage padding '%d', got '%d'", expectedUsePad, longChildCmd.UsagePadding()) + } + + if childCmd.CommandPathPadding() != expectedPathPad { + t.Errorf("Expected command path padding '%d', got '%d'", expectedPathPad, childCmd.CommandPathPadding()) + } + if longChildCmd.CommandPathPadding() != expectedPathPad { + t.Errorf("Expected command path padding '%d', got '%d'", expectedPathPad, longChildCmd.CommandPathPadding()) + } + + if childCmd.NamePadding() != expectedNamePad { + t.Errorf("Expected name padding '%d', got '%d'", expectedNamePad, childCmd.NamePadding()) + } + if longChildCmd.NamePadding() != expectedNamePad { + t.Errorf("Expected name padding '%d', got '%d'", expectedNamePad, longChildCmd.NamePadding()) + } +}