119625d8cSopenharmony_ci 219625d8cSopenharmony_ciusing namespace System.Management.Automation 319625d8cSopenharmony_ciusing namespace System.Management.Automation.Language 419625d8cSopenharmony_ci 519625d8cSopenharmony_ciRegister-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { 619625d8cSopenharmony_ci param($wordToComplete, $commandAst, $cursorPosition) 719625d8cSopenharmony_ci 819625d8cSopenharmony_ci $commandElements = $commandAst.CommandElements 919625d8cSopenharmony_ci $command = @( 1019625d8cSopenharmony_ci 'my-app' 1119625d8cSopenharmony_ci for ($i = 1; $i -lt $commandElements.Count; $i++) { 1219625d8cSopenharmony_ci $element = $commandElements[$i] 1319625d8cSopenharmony_ci if ($element -isnot [StringConstantExpressionAst] -or 1419625d8cSopenharmony_ci $element.StringConstantType -ne [StringConstantType]::BareWord -or 1519625d8cSopenharmony_ci $element.Value.StartsWith('-') -or 1619625d8cSopenharmony_ci $element.Value -eq $wordToComplete) { 1719625d8cSopenharmony_ci break 1819625d8cSopenharmony_ci } 1919625d8cSopenharmony_ci $element.Value 2019625d8cSopenharmony_ci }) -join ';' 2119625d8cSopenharmony_ci 2219625d8cSopenharmony_ci $completions = @(switch ($command) { 2319625d8cSopenharmony_ci 'my-app' { 2419625d8cSopenharmony_ci [CompletionResult]::new('--single-quotes', 'single-quotes', [CompletionResultType]::ParameterName, 'Can be ''always'', ''auto'', or ''never''') 2519625d8cSopenharmony_ci [CompletionResult]::new('--double-quotes', 'double-quotes', [CompletionResultType]::ParameterName, 'Can be "always", "auto", or "never"') 2619625d8cSopenharmony_ci [CompletionResult]::new('--backticks', 'backticks', [CompletionResultType]::ParameterName, 'For more information see `echo test`') 2719625d8cSopenharmony_ci [CompletionResult]::new('--backslash', 'backslash', [CompletionResultType]::ParameterName, 'Avoid ''\n''') 2819625d8cSopenharmony_ci [CompletionResult]::new('--brackets', 'brackets', [CompletionResultType]::ParameterName, 'List packages [filter]') 2919625d8cSopenharmony_ci [CompletionResult]::new('--expansions', 'expansions', [CompletionResultType]::ParameterName, 'Execute the shell command with $SHELL') 3019625d8cSopenharmony_ci [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 3119625d8cSopenharmony_ci [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 3219625d8cSopenharmony_ci [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version') 3319625d8cSopenharmony_ci [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') 3419625d8cSopenharmony_ci [CompletionResult]::new('cmd-single-quotes', 'cmd-single-quotes', [CompletionResultType]::ParameterValue, 'Can be ''always'', ''auto'', or ''never''') 3519625d8cSopenharmony_ci [CompletionResult]::new('cmd-double-quotes', 'cmd-double-quotes', [CompletionResultType]::ParameterValue, 'Can be "always", "auto", or "never"') 3619625d8cSopenharmony_ci [CompletionResult]::new('cmd-backticks', 'cmd-backticks', [CompletionResultType]::ParameterValue, 'For more information see `echo test`') 3719625d8cSopenharmony_ci [CompletionResult]::new('cmd-backslash', 'cmd-backslash', [CompletionResultType]::ParameterValue, 'Avoid ''\n''') 3819625d8cSopenharmony_ci [CompletionResult]::new('cmd-brackets', 'cmd-brackets', [CompletionResultType]::ParameterValue, 'List packages [filter]') 3919625d8cSopenharmony_ci [CompletionResult]::new('cmd-expansions', 'cmd-expansions', [CompletionResultType]::ParameterValue, 'Execute the shell command with $SHELL') 4019625d8cSopenharmony_ci [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') 4119625d8cSopenharmony_ci break 4219625d8cSopenharmony_ci } 4319625d8cSopenharmony_ci 'my-app;cmd-single-quotes' { 4419625d8cSopenharmony_ci [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 4519625d8cSopenharmony_ci [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 4619625d8cSopenharmony_ci break 4719625d8cSopenharmony_ci } 4819625d8cSopenharmony_ci 'my-app;cmd-double-quotes' { 4919625d8cSopenharmony_ci [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 5019625d8cSopenharmony_ci [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 5119625d8cSopenharmony_ci break 5219625d8cSopenharmony_ci } 5319625d8cSopenharmony_ci 'my-app;cmd-backticks' { 5419625d8cSopenharmony_ci [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 5519625d8cSopenharmony_ci [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 5619625d8cSopenharmony_ci break 5719625d8cSopenharmony_ci } 5819625d8cSopenharmony_ci 'my-app;cmd-backslash' { 5919625d8cSopenharmony_ci [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 6019625d8cSopenharmony_ci [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 6119625d8cSopenharmony_ci break 6219625d8cSopenharmony_ci } 6319625d8cSopenharmony_ci 'my-app;cmd-brackets' { 6419625d8cSopenharmony_ci [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 6519625d8cSopenharmony_ci [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 6619625d8cSopenharmony_ci break 6719625d8cSopenharmony_ci } 6819625d8cSopenharmony_ci 'my-app;cmd-expansions' { 6919625d8cSopenharmony_ci [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') 7019625d8cSopenharmony_ci [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') 7119625d8cSopenharmony_ci break 7219625d8cSopenharmony_ci } 7319625d8cSopenharmony_ci 'my-app;help' { 7419625d8cSopenharmony_ci [CompletionResult]::new('cmd-single-quotes', 'cmd-single-quotes', [CompletionResultType]::ParameterValue, 'Can be ''always'', ''auto'', or ''never''') 7519625d8cSopenharmony_ci [CompletionResult]::new('cmd-double-quotes', 'cmd-double-quotes', [CompletionResultType]::ParameterValue, 'Can be "always", "auto", or "never"') 7619625d8cSopenharmony_ci [CompletionResult]::new('cmd-backticks', 'cmd-backticks', [CompletionResultType]::ParameterValue, 'For more information see `echo test`') 7719625d8cSopenharmony_ci [CompletionResult]::new('cmd-backslash', 'cmd-backslash', [CompletionResultType]::ParameterValue, 'Avoid ''\n''') 7819625d8cSopenharmony_ci [CompletionResult]::new('cmd-brackets', 'cmd-brackets', [CompletionResultType]::ParameterValue, 'List packages [filter]') 7919625d8cSopenharmony_ci [CompletionResult]::new('cmd-expansions', 'cmd-expansions', [CompletionResultType]::ParameterValue, 'Execute the shell command with $SHELL') 8019625d8cSopenharmony_ci [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') 8119625d8cSopenharmony_ci break 8219625d8cSopenharmony_ci } 8319625d8cSopenharmony_ci 'my-app;help;cmd-single-quotes' { 8419625d8cSopenharmony_ci break 8519625d8cSopenharmony_ci } 8619625d8cSopenharmony_ci 'my-app;help;cmd-double-quotes' { 8719625d8cSopenharmony_ci break 8819625d8cSopenharmony_ci } 8919625d8cSopenharmony_ci 'my-app;help;cmd-backticks' { 9019625d8cSopenharmony_ci break 9119625d8cSopenharmony_ci } 9219625d8cSopenharmony_ci 'my-app;help;cmd-backslash' { 9319625d8cSopenharmony_ci break 9419625d8cSopenharmony_ci } 9519625d8cSopenharmony_ci 'my-app;help;cmd-brackets' { 9619625d8cSopenharmony_ci break 9719625d8cSopenharmony_ci } 9819625d8cSopenharmony_ci 'my-app;help;cmd-expansions' { 9919625d8cSopenharmony_ci break 10019625d8cSopenharmony_ci } 10119625d8cSopenharmony_ci 'my-app;help;help' { 10219625d8cSopenharmony_ci break 10319625d8cSopenharmony_ci } 10419625d8cSopenharmony_ci }) 10519625d8cSopenharmony_ci 10619625d8cSopenharmony_ci $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | 10719625d8cSopenharmony_ci Sort-Object -Property ListItemText 10819625d8cSopenharmony_ci} 109