-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion_powershell.go
More file actions
70 lines (55 loc) · 1.81 KB
/
completion_powershell.go
File metadata and controls
70 lines (55 loc) · 1.81 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
package cli
import (
"context"
"fmt"
)
// PowerShellCompletion implements PowerShell completion
type PowerShellCompletion struct{}
func (p *PowerShellCompletion) GetCompletions(cmd *Command, args []string) []string {
return getCompletionWords(cmd)
}
func (p *PowerShellCompletion) Register(cmd *Command) {
psCmd := Cmd("__powershellcomplete").
Description("PowerShell completion helper").
Hidden().
Action(func(ctx context.Context, psCommand *Command) error {
targetCmd := psCommand.GetParent()
// For completion, we don't need args since we complete the parent
words := p.GetCompletions(targetCmd, nil)
for _, word := range words {
fmt.Println(word)
}
return nil
})
cmd.AddCommand(psCmd)
// Recursively register for all subcommands
for _, subcmd := range cmd.GetCommands() {
if !subcmd.IsHidden() {
p.Register(subcmd)
}
}
}
func (p *PowerShellCompletion) GenerateScript(cmd *Command) string {
cmdName := cmd.GetName()
script := fmt.Sprintf(`# PowerShell completion script for %s
# Add this to your PowerShell profile:
# %s __powershellcomplete | Out-String | Invoke-Expression
Register-ArgumentCompleter -Native -CommandName %s -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$cmdPath = $commandAst.CommandElements[0].Value
# Build command path from AST
for ($i = 1; $i -lt $commandAst.CommandElements.Count; $i++) {
$element = $commandAst.CommandElements[$i].Value
if (-not $element.StartsWith('-')) {
$cmdPath = "$cmdPath $element"
}
}
# Get completions
$completions = & $cmdPath __powershellcomplete 2>$null
$completions | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
`, cmdName, cmdName, cmdName)
return script
}