mirror of
https://github.com/clap-rs/clap
synced 2024-12-15 07:12:32 +00:00
5000d58f38
Let's generate autocompletions for aliased subcommands. $ source before.zsh $ clap-test [TAB] <- gives me "foo bar --" $ clap-test foo [TAB] <- gives me "--my-flag" $ clap-test bar [TAB] <- no reaction $ source after.zsh $ clap-test [TAB] <- gives me "foo bar --" $ clap-test foo [TAB] <- gives me "--my-flag" $ clap-test bar [TAB] <- gives me "--my-flag"
125 lines
7.4 KiB
PowerShell
125 lines
7.4 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, 'some config file')
|
|
[CompletionResult]::new('-C', 'C ', [CompletionResultType]::ParameterName, 'some config file')
|
|
[CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'some config file')
|
|
[CompletionResult]::new('--conf', 'conf', [CompletionResultType]::ParameterName, 'some config file')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
|
|
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things')
|
|
[CompletionResult]::new('some_cmd', 'some_cmd', [CompletionResultType]::ParameterValue, 'top level subcommand')
|
|
[CompletionResult]::new('some_cmd_alias', 'some_cmd_alias', [CompletionResultType]::ParameterValue, 'top level subcommand')
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
|
break
|
|
}
|
|
'my-app;test' {
|
|
[CompletionResult]::new('--case', 'case', [CompletionResultType]::ParameterName, 'the case to test')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
|
|
break
|
|
}
|
|
'my-app;some_cmd' {
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
|
|
[CompletionResult]::new('sub_cmd', 'sub_cmd', [CompletionResultType]::ParameterValue, 'sub-subcommand')
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
|
break
|
|
}
|
|
'my-app;some_cmd_alias' {
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
|
[CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
|
|
[CompletionResult]::new('sub_cmd', 'sub_cmd', [CompletionResultType]::ParameterValue, 'sub-subcommand')
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
|
break
|
|
}
|
|
'my-app;some_cmd;sub_cmd' {
|
|
[CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'the other case to test')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
|
|
[CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
|
|
break
|
|
}
|
|
'my-app;some_cmd_alias;sub_cmd' {
|
|
[CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'the other case to test')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
|
|
[CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version')
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
|
|
break
|
|
}
|
|
'my-app;some_cmd;help' {
|
|
[CompletionResult]::new('sub_cmd', 'sub_cmd', [CompletionResultType]::ParameterValue, 'sub-subcommand')
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
|
break
|
|
}
|
|
'my-app;some_cmd;help;sub_cmd' {
|
|
break
|
|
}
|
|
'my-app;some_cmd;help;help' {
|
|
break
|
|
}
|
|
'my-app;some_cmd_alias;help' {
|
|
[CompletionResult]::new('sub_cmd', 'sub_cmd', [CompletionResultType]::ParameterValue, 'sub-subcommand')
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
|
break
|
|
}
|
|
'my-app;some_cmd_alias;help;sub_cmd' {
|
|
break
|
|
}
|
|
'my-app;some_cmd_alias;help;help' {
|
|
break
|
|
}
|
|
'my-app;help' {
|
|
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things')
|
|
[CompletionResult]::new('some_cmd', 'some_cmd', [CompletionResultType]::ParameterValue, 'top level subcommand')
|
|
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
|
|
break
|
|
}
|
|
'my-app;help;test' {
|
|
break
|
|
}
|
|
'my-app;help;some_cmd' {
|
|
[CompletionResult]::new('sub_cmd', 'sub_cmd', [CompletionResultType]::ParameterValue, 'sub-subcommand')
|
|
break
|
|
}
|
|
'my-app;help;some_cmd;sub_cmd' {
|
|
break
|
|
}
|
|
'my-app;help;help' {
|
|
break
|
|
}
|
|
})
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
Sort-Object -Property ListItemText
|
|
}
|