• 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('-o', 'o', [CompletionResultType]::ParameterName, 'cmd option')
25             [CompletionResult]::new('-O', 'O', [CompletionResultType]::ParameterName, 'cmd option')
26             [CompletionResult]::new('--option', 'option', [CompletionResultType]::ParameterName, 'cmd option')
27             [CompletionResult]::new('--opt', 'opt', [CompletionResultType]::ParameterName, 'cmd option')
28             [CompletionResult]::new('-f', 'f', [CompletionResultType]::ParameterName, 'cmd flag')
29             [CompletionResult]::new('-F', 'F', [CompletionResultType]::ParameterName, 'cmd flag')
30             [CompletionResult]::new('--flag', 'flag', [CompletionResultType]::ParameterName, 'cmd flag')
31             [CompletionResult]::new('--flg', 'flg', [CompletionResultType]::ParameterName, 'cmd flag')
32             [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
33             [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
34             [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version')
35             [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
36             break
37         }
38     })
39 
40     $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
41         Sort-Object -Property ListItemText
42 }
43