Winget-cli: Autocomplete [tab] winget commands in PowerShell

Created on 15 May 2020  路  2Comments  路  Source: microsoft/winget-cli

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.

Proposed technical implementation details (optional)

Issue-Feature

Most helpful comment

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', $_)
    }
}

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jantari picture jantari  路  31Comments

paule96 picture paule96  路  12Comments

revanmj picture revanmj  路  12Comments

denelon picture denelon  路  41Comments

denelon picture denelon  路  21Comments