-
Notifications
You must be signed in to change notification settings - Fork 3
/
completion.go
40 lines (35 loc) · 1.09 KB
/
completion.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
36
37
38
39
40
package serpent
import (
"strings"
"github.com/spf13/pflag"
)
// CompletionModeEnv is a special environment variable that is
// set when the command is being run in completion mode.
const CompletionModeEnv = "COMPLETION_MODE"
// IsCompletionMode returns true if the command is being run in completion mode.
func (inv *Invocation) IsCompletionMode() bool {
_, ok := inv.Environ.Lookup(CompletionModeEnv)
return ok
}
// DefaultCompletionHandler is a handler that prints all the subcommands, or
// all the options that haven't been exhaustively set, if the current word
// starts with a dash.
func DefaultCompletionHandler(inv *Invocation) []string {
_, cur := inv.CurWords()
var allResps []string
if strings.HasPrefix(cur, "-") {
for _, opt := range inv.Command.Options {
_, isSlice := opt.Value.(pflag.SliceValue)
if opt.ValueSource == ValueSourceNone ||
opt.ValueSource == ValueSourceDefault ||
isSlice {
allResps = append(allResps, "--"+opt.Flag)
}
}
return allResps
}
for _, cmd := range inv.Command.Children {
allResps = append(allResps, cmd.Name())
}
return allResps
}