@@ -17,9 +17,6 @@ const (
17
17
ShellCompNoDescRequestCmd = "__completeNoDesc"
18
18
)
19
19
20
- // Global map of flag completion functions.
21
- var flagCompletionFunctions = map [* pflag.Flag ]func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ){}
22
-
23
20
// ShellCompDirective is a bit map representing the different behaviors the shell
24
21
// can be instructed to have once completions have been provided.
25
22
type ShellCompDirective int
@@ -94,10 +91,15 @@ func (c *Command) RegisterFlagCompletionFunc(flagName string, f func(cmd *Comman
94
91
if flag == nil {
95
92
return fmt .Errorf ("RegisterFlagCompletionFunc: flag '%s' does not exist" , flagName )
96
93
}
97
- if _ , exists := flagCompletionFunctions [flag ]; exists {
94
+
95
+ root := c .Root ()
96
+ if _ , exists := root .flagCompletionFunctions [flag ]; exists {
98
97
return fmt .Errorf ("RegisterFlagCompletionFunc: flag '%s' already registered" , flagName )
99
98
}
100
- flagCompletionFunctions [flag ] = f
99
+ if root .flagCompletionFunctions == nil {
100
+ root .flagCompletionFunctions = map [* pflag.Flag ]func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ){}
101
+ }
102
+ root .flagCompletionFunctions [flag ] = f
101
103
return nil
102
104
}
103
105
@@ -374,7 +376,7 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi
374
376
// Find the completion function for the flag or command
375
377
var completionFn func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective )
376
378
if flag != nil {
377
- completionFn = flagCompletionFunctions [flag ]
379
+ completionFn = c . Root (). flagCompletionFunctions [flag ]
378
380
} else {
379
381
completionFn = finalCmd .ValidArgsFunction
380
382
}
0 commit comments