I'd like to be able to use autocomplete for winget commands in PowerShell so I don't have to type them in their entirety.
You can do this yourself with PowerShell's Register-ArgumentCompleter command like so:
$wingetCompleter = {
param($wordToComplete, $commandAst, $cursorPosition)
$tokens = $commandAst.Extent.Text.Trim() -split '\s+'
$completions = switch ($tokens[1]) {
'install' { "-q","-m","-v","-s","-e","-i","-h","-o","-l",
"--query","--manifest","--id","--name","--moniker","--version","--source","--exact","--interactive",
"--silent","--log","--override","--location","--help"; break }
'search' { "-q","-s","-n","-e","-?",
"--query","--id","--name","--moniker","--tag","--command","--source","--count","--exact","--help"
break }
'show' { "-q","-m","-v","-s","-e","-?",
"--query","--manifest","--id","--name","--moniker","--version","--source","--exact","--versions","--help"
break }
'source' { "-?", "add","list","update","remove","reset","--help"; break }
'hash' { "-f","-m","-?","--file","--msix","--help"; break }
'validate' { "-?","--manifest","--help"; break }
default { "install","show","source","search","hash","validate","-v","--help","--info","--version" }
}
$completions | Where-Object {$_ -like "${wordToComplete}*"} | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
Register-ArgumentCompleter -CommandName winget -Native -ScriptBlock $wingetCompleter
This isn't fully fleshed out but could be done so fairly easily. There's a fleshed-out version here. That said, rather than folks external to this project having to provide the completions and then update those completions when new commands are added, it would be better to have winget return completions via a hidden complete command. This is what dotnet does which makes the arg completer func very simple:
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
dotnet complete --position $cursorPosition $commandAst | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
Closing issue as this has been released, see the doc for more details.
Most helpful comment
You can do this yourself with PowerShell's
Register-ArgumentCompletercommand like so:This isn't fully fleshed out but could be done so fairly easily. There's a fleshed-out version here. That said, rather than folks external to this project having to provide the completions and then update those completions when new commands are added, it would be better to have
wingetreturn completions via a hiddencompletecommand. This is whatdotnetdoes which makes the arg completer func very simple: