-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompletion.go
33 lines (29 loc) · 967 Bytes
/
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
package serpent
import (
"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 known flags and
// subcommands that haven't been exhaustively set.
func DefaultCompletionHandler(inv *Invocation) []string {
var allResps []string
for _, cmd := range inv.Command.Children {
allResps = append(allResps, cmd.Name())
}
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
}