winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

Completion.md (4163B)


      1 # WinGet Command Line Tab Completion
      2 
      3 WinGet offers a `complete` command that can be leveraged by your shell to provide context sensitive tab completion. It allows for completion of command names, argument names, and argument values, dependent on the current command line state.
      4 
      5 > Note, this feature was released in [v0.1.42241 Preview](https://github.com/microsoft/winget-cli/releases/tag/v0.1.42241-preview). Please update if you are on an older build.
      6 
      7 ## Examples
      8 
      9 > These examples assume that the tab completion in your shell works similar to PowerShell; repeated presses of tab (`⇥`) will result in cycling through the possible values.
     10 
     11 Input | Result | Reason
     12 --- | --- | ---
     13 `winget ⇥` | `winget install` | `install` is the first command below the root
     14 `winget sh⇥` | `winget show` | `show` is the first command that starts with `sh`
     15 `winget source l⇥` | `winget source list` | `list` is the first sub-command of source that starts with `l`
     16 `winget -⇥` | `winget --version` | `--version` is the first argument defined for the root
     17 `winget install power⇥` | `winget install "Power Toys"` | `"Power Toys"` is the first package whose Id, Name, or Moniker starts with `power`
     18 `winget install "Power Toys" --version ⇥` | `winget install "Power Toys" --version 0.19.2` | `0.19.2` is the highest version of Power Toys at the time of writing
     19 
     20 ## PowerShell
     21 
     22 You can add the argument completer to your `$PROFILE`, which will enable it in all subsequent PowerShell sessions.
     23 For more information, see [How to create your profile](https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_profiles#how-to-create-a-profile) and [Profiles and execution policy](https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_profiles#profiles-and-execution-policy).
     24 
     25 Here is the PowerShell command to add to your `$PROFILE`:
     26 
     27 ```PowerShell
     28 Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
     29     param($wordToComplete, $commandAst, $cursorPosition)
     30         [Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
     31         $Local:word = $wordToComplete.Replace('"', '""')
     32         $Local:ast = $commandAst.ToString().Replace('"', '""')
     33         winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
     34             [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
     35         }
     36 }
     37 ```
     38 
     39 ## Command Reference
     40 
     41 The complete command takes 3 required arguments:
     42 
     43 Argument | Description
     44 --- | ---
     45 `--word` | The current word that is being completed; the token that the cursor is located within. Can be empty to indicate no current value at the cursor, but if provided, it must appear as a substring in the command line.
     46 `--commandline` | The entire current command line, including `winget`. See the examples above; everything but the tab character (`⇥`) should be provided to this argument.
     47 `--position` | The current position of the cursor in the command line. Can be greater than the length of the command line string to indicate at the end.
     48 
     49 When a word value is provided, the completion operates in replacement mode.  It will suggest completions that would fit correctly at this location that also start with the given word value.
     50 
     51 When a word value is not provided (an empty value is provided for word, ex. `--word=`), the completion operates in insertion mode.  It will suggest completions that would fit as a new value in the cursor's location.
     52 
     53 Based on the arguments, the completions suggested can be one of:
     54 1. A sub command :: The cursor is located just after a command and there are sub commands available.
     55 2. An argument specifier :: The cursor is not positioned after an argument specifier that expects a value, and there are arguments available.
     56 3. An argument value :: The cursor is positioned after an argument specifier that expects a value, or a positional argument is expected.
     57 
     58 After evaluating all of these cases, the potential completions are output, one on each line. If the completion string contains a space, it is wrapped in quotations.