2022-03-07 20:03:46 +00:00
|
|
|
|
|
|
|
using namespace System.Management.Automation
|
|
|
|
using namespace System.Management.Automation.Language
|
|
|
|
|
|
|
|
Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock {
|
|
|
|
param($wordToComplete, $commandAst, $cursorPosition)
|
|
|
|
|
|
|
|
$commandElements = $commandAst.CommandElements
|
|
|
|
$command = @(
|
|
|
|
'my-app'
|
|
|
|
for ($i = 1; $i -lt $commandElements.Count; $i++) {
|
|
|
|
$element = $commandElements[$i]
|
|
|
|
if ($element -isnot [StringConstantExpressionAst] -or
|
|
|
|
$element.StringConstantType -ne [StringConstantType]::BareWord -or
|
|
|
|
$element.Value.StartsWith('-') -or
|
|
|
|
$element.Value -eq $wordToComplete) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
$element.Value
|
|
|
|
}) -join ';'
|
|
|
|
|
|
|
|
$completions = @(switch ($command) {
|
|
|
|
'my-app' {
|
|
|
|
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
|
|
|
|
[CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'v')
|
2023-01-03 16:49:43 +00:00
|
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
|
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
2022-03-07 20:03:46 +00:00
|
|
|
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand')
|
|
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
'my-app;test' {
|
|
|
|
[CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'd')
|
2022-08-08 21:08:47 +00:00
|
|
|
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
|
2023-01-03 16:49:43 +00:00
|
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
|
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
2022-03-07 20:03:46 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
'my-app;help' {
|
2022-08-25 05:44:07 +00:00
|
|
|
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand')
|
|
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
'my-app;help;test' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
'my-app;help;help' {
|
2022-03-07 20:03:46 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
|
|
Sort-Object -Property ListItemText
|
|
|
|
}
|