Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ Simply type ` + c.DisplayName() + ` help [path to command] for full details.`,
cmd = c.Root()
}
for _, subCmd := range cmd.Commands() {
if subCmd.IsAvailableCommand() || subCmd == cmd.helpCommand {
if subCmd.IsAvailableCommand() || (subCmd == cmd.helpCommand && !subCmd.Hidden) {
if strings.HasPrefix(subCmd.Name(), toComplete) {
completions = append(completions, CompletionWithDesc(subCmd.Name(), subCmd.Short))
}
Expand Down Expand Up @@ -1375,7 +1375,7 @@ func (c *Command) Groups() []*Group {
// AllChildCommandsHaveGroup returns if all subcommands are assigned to a group
func (c *Command) AllChildCommandsHaveGroup() bool {
for _, sub := range c.commands {
if (sub.IsAvailableCommand() || sub == c.helpCommand) && sub.GroupID == "" {
if (sub.IsAvailableCommand() || (sub == c.helpCommand && !sub.Hidden)) && sub.GroupID == "" {
return false
}
}
Expand Down Expand Up @@ -1949,13 +1949,13 @@ Aliases:
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}

Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (and (eq .Name "help") (not .Hidden)))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}

{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (and (eq .Name "help") (not .Hidden))))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}

Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (and (eq .Name "help") (not .Hidden))))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}

Flags:
Expand Down Expand Up @@ -1993,23 +1993,23 @@ func defaultUsageFunc(w io.Writer, in interface{}) error {
if len(c.Groups()) == 0 {
fmt.Fprintf(w, "\n\nAvailable Commands:")
for _, subcmd := range cmds {
if subcmd.IsAvailableCommand() || subcmd.Name() == helpCommandName {
if subcmd.IsAvailableCommand() || (subcmd == c.helpCommand && !subcmd.Hidden) {
fmt.Fprintf(w, "\n %s %s", rpad(subcmd.Name(), subcmd.NamePadding()), subcmd.Short)
}
}
} else {
for _, group := range c.Groups() {
fmt.Fprintf(w, "\n\n%s", group.Title)
for _, subcmd := range cmds {
if subcmd.GroupID == group.ID && (subcmd.IsAvailableCommand() || subcmd.Name() == helpCommandName) {
if subcmd.GroupID == group.ID && (subcmd.IsAvailableCommand() || (subcmd == c.helpCommand && !subcmd.Hidden)) {
fmt.Fprintf(w, "\n %s %s", rpad(subcmd.Name(), subcmd.NamePadding()), subcmd.Short)
}
}
}
if !c.AllChildCommandsHaveGroup() {
fmt.Fprintf(w, "\n\nAdditional Commands:")
for _, subcmd := range cmds {
if subcmd.GroupID == "" && (subcmd.IsAvailableCommand() || subcmd.Name() == helpCommandName) {
if subcmd.GroupID == "" && (subcmd.IsAvailableCommand() || (subcmd == c.helpCommand && !subcmd.Hidden)) {
fmt.Fprintf(w, "\n %s %s", rpad(subcmd.Name(), subcmd.NamePadding()), subcmd.Short)
}
}
Expand Down
20 changes: 20 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,26 @@ func TestHiddenCommandIsHidden(t *testing.T) {
}
}

func TestHiddenHelpCommandIsHidden(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
childCmd := &Command{Use: "child", Run: emptyRun}
rootCmd.AddCommand(childCmd)
rootCmd.InitDefaultHelpCmd()

// Help command should appear in usage by default.
usage := rootCmd.UsageString()
if !strings.Contains(usage, "help") {
t.Error("help command should appear in usage by default")
}

// Hiding the help command should remove it from usage.
rootCmd.helpCommand.Hidden = true
usage = rootCmd.UsageString()
if strings.Contains(usage, " help") {
t.Error("hidden help command should not appear in usage")
}
}

func TestCommandsAreSorted(t *testing.T) {
EnableCommandSorting = true

Expand Down
Loading