mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 23:02:31 +00:00
e782775229
Found while trying to add Nushell completions to [`jj`](https://github.com/martinvonz/jj).
54 lines
2.5 KiB
PowerShell
54 lines
2.5 KiB
PowerShell
|
|
using namespace System.Management.Automation
|
|
using namespace System.Management.Automation.Language
|
|
|
|
Register-ArgumentCompleter -Native -CommandName 'bin-name' -ScriptBlock {
|
|
param($wordToComplete, $commandAst, $cursorPosition)
|
|
|
|
$commandElements = $commandAst.CommandElements
|
|
$command = @(
|
|
'bin-name'
|
|
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) {
|
|
'bin-name' {
|
|
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
|
|
[CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'v')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand with a second line')
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
|
break
|
|
}
|
|
'bin-name;test' {
|
|
[CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'd')
|
|
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
|
break
|
|
}
|
|
'bin-name;help' {
|
|
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand with a second line')
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
|
break
|
|
}
|
|
'bin-name;help;test' {
|
|
break
|
|
}
|
|
'bin-name;help;help' {
|
|
break
|
|
}
|
|
})
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
Sort-Object -Property ListItemText
|
|
}
|