/
shell_completions.go
35 lines (27 loc) · 1.08 KB
/
shell_completions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package zulu
import (
"fmt"
"github.com/zulucmd/zflag"
)
// FlagOptFilename instructs the various shell completion implementations to
// limit completions for the flag to the specified file extensions.
func FlagOptFilename(extensions ...string) zflag.Opt {
return zflag.OptAnnotation(BashCompFilenameExt, extensions)
}
// FlagOptDirname instructs the various shell completion implementations to
// limit completions for the flag to directory names.
func FlagOptDirname(dirnames ...string) zflag.Opt {
return zflag.OptAnnotation(BashCompSubdirsInDir, dirnames)
}
// FlagOptCompletionFunc is used to register a function to provide completion for a flag.
func FlagOptCompletionFunc(f func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)) zflag.Opt {
return func(flag *zflag.Flag) error {
flagCompletionMutex.Lock()
defer flagCompletionMutex.Unlock()
if _, exists := flagCompletionFunctions[flag]; exists {
return fmt.Errorf("RegisterFlagCompletionFunc: flag '%s' already registered", flag.Name)
}
flagCompletionFunctions[flag] = f
return nil
}
}