-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.go
103 lines (97 loc) · 2.16 KB
/
args.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package wings
import (
"github.com/c-bata/go-prompt"
"github.com/df-mc/dragonfly/server/cmd"
"go/types"
"strings"
)
// Argument should be implemented on custom command argument types to allow for better cmdline support.
type Argument interface {
Description() string
}
func (w *wings) GenerateSubCommandSuggestions(c cmd.Command, args []string) []prompt.Suggest {
params := c.Params(w.source)
s := make([]prompt.Suggest, 0)
for i := len(args) - 1; i >= 0; i-- {
name := args[i]
for _, l := range params {
f := false
bottomLevel := make([]prompt.Suggest, 0)
for _, param := range l {
if _, ok := param.Value.(cmd.SubCommand); ok {
if param.Name == strings.TrimSpace(name) {
f = true
// SubSub command.
} else if f {
s = append(s, prompt.Suggest{Text: param.Name, Description: paramToDescription(param)})
break
}
} else {
bottomLevel = append(bottomLevel, prompt.Suggest{Text: param.Name, Description: paramToDescription(param)})
}
}
if f {
s = append(s, bottomLevel[:]...)
}
}
}
return s
}
func (w *wings) GenerateArgSuggestions(c cmd.Command) []prompt.Suggest {
params := c.Params(w.source)
s := make([]prompt.Suggest, 0)
for _, l := range params {
if len(l) == 0 {
continue
}
param := l[0]
s = append(s, prompt.Suggest{
Text: param.Name,
Description: paramToDescription(param),
})
}
return s
}
func paramToDescription(param cmd.ParamInfo) string {
d := ""
switch param.Value.(type) {
case cmd.SubCommand:
d = "Subcommand"
case int64:
d = "Number"
case int32:
d = "Number"
case int16:
d = "Number"
case int:
d = "Number"
case uint64:
d = "Positive Number"
case uint32:
d = "Positive Number"
case uint16:
d = "Positive Number"
case uint:
d = "Positive Number"
case uint8:
d = "One byte"
case int8:
d = "One byte"
case float64:
case float32:
d = "Decimal Number"
case string:
d = "string"
case types.Slice:
x := param.Value.(types.Slice)
d = "List of " + x.Elem().String()
case Argument:
d = param.Value.(Argument).Description()
default:
d = "No Description/Type found."
}
if param.Optional {
d = "Optional " + d
}
return d
}