Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# winget parameter completion
Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
$Local:word = $wordToComplete.Replace('"', '""')
$Local:ast = $commandAst.ToString().Replace('"', '""')
winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# PowerShell parameter completion shim for the dotnet CLI
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# PowerShell parameter completion shim for the npm CLI
Register-ArgumentCompleter -Native -CommandName npm -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$Local:ast = $commandAst.ToString().Replace(' ', '')
if ($Local:ast -eq 'npm') {
$command = 'run install start'
$array = $command.Split(' ')
$array |
Where-Object { $_ -like "$wordToComplete*" } |
ForEach-Object {
New-Object -Type System.Management.Automation.CompletionResult -ArgumentList $_
}
}
if ($Local:ast -eq 'npmrun') {
$scripts = (Get-Content .\package.json | ConvertFrom-Json).scripts
$scripts |
Get-Member -MemberType NoteProperty |
Where-Object { $_.Name -like "$wordToComplete*" } |
ForEach-Object {
New-Object -Type System.Management.Automation.CompletionResult -ArgumentList $_.Name
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment