mirror of
https://github.com/clap-rs/clap
synced 2025-01-18 23:53:54 +00:00
0d0be51606
* Copy hide flag * Revert global args special handling. Another commit will address the issue of whether global args should be included in help subtrees.
57 lines
2.7 KiB
PowerShell
57 lines
2.7 KiB
PowerShell
|
|
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')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information')
|
|
[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')
|
|
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information')
|
|
break
|
|
}
|
|
'my-app;help' {
|
|
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
|
|
[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' {
|
|
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
|
|
break
|
|
}
|
|
'my-app;help;help' {
|
|
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
|
|
break
|
|
}
|
|
})
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
Sort-Object -Property ListItemText
|
|
}
|