• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 using namespace System.Management.Automation
3 using namespace System.Management.Automation.Language
4 
5 Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock {
6     param($wordToComplete, $commandAst, $cursorPosition)
7 
8     $commandElements = $commandAst.CommandElements
9     $command = @(
10         'my-app'
11         for ($i = 1; $i -lt $commandElements.Count; $i++) {
12             $element = $commandElements[$i]
13             if ($element -isnot [StringConstantExpressionAst] -or
14                 $element.StringConstantType -ne [StringConstantType]::BareWord -or
15                 $element.Value.StartsWith('-') -or
16                 $element.Value -eq $wordToComplete) {
17                 break
18         }
19         $element.Value
20     }) -join ';'
21 
22     $completions = @(switch ($command) {
23         'my-app' {
24             [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
25             [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
26             break
27         }
28     })
29 
30     $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
31         Sort-Object -Property ListItemText
32 }
33