-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkongplete.go
255 lines (229 loc) · 5.98 KB
/
kongplete.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package kongplete
import (
"fmt"
"github.com/alecthomas/kong"
"github.com/posener/complete"
"github.com/willabides/kongplete/internal/positionalpredictor"
)
const predictorTag = "predictor"
type options struct {
predictors map[string]complete.Predictor
exitFunc func(code int)
errorHandler func(error)
}
// Option is a configuration option for running Complete
type Option func(*options)
// WithPredictor use the named predictor
func WithPredictor(name string, predictor complete.Predictor) Option {
return func(o *options) {
if o.predictors == nil {
o.predictors = map[string]complete.Predictor{}
}
o.predictors[name] = predictor
}
}
// WithPredictors use these predictors
func WithPredictors(predictors map[string]complete.Predictor) Option {
return func(o *options) {
for k, v := range predictors {
WithPredictor(k, v)(o)
}
}
}
// WithExitFunc the exit command that is run after completions
func WithExitFunc(exitFunc func(code int)) Option {
return func(o *options) {
o.exitFunc = exitFunc
}
}
// WithErrorHandler handle errors with completions
func WithErrorHandler(handler func(error)) Option {
return func(o *options) {
o.errorHandler = handler
}
}
func buildOptions(opt ...Option) *options {
opts := &options{
predictors: map[string]complete.Predictor{},
}
for _, o := range opt {
o(opts)
}
return opts
}
// Command returns a completion Command for a kong parser
func Command(parser *kong.Kong, opt ...Option) (complete.Command, error) {
opts := buildOptions(opt...)
if parser == nil || parser.Model == nil {
return complete.Command{}, nil
}
command, err := nodeCommand(parser.Model.Node, opts.predictors)
if err != nil {
return complete.Command{}, err
}
return *command, err
}
// Complete runs completion for a kong parser
func Complete(parser *kong.Kong, opt ...Option) {
if parser == nil {
return
}
opts := buildOptions(opt...)
errHandler := opts.errorHandler
if errHandler == nil {
errHandler = func(err error) {
parser.Errorf("error running command completion: %v", err)
}
}
exitFunc := opts.exitFunc
if exitFunc == nil {
exitFunc = parser.Exit
}
cmd, err := Command(parser, opt...)
if err != nil {
errHandler(err)
exitFunc(1)
}
cmp := complete.New(parser.Model.Name, cmd)
cmp.Out = parser.Stdout
done := cmp.Complete()
if done {
exitFunc(0)
}
}
func nodeCommand(node *kong.Node, predictors map[string]complete.Predictor) (*complete.Command, error) {
if node == nil {
return nil, nil
}
cmd := complete.Command{
Sub: complete.Commands{},
GlobalFlags: complete.Flags{},
}
for _, child := range node.Children {
if child == nil || child.Hidden {
continue
}
childCmd, err := nodeCommand(child, predictors)
if err != nil {
return nil, err
}
if childCmd != nil {
cmd.Sub[child.Name] = *childCmd
}
}
for _, flag := range node.Flags {
if flag == nil || flag.Hidden {
continue
}
predictor, err := flagPredictor(flag, predictors)
if err != nil {
return nil, err
}
for _, f := range flagNamesWithHyphens(flag) {
cmd.GlobalFlags[f] = predictor
}
}
boolFlags, nonBoolFlags := boolAndNonBoolFlags(node.Flags)
isCumulative := false
if len(node.Positional) > 0 && node.Positional[len(node.Positional)-1].IsCumulative() {
isCumulative = true
}
pps, err := positionalPredictors(node.Positional, predictors)
if err != nil {
return nil, err
}
cmd.Args = &positionalpredictor.PositionalPredictor{
Predictors: pps,
ArgFlags: flagNamesWithHyphens(nonBoolFlags...),
BoolFlags: flagNamesWithHyphens(boolFlags...),
IsCumulative: isCumulative,
}
return &cmd, nil
}
func flagNamesWithHyphens(flags ...*kong.Flag) []string {
names := make([]string, 0, len(flags)*2)
if flags == nil {
return names
}
for _, flag := range flags {
names = append(names, "--"+flag.Name)
if flag.Short != 0 {
names = append(names, "-"+string(flag.Short))
}
}
return names
}
// boolAndNonBoolFlags divides a list of flags into boolean and non-boolean flags
func boolAndNonBoolFlags(flags []*kong.Flag) (boolFlags, nonBoolFlags []*kong.Flag) {
boolFlags = make([]*kong.Flag, 0, len(flags))
nonBoolFlags = make([]*kong.Flag, 0, len(flags))
for _, flag := range flags {
switch flag.Value.IsBool() {
case true:
boolFlags = append(boolFlags, flag)
case false:
nonBoolFlags = append(nonBoolFlags, flag)
}
}
return boolFlags, nonBoolFlags
}
// kongTag interface for *kong.kongTag
type kongTag interface {
Has(string) bool
Get(string) string
}
func tagPredictor(tag kongTag, predictors map[string]complete.Predictor) (complete.Predictor, error) {
if tag == nil {
return nil, nil
}
if !tag.Has(predictorTag) {
return nil, nil
}
if predictors == nil {
predictors = map[string]complete.Predictor{}
}
predictorName := tag.Get(predictorTag)
predictor, ok := predictors[predictorName]
if !ok {
return nil, fmt.Errorf("no predictor with name %q", predictorName)
}
return predictor, nil
}
func valuePredictor(value *kong.Value, predictors map[string]complete.Predictor) (complete.Predictor, error) {
if value == nil {
return nil, nil
}
predictor, err := tagPredictor(value.Tag, predictors)
if err != nil {
return nil, err
}
if predictor != nil {
return predictor, nil
}
switch {
case value.IsBool():
return complete.PredictNothing, nil
case value.Enum != "":
enumVals := make([]string, 0, len(value.EnumMap()))
for enumVal := range value.EnumMap() {
enumVals = append(enumVals, enumVal)
}
return complete.PredictSet(enumVals...), nil
default:
return complete.PredictAnything, nil
}
}
func positionalPredictors(args []*kong.Positional, predictors map[string]complete.Predictor) ([]complete.Predictor, error) {
res := make([]complete.Predictor, len(args))
var err error
for i, arg := range args {
res[i], err = valuePredictor(arg, predictors)
if err != nil {
return nil, err
}
}
return res, nil
}
func flagPredictor(flag *kong.Flag, predictors map[string]complete.Predictor) (complete.Predictor, error) {
return valuePredictor(flag.Value, predictors)
}