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('-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('some_cmd', 'some_cmd', [CompletionResultType]::ParameterValue, 'tests other things') 34 [CompletionResult]::new('some-cmd-with-hyphens', 'some-cmd-with-hyphens', [CompletionResultType]::ParameterValue, 'some-cmd-with-hyphens') 35 [CompletionResult]::new('some-hidden-cmd', 'some-hidden-cmd', [CompletionResultType]::ParameterValue, 'some-hidden-cmd') 36 [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') 37 break 38 } 39 'my-app;test' { 40 [CompletionResult]::new('--case', 'case', [CompletionResultType]::ParameterName, 'the case to test') 41 [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 42 [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 43 [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version') 44 [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') 45 break 46 } 47 'my-app;some_cmd' { 48 [CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'the other case to test') 49 [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 50 [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 51 [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version') 52 [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') 53 break 54 } 55 'my-app;some-cmd-with-hyphens' { 56 [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 57 [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 58 [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version') 59 [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') 60 break 61 } 62 'my-app;some-hidden-cmd' { 63 [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 64 [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 65 [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version') 66 [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') 67 break 68 } 69 'my-app;help' { 70 [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things') 71 [CompletionResult]::new('some_cmd', 'some_cmd', [CompletionResultType]::ParameterValue, 'tests other things') 72 [CompletionResult]::new('some-cmd-with-hyphens', 'some-cmd-with-hyphens', [CompletionResultType]::ParameterValue, 'some-cmd-with-hyphens') 73 [CompletionResult]::new('some-hidden-cmd', 'some-hidden-cmd', [CompletionResultType]::ParameterValue, 'some-hidden-cmd') 74 [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') 75 break 76 } 77 'my-app;help;test' { 78 break 79 } 80 'my-app;help;some_cmd' { 81 break 82 } 83 'my-app;help;some-cmd-with-hyphens' { 84 break 85 } 86 'my-app;help;some-hidden-cmd' { 87 break 88 } 89 'my-app;help;help' { 90 break 91 } 92 }) 93 94 $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | 95 Sort-Object -Property ListItemText 96 } 97