-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcompletion.go
More file actions
390 lines (367 loc) · 11.8 KB
/
completion.go
File metadata and controls
390 lines (367 loc) · 11.8 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package flaggy
import (
"fmt"
"strings"
)
// EnableCompletion enables shell autocomplete outputs to be generated.
func EnableCompletion() {
DefaultParser.ShowCompletion = true
}
// DisableCompletion disallows shell autocomplete outputs to be generated.
func DisableCompletion() {
DefaultParser.ShowCompletion = false
}
// GenerateBashCompletion returns a bash completion script for the parser.
func GenerateBashCompletion(p *Parser) string {
var b strings.Builder
funcName := "_" + sanitizeName(p.Name) + "_complete"
b.WriteString("# bash completion for " + p.Name + "\n")
b.WriteString(funcName + "() {\n")
b.WriteString(" local cur prev\n")
b.WriteString(" COMPREPLY=()\n")
b.WriteString(" cur=\"${COMP_WORDS[COMP_CWORD]}\"\n")
b.WriteString(" prev=\"${COMP_WORDS[COMP_CWORD-1]}\"\n")
b.WriteString(" case \"$prev\" in\n")
bashCaseEntries(&p.Subcommand, &b)
rootOpts := collectOptions(&p.Subcommand)
b.WriteString(" *)\n COMPREPLY=( $(compgen -W \"" + rootOpts + "\" -- \"$cur\") )\n return 0\n ;;\n esac\n}\n")
b.WriteString("complete -F " + funcName + " " + p.Name + "\n")
return b.String()
}
// GenerateZshCompletion returns a zsh completion script for the parser.
func GenerateZshCompletion(p *Parser) string {
var b strings.Builder
funcName := "_" + sanitizeName(p.Name)
b.WriteString("#compdef " + p.Name + "\n\n")
b.WriteString(funcName + "() {\n")
b.WriteString(" local cur prev\n")
b.WriteString(" cur=${words[CURRENT]}\n")
b.WriteString(" prev=${words[CURRENT-1]}\n")
b.WriteString(" case \"$prev\" in\n")
zshCaseEntries(&p.Subcommand, &b)
rootOpts := collectOptions(&p.Subcommand)
b.WriteString(" *)\n compadd -- " + rootOpts + "\n ;;\n esac\n}\n")
b.WriteString("compdef " + funcName + " " + p.Name + "\n")
return b.String()
}
// GenerateFishCompletion returns a fish completion script for the parser.
func GenerateFishCompletion(p *Parser) string {
var b strings.Builder
b.WriteString("# fish completion for " + p.Name + "\n")
writeFishEntries(&p.Subcommand, &b, p.Name, nil)
return b.String()
}
// GeneratePowerShellCompletion returns a PowerShell completion script for the parser.
func GeneratePowerShellCompletion(p *Parser) string {
var b strings.Builder
b.WriteString("# PowerShell completion for " + p.Name + "\n")
b.WriteString("Register-ArgumentCompleter -CommandName '" + p.Name + "' -ScriptBlock {\n")
b.WriteString(" param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)\n")
b.WriteString(" $completions = @(\n")
writePowerShellEntries(&p.Subcommand, &b)
b.WriteString(" )\n")
b.WriteString(" $completions | Where-Object { $_.CompletionText -like \"$wordToComplete*\" }\n")
b.WriteString("}\n")
return b.String()
}
// GenerateNushellCompletion returns a Nushell completion script for the parser.
func GenerateNushellCompletion(p *Parser) string {
var b strings.Builder
command := p.Name
funcName := "nu-complete " + command
b.WriteString("# nushell completion for " + command + "\n")
b.WriteString("def \"" + funcName + "\" [] {\n")
b.WriteString(" [\n")
writeNushellEntries(&p.Subcommand, &b)
b.WriteString(" ]\n")
b.WriteString("}\n\n")
b.WriteString("extern \"" + command + "\" [\n")
writeNushellFlagSignature(&p.Subcommand, &b)
b.WriteString(" command?: string@\"" + funcName + "\"\n")
b.WriteString("]\n")
return b.String()
}
// collectOptions builds a space-delimited list of flags, subcommands, and positional values
// for the provided subcommand.
func collectOptions(sc *Subcommand) string {
var opts []string
for _, f := range sc.Flags {
if len(f.ShortName) > 0 {
opts = append(opts, "-"+f.ShortName)
}
if len(f.LongName) > 0 {
opts = append(opts, "--"+f.LongName)
}
}
for _, p := range sc.PositionalFlags {
if p.Name != "" {
opts = append(opts, p.Name)
}
}
for _, s := range sc.Subcommands {
if s.Hidden {
continue
}
if s.Name != "" {
opts = append(opts, s.Name)
}
if s.ShortName != "" {
opts = append(opts, s.ShortName)
}
}
return strings.Join(opts, " ")
}
func bashCaseEntries(sc *Subcommand, b *strings.Builder) {
for _, s := range sc.Subcommands {
if s.Hidden {
continue
}
opts := collectOptions(s)
b.WriteString(" " + s.Name + ")\n COMPREPLY=( $(compgen -W \"" + opts + "\" -- \"$cur\") )\n return 0\n ;;\n")
if s.ShortName != "" {
b.WriteString(" " + s.ShortName + ")\n COMPREPLY=( $(compgen -W \"" + opts + "\" -- \"$cur\") )\n return 0\n ;;\n")
}
bashCaseEntries(s, b)
}
}
func zshCaseEntries(sc *Subcommand, b *strings.Builder) {
for _, s := range sc.Subcommands {
if s.Hidden {
continue
}
opts := collectOptions(s)
b.WriteString(" " + s.Name + ")\n compadd -- " + opts + "\n return\n ;;\n")
if s.ShortName != "" {
b.WriteString(" " + s.ShortName + ")\n compadd -- " + opts + "\n return\n ;;\n")
}
zshCaseEntries(s, b)
}
}
func sanitizeName(n string) string {
return strings.ReplaceAll(n, "-", "_")
}
// writeFishEntries builds the fish completion statements for the provided subcommand path so
// the generated script mirrors Flaggy's flag and subcommand hierarchy for interactive use.
func writeFishEntries(sc *Subcommand, b *strings.Builder, command string, path []string) {
condition := fishConditionForFlags(path)
for _, f := range sc.Flags {
if f.Hidden {
continue
}
line := "complete -c " + command
if condition != "" {
line += " -n '" + condition + "'"
}
if f.ShortName != "" {
line += " -s " + f.ShortName
}
if f.LongName != "" {
line += " -l " + f.LongName
}
if f.Description != "" {
line += " -d '" + escapeSingleQuotes(f.Description) + "'"
}
line += "\n"
b.WriteString(line)
}
for _, p := range sc.PositionalFlags {
if p.Hidden {
continue
}
if p.Name == "" {
continue
}
line := "complete -c " + command
if condition != "" {
line += " -n '" + condition + "'"
}
line += " -a '" + escapeSingleQuotes(p.Name) + "'"
if p.Description != "" {
line += " -d '" + escapeSingleQuotes(p.Description) + "'"
}
line += "\n"
b.WriteString(line)
}
subCondition := fishConditionForSubcommands(path)
for _, sub := range sc.Subcommands {
if sub.Hidden {
continue
}
line := "complete -c " + command
if subCondition != "" {
line += " -n '" + subCondition + "'"
}
line += " -a '" + escapeSingleQuotes(sub.Name) + "'"
if sub.Description != "" {
line += " -d '" + escapeSingleQuotes(sub.Description) + "'"
}
line += "\n"
b.WriteString(line)
if sub.ShortName != "" {
aliasLine := "complete -c " + command
if subCondition != "" {
aliasLine += " -n '" + subCondition + "'"
}
aliasLine += " -a '" + escapeSingleQuotes(sub.ShortName) + "'"
if sub.Description != "" {
aliasLine += " -d '" + escapeSingleQuotes(sub.Description) + "'"
}
aliasLine += "\n"
b.WriteString(aliasLine)
}
nextPath := appendPath(path, sub.Name)
writeFishEntries(sub, b, command, nextPath)
}
}
// fishConditionForFlags returns the fish condition needed to scope flag suggestions to the
// current subcommand path while leaving root flags globally available.
func fishConditionForFlags(path []string) string {
if len(path) == 0 {
return ""
}
return "__fish_seen_subcommand_from " + path[len(path)-1]
}
// fishConditionForSubcommands returns the fish condition that ensures subcommand suggestions
// appear only after their parent command token has been entered.
func fishConditionForSubcommands(path []string) string {
if len(path) == 0 {
return "__fish_use_subcommand"
}
return "__fish_seen_subcommand_from " + path[len(path)-1]
}
// appendPath creates a new slice with the next subcommand name appended so recursive
// completion builders can keep the traversal stack immutable.
func appendPath(path []string, value string) []string {
next := make([]string, len(path)+1)
copy(next, path)
next[len(path)] = value
return next
}
// escapeSingleQuotes prepares text for inclusion in single-quoted shell strings so flag
// descriptions render safely in the generated scripts.
func escapeSingleQuotes(s string) string {
return strings.ReplaceAll(s, "'", "\\'")
}
// escapeDoubleQuotes prepares text for inclusion in double-quoted shell strings which is
// required for PowerShell and Nushell emission.
func escapeDoubleQuotes(s string) string {
return strings.ReplaceAll(s, "\"", "\\\"")
}
// writePowerShellEntries walks the parser tree and emits CompletionResult entries so the
// PowerShell script can surface flags, positionals, and subcommands interactively.
func writePowerShellEntries(sc *Subcommand, b *strings.Builder) {
for _, f := range sc.Flags {
if f.Hidden {
continue
}
if f.LongName != "" {
writePowerShellLine("--"+f.LongName, f.Description, "ParameterName", b)
}
if f.ShortName != "" {
writePowerShellLine("-"+f.ShortName, f.Description, "ParameterName", b)
}
}
for _, p := range sc.PositionalFlags {
if p.Hidden {
continue
}
if p.Name == "" {
continue
}
writePowerShellLine(p.Name, p.Description, "ParameterValue", b)
}
for _, sub := range sc.Subcommands {
if sub.Hidden {
continue
}
writePowerShellLine(sub.Name, sub.Description, "Command", b)
if sub.ShortName != "" {
writePowerShellLine(sub.ShortName, sub.Description, "Command", b)
}
writePowerShellEntries(sub, b)
}
}
// writePowerShellLine emits a single CompletionResult definition with the supplied tooltip and
// completion type for consumption by Register-ArgumentCompleter.
func writePowerShellLine(value, description, kind string, b *strings.Builder) {
tooltip := description
if tooltip == "" {
tooltip = value
}
line := fmt.Sprintf(" [System.Management.Automation.CompletionResult]::new(\"%s\", \"%s\", \"%s\", \"%s\")\n", escapeDoubleQuotes(value), escapeDoubleQuotes(value), kind, escapeDoubleQuotes(tooltip))
b.WriteString(line)
}
// writeNushellEntries collects all completion values into Nushell's structured format so
// external commands can expose their interactive help inside the shell.
func writeNushellEntries(sc *Subcommand, b *strings.Builder) {
for _, f := range sc.Flags {
if f.Hidden {
continue
}
if f.LongName != "" {
writeNushellLine("--"+f.LongName, f.Description, b)
}
if f.ShortName != "" {
writeNushellLine("-"+f.ShortName, f.Description, b)
}
}
for _, p := range sc.PositionalFlags {
if p.Hidden {
continue
}
if p.Name == "" {
continue
}
writeNushellLine(p.Name, p.Description, b)
}
for _, sub := range sc.Subcommands {
if sub.Hidden {
continue
}
writeNushellLine(sub.Name, sub.Description, b)
if sub.ShortName != "" {
writeNushellLine(sub.ShortName, sub.Description, b)
}
writeNushellEntries(sub, b)
}
}
// writeNushellLine emits a single structured completion item for Nushell with a value and
// friendly description.
func writeNushellLine(value, description string, b *strings.Builder) {
tooltip := description
if tooltip == "" {
tooltip = value
}
line := fmt.Sprintf(" { value: \"%s\", description: \"%s\" }\n", escapeDoubleQuotes(value), escapeDoubleQuotes(tooltip))
b.WriteString(line)
}
// writeNushellFlagSignature appends flag signature stubs so Nushell understands which
// switches are available when invoking the external command.
func writeNushellFlagSignature(sc *Subcommand, b *strings.Builder) {
for _, f := range sc.Flags {
if f.Hidden {
continue
}
if f.LongName != "" || f.ShortName != "" {
line := " "
if f.LongName != "" {
line += "--" + f.LongName
}
if f.ShortName != "" {
if f.LongName != "" {
line += "(-" + f.ShortName + ")"
} else {
line += "-" + f.ShortName
}
}
line += "\n"
b.WriteString(line)
}
}
for _, sub := range sc.Subcommands {
if sub.Hidden {
continue
}
writeNushellFlagSignature(sub, b)
}
}