From 16f2a0735a9362ec5e68303efbc2774fcd46544c Mon Sep 17 00:00:00 2001 From: silenceshell Date: Thu, 24 Jun 2021 13:51:23 +0800 Subject: [PATCH] set to root command --- completions.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/completions.go b/completions.go index 267b6fba..cdeed000 100644 --- a/completions.go +++ b/completions.go @@ -93,13 +93,14 @@ func (c *Command) RegisterFlagCompletionFunc(flagName string, f func(cmd *Comman return fmt.Errorf("RegisterFlagCompletionFunc: flag '%s' does not exist", flagName) } - if _, exists := c.Root().flagCompletionFunctions[flag]; exists { + root := c.Root() + if _, exists := root.flagCompletionFunctions[flag]; exists { return fmt.Errorf("RegisterFlagCompletionFunc: flag '%s' already registered", flagName) } - if c.Root().flagCompletionFunctions == nil { - c.Root().flagCompletionFunctions = map[*pflag.Flag]func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective){} + if root.flagCompletionFunctions == nil { + root.flagCompletionFunctions = map[*pflag.Flag]func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective){} } - c.Root().flagCompletionFunctions[flag] = f + root.flagCompletionFunctions[flag] = f return nil }