2023-01-10 08:14:52 +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' {
|
2024-07-31 10:56:12 +00:00
|
|
|
[CompletionResult]::new('-h', '-h', [CompletionResultType]::ParameterName, 'Print help')
|
|
|
|
[CompletionResult]::new('--help', '--help', [CompletionResultType]::ParameterName, 'Print help')
|
2023-01-10 08:14:52 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
|
|
Sort-Object -Property ListItemText
|
|
|
|
}
|