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('-c', 'c', [CompletionResultType]::ParameterName, 'c')
2519625d8cSopenharmony_ci            [CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'v')
2619625d8cSopenharmony_ci            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
2719625d8cSopenharmony_ci            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
2819625d8cSopenharmony_ci            [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand')
2919625d8cSopenharmony_ci            [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
3019625d8cSopenharmony_ci            break
3119625d8cSopenharmony_ci        }
3219625d8cSopenharmony_ci        'my-app;test' {
3319625d8cSopenharmony_ci            [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'd')
3419625d8cSopenharmony_ci            [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
3519625d8cSopenharmony_ci            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
3619625d8cSopenharmony_ci            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
3719625d8cSopenharmony_ci            break
3819625d8cSopenharmony_ci        }
3919625d8cSopenharmony_ci        'my-app;help' {
4019625d8cSopenharmony_ci            [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand')
4119625d8cSopenharmony_ci            [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
4219625d8cSopenharmony_ci            break
4319625d8cSopenharmony_ci        }
4419625d8cSopenharmony_ci        'my-app;help;test' {
4519625d8cSopenharmony_ci            break
4619625d8cSopenharmony_ci        }
4719625d8cSopenharmony_ci        'my-app;help;help' {
4819625d8cSopenharmony_ci            break
4919625d8cSopenharmony_ci        }
5019625d8cSopenharmony_ci    })
5119625d8cSopenharmony_ci
5219625d8cSopenharmony_ci    $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
5319625d8cSopenharmony_ci        Sort-Object -Property ListItemText
5419625d8cSopenharmony_ci}
55