mirror of
https://github.com/clap-rs/clap
synced 2024-12-15 23:32:32 +00:00
61 lines
2.5 KiB
PowerShell
61 lines
2.5 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('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
||
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
||
|
[CompletionResult]::new('foo', 'foo', [CompletionResultType]::ParameterValue, 'foo')
|
||
|
[CompletionResult]::new('bar', 'bar', [CompletionResultType]::ParameterValue, 'bar')
|
||
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
||
|
break
|
||
|
}
|
||
|
'my-app;foo' {
|
||
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
||
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
||
|
break
|
||
|
}
|
||
|
'my-app;bar' {
|
||
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
||
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
||
|
break
|
||
|
}
|
||
|
'my-app;help' {
|
||
|
[CompletionResult]::new('foo', 'foo', [CompletionResultType]::ParameterValue, 'foo')
|
||
|
[CompletionResult]::new('bar', 'bar', [CompletionResultType]::ParameterValue, 'bar')
|
||
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
||
|
break
|
||
|
}
|
||
|
'my-app;help;foo' {
|
||
|
break
|
||
|
}
|
||
|
'my-app;help;bar' {
|
||
|
break
|
||
|
}
|
||
|
'my-app;help;help' {
|
||
|
break
|
||
|
}
|
||
|
})
|
||
|
|
||
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
||
|
Sort-Object -Property ListItemText
|
||
|
}
|