/
smartsuggest.go
55 lines (45 loc) · 1.12 KB
/
smartsuggest.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package internal
import (
"strings"
"github.com/c-bata/go-prompt"
)
// SmartSuggest is an advanced struct that has additional properties.
type SmartSuggest struct {
prompt.Suggest
Required bool
}
// SmartSuggestGroup is a group.
type SmartSuggestGroup []SmartSuggest
// Valid returns true if all of the required fields are found or false with the
// field that is missing.
func (g SmartSuggestGroup) Valid(list []string) (bool, string) {
for _, suggestion := range g {
if suggestion.Required {
found := false
missingArg := suggestion.Text
for k, typed := range list {
// Determine if the argument is found.
if typed == suggestion.Text {
// Determine if there is a value that is not a parameter
// after it.
if len(list) >= k+2 && !strings.HasPrefix(list[k+1], "-") {
found = true
}
break
}
}
if !found {
return false, missingArg
}
}
}
return true, ""
}
// ToSuggest returns an array of suggestions.
func (g SmartSuggestGroup) ToSuggest() []prompt.Suggest {
arr := make([]prompt.Suggest, 0)
for _, v := range g {
arr = append(arr, v.Suggest)
}
return arr
}