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,18 +1158,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 len(x.Deprecated) == 0 && !x.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 {
@ -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())
}
}