Skip to content

Autocomplete [tab] winget commands in PowerShell #146

Closed

Description

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)

Activity

  1. added
    Issue-Feature
    on May 15, 2020
  2. added this to the milestone on May 23, 2020
  3. rkeithhill commented on May 26, 2020

    @rkeithhill

    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', $_)
        }
    }
  4. JohnMcPMS commented on Aug 27, 2020

    @JohnMcPMS
    Member

    Closing issue as this has been released, see the doc for more details.

  5. modified the milestones: , v1.0 - Windows Package Manager on May 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Feature

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions