Fix hidden commands changing padding in usage template

This commit is contained in:
Alistair Scott 2022-03-15 16:57:42 +00:00
parent 03c3eb73f8
commit 2c1b2fed74
2 changed files with 56 additions and 12 deletions

View file

@ -1158,6 +1158,7 @@ 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
if len(x.Deprecated) == 0 && !x.Hidden {
// update max lengths // update max lengths
usageLen := len(x.Use) usageLen := len(x.Use)
if usageLen > c.commandsMaxUseLen { if usageLen > c.commandsMaxUseLen {
@ -1171,6 +1172,7 @@ func (c *Command) AddCommand(cmds ...*Command) {
if nameLen > c.commandsMaxNameLen { if nameLen > c.commandsMaxNameLen {
c.commandsMaxNameLen = nameLen 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 {
x.SetGlobalNormalizationFunc(c.globNormFunc) x.SetGlobalNormalizationFunc(c.globNormFunc)
@ -1199,6 +1201,9 @@ main:
c.commandsMaxCommandPathLen = 0 c.commandsMaxCommandPathLen = 0
c.commandsMaxNameLen = 0 c.commandsMaxNameLen = 0
for _, command := range c.commands { for _, command := range c.commands {
if len(command.Deprecated) != 0 || command.Hidden {
continue
}
usageLen := len(command.Use) usageLen := len(command.Use)
if usageLen > c.commandsMaxUseLen { if usageLen > c.commandsMaxUseLen {
c.commandsMaxUseLen = usageLen c.commandsMaxUseLen = usageLen

View file

@ -2161,3 +2161,42 @@ func TestSetContextPersistentPreRun(t *testing.T) {
t.Error(err) 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())
}
}