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('-o', 'o', [CompletionResultType]::ParameterName, 'cmd option')
|
2023-07-04 07:27:47 +00:00
|
|
|
[CompletionResult]::new('-O', 'O ', [CompletionResultType]::ParameterName, 'cmd option')
|
2022-03-07 20:03:46 +00:00
|
|
|
[CompletionResult]::new('--option', 'option', [CompletionResultType]::ParameterName, 'cmd option')
|
|
|
|
[CompletionResult]::new('--opt', 'opt', [CompletionResultType]::ParameterName, 'cmd option')
|
|
|
|
[CompletionResult]::new('-f', 'f', [CompletionResultType]::ParameterName, 'cmd flag')
|
2023-07-04 07:27:47 +00:00
|
|
|
[CompletionResult]::new('-F', 'F ', [CompletionResultType]::ParameterName, 'cmd flag')
|
2022-03-07 20:03:46 +00:00
|
|
|
[CompletionResult]::new('--flag', 'flag', [CompletionResultType]::ParameterName, 'cmd flag')
|
|
|
|
[CompletionResult]::new('--flg', 'flg', [CompletionResultType]::ParameterName, 'cmd flag')
|
2023-01-03 16:49:43 +00:00
|
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
|
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
2023-07-04 07:27:47 +00:00
|
|
|
[CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version')
|
2023-01-03 16:49:43 +00:00
|
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
|
2022-03-07 20:03:46 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
|
|
Sort-Object -Property ListItemText
|
|
|
|
}
|