mirror of
https://github.com/clap-rs/clap
synced 2024-12-15 23:32:32 +00:00
f70ebe89a7
Before we introduced actions, it required specific setups to engage with claps version and help printing. With actions making that more explicit, we don't get as much benefit from our multiple, obscure, ways of users customizing help Before - Modify existing help or version with `mut_arg` which would automatically be pushed down the command tree like `global(true)` - Create an new help or version and have it treated as if it was the built-in on (I think) - Use the same flags as built-in and have the built-in flags automatically disabled - Users could explicitly disable the built-in functionality and do what they want Now - `mut_arg` no longer works as we define help and version flags at the end - If someone defines a flag that overlaps with the built-ins by id, long, or short, a debug assert will tell them to explicitly disable the built-in - Any customization has to be done by a user providing their own. To propagate through the command tree, they need to set `global(true)`. Benefits - Hopefully, this makes it less confusing on how to override help behavior. Someone creates an arg and we then tell them how to disable the built-in - This greatly simplifies the arg handling by pushing more responsibility onto the developer in what are hopefully just corner cases - This removes about 1Kb from .text Fixes #3405 Fixes #4033
52 lines
3.1 KiB
PowerShell
52 lines
3.1 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('--choice', 'choice', [CompletionResultType]::ParameterName, 'choice')
|
|
[CompletionResult]::new('--unknown', 'unknown', [CompletionResultType]::ParameterName, 'unknown')
|
|
[CompletionResult]::new('--other', 'other', [CompletionResultType]::ParameterName, 'other')
|
|
[CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'p')
|
|
[CompletionResult]::new('--path', 'path', [CompletionResultType]::ParameterName, 'path')
|
|
[CompletionResult]::new('-f', 'f', [CompletionResultType]::ParameterName, 'f')
|
|
[CompletionResult]::new('--file', 'file', [CompletionResultType]::ParameterName, 'file')
|
|
[CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'd')
|
|
[CompletionResult]::new('--dir', 'dir', [CompletionResultType]::ParameterName, 'dir')
|
|
[CompletionResult]::new('-e', 'e', [CompletionResultType]::ParameterName, 'e')
|
|
[CompletionResult]::new('--exe', 'exe', [CompletionResultType]::ParameterName, 'exe')
|
|
[CompletionResult]::new('--cmd-name', 'cmd-name', [CompletionResultType]::ParameterName, 'cmd-name')
|
|
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c')
|
|
[CompletionResult]::new('--cmd', 'cmd', [CompletionResultType]::ParameterName, 'cmd')
|
|
[CompletionResult]::new('-u', 'u', [CompletionResultType]::ParameterName, 'u')
|
|
[CompletionResult]::new('--user', 'user', [CompletionResultType]::ParameterName, 'user')
|
|
[CompletionResult]::new('-H', 'H', [CompletionResultType]::ParameterName, 'H')
|
|
[CompletionResult]::new('--host', 'host', [CompletionResultType]::ParameterName, 'host')
|
|
[CompletionResult]::new('--url', 'url', [CompletionResultType]::ParameterName, 'url')
|
|
[CompletionResult]::new('--email', 'email', [CompletionResultType]::ParameterName, 'email')
|
|
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information')
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information')
|
|
break
|
|
}
|
|
})
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
Sort-Object -Property ListItemText
|
|
}
|