1
2using namespace System.Management.Automation
3using namespace System.Management.Automation.Language
4
5Register-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('-c', 'c', [CompletionResultType]::ParameterName, 'some config file')
25            [CompletionResult]::new('-C', 'C', [CompletionResultType]::ParameterName, 'some config file')
26            [CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'some config file')
27            [CompletionResult]::new('--conf', 'conf', [CompletionResultType]::ParameterName, 'some config file')
28            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
29            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
30            [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version')
31            [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
32            [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things')
33            [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
34            break
35        }
36        'my-app;test' {
37            [CompletionResult]::new('--case', 'case', [CompletionResultType]::ParameterName, 'the case to test')
38            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
39            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
40            [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version')
41            [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
42            break
43        }
44        'my-app;help' {
45            [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things')
46            [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
47            break
48        }
49        'my-app;help;test' {
50            break
51        }
52        'my-app;help;help' {
53            break
54        }
55    })
56
57    $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
58        Sort-Object -Property ListItemText
59}
60