@@ -30,41 +30,45 @@ func (cmd *Command) writeFishCompletionTemplate(w io.Writer) error {
3030 if err != nil {
3131 return err
3232 }
33- allCommands := []string {}
3433
3534 // Add global flags
36- completions := cmd .prepareFishFlags (cmd .VisibleFlags (), allCommands )
35+ completions := cmd .prepareFishFlags (cmd .VisibleFlags (), [] string {} )
3736
3837 // Add help flag
3938 if ! cmd .HideHelp {
4039 completions = append (
4140 completions ,
42- cmd .prepareFishFlags ([]Flag {HelpFlag }, allCommands )... ,
41+ cmd .prepareFishFlags ([]Flag {HelpFlag }, [] string {} )... ,
4342 )
4443 }
4544
4645 // Add version flag
4746 if ! cmd .HideVersion {
4847 completions = append (
4948 completions ,
50- cmd .prepareFishFlags ([]Flag {VersionFlag }, allCommands )... ,
49+ cmd .prepareFishFlags ([]Flag {VersionFlag }, [] string {} )... ,
5150 )
5251 }
5352
5453 // Add commands and their flags
5554 completions = append (
5655 completions ,
57- cmd .prepareFishCommands (cmd .VisibleCommands (), & allCommands , []string {})... ,
56+ cmd .prepareFishCommands (cmd .VisibleCommands (), []string {})... ,
5857 )
5958
59+ toplevelCommandNames := []string {}
60+ for _ , child := range cmd .Commands {
61+ toplevelCommandNames = append (toplevelCommandNames , child .Names ()... )
62+ }
63+
6064 return t .ExecuteTemplate (w , name , & fishCommandCompletionTemplate {
6165 Command : cmd ,
6266 Completions : completions ,
63- AllCommands : allCommands ,
67+ AllCommands : toplevelCommandNames ,
6468 })
6569}
6670
67- func (cmd * Command ) prepareFishCommands (commands []* Command , allCommands * [] string , previousCommands []string ) []string {
71+ func (cmd * Command ) prepareFishCommands (commands []* Command , previousCommands []string ) []string {
6872 completions := []string {}
6973 for _ , command := range commands {
7074 var completion strings.Builder
@@ -88,7 +92,6 @@ func (cmd *Command) prepareFishCommands(commands []*Command, allCommands *[]stri
8892 )
8993 }
9094
91- * allCommands = append (* allCommands , command .Names ()... )
9295 completions = append (completions , completion .String ())
9396 completions = append (
9497 completions ,
@@ -100,7 +103,7 @@ func (cmd *Command) prepareFishCommands(commands []*Command, allCommands *[]stri
100103 completions = append (
101104 completions ,
102105 cmd .prepareFishCommands (
103- command .Commands , allCommands , command .Names (),
106+ command .Commands , command .Names (),
104107 )... ,
105108 )
106109 }
0 commit comments