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('--choice', 'choice', [CompletionResultType]::ParameterName, 'choice') 25 [CompletionResult]::new('--unknown', 'unknown', [CompletionResultType]::ParameterName, 'unknown') 26 [CompletionResult]::new('--other', 'other', [CompletionResultType]::ParameterName, 'other') 27 [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'p') 28 [CompletionResult]::new('--path', 'path', [CompletionResultType]::ParameterName, 'path') 29 [CompletionResult]::new('-f', 'f', [CompletionResultType]::ParameterName, 'f') 30 [CompletionResult]::new('--file', 'file', [CompletionResultType]::ParameterName, 'file') 31 [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'd') 32 [CompletionResult]::new('--dir', 'dir', [CompletionResultType]::ParameterName, 'dir') 33 [CompletionResult]::new('-e', 'e', [CompletionResultType]::ParameterName, 'e') 34 [CompletionResult]::new('--exe', 'exe', [CompletionResultType]::ParameterName, 'exe') 35 [CompletionResult]::new('--cmd-name', 'cmd-name', [CompletionResultType]::ParameterName, 'cmd-name') 36 [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c') 37 [CompletionResult]::new('--cmd', 'cmd', [CompletionResultType]::ParameterName, 'cmd') 38 [CompletionResult]::new('-u', 'u', [CompletionResultType]::ParameterName, 'u') 39 [CompletionResult]::new('--user', 'user', [CompletionResultType]::ParameterName, 'user') 40 [CompletionResult]::new('-H', 'H', [CompletionResultType]::ParameterName, 'H') 41 [CompletionResult]::new('--host', 'host', [CompletionResultType]::ParameterName, 'host') 42 [CompletionResult]::new('--url', 'url', [CompletionResultType]::ParameterName, 'url') 43 [CompletionResult]::new('--email', 'email', [CompletionResultType]::ParameterName, 'email') 44 [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 45 [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 46 break 47 } 48 }) 49 50 $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | 51 Sort-Object -Property ListItemText 52 } 53