From e75361747461c22390cdb166c1ad624071b2b830 Mon Sep 17 00:00:00 2001 From: RubixDev Date: Tue, 4 Jul 2023 14:40:22 +0800 Subject: [PATCH] fix(complete): make short options case-sensitive in PowerShell fixes #2145 --- clap_complete/src/shells/powershell.rs | 4 +++- clap_complete/tests/snapshots/aliases.ps1 | 6 +++--- clap_complete/tests/snapshots/feature_sample.ps1 | 6 +++--- clap_complete/tests/snapshots/quoting.ps1 | 2 +- clap_complete/tests/snapshots/special_commands.ps1 | 12 ++++++------ clap_complete/tests/snapshots/sub_subcommands.ps1 | 10 +++++----- clap_complete/tests/snapshots/value_hint.ps1 | 2 +- 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/clap_complete/src/shells/powershell.rs b/clap_complete/src/shells/powershell.rs index 417facf70c27..6b09b2e3ad8e 100644 --- a/clap_complete/src/shells/powershell.rs +++ b/clap_complete/src/shells/powershell.rs @@ -124,7 +124,9 @@ fn generate_aliases(completions: &mut String, preamble: &String, arg: &Arg) { for alias in aliases { let _ = write!( completions, - "{preamble}'-{alias}', '{alias}', [CompletionResultType]::ParameterName, '{tooltip}')" + "{preamble}'-{alias}', '{alias}{}', [CompletionResultType]::ParameterName, '{tooltip}')", + // make PowerShell realize there is a difference between `-s` and `-S` + if alias.is_uppercase() { " " } else { "" }, ); } } diff --git a/clap_complete/tests/snapshots/aliases.ps1 b/clap_complete/tests/snapshots/aliases.ps1 index 7bc67b6a40f1..5095c9660914 100644 --- a/clap_complete/tests/snapshots/aliases.ps1 +++ b/clap_complete/tests/snapshots/aliases.ps1 @@ -22,16 +22,16 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { $completions = @(switch ($command) { 'my-app' { [CompletionResult]::new('-o', 'o', [CompletionResultType]::ParameterName, 'cmd option') - [CompletionResult]::new('-O', 'O', [CompletionResultType]::ParameterName, 'cmd option') + [CompletionResult]::new('-O', 'O ', [CompletionResultType]::ParameterName, 'cmd option') [CompletionResult]::new('--option', 'option', [CompletionResultType]::ParameterName, 'cmd option') [CompletionResult]::new('--opt', 'opt', [CompletionResultType]::ParameterName, 'cmd option') [CompletionResult]::new('-f', 'f', [CompletionResultType]::ParameterName, 'cmd flag') - [CompletionResult]::new('-F', 'F', [CompletionResultType]::ParameterName, 'cmd flag') + [CompletionResult]::new('-F', 'F ', [CompletionResultType]::ParameterName, 'cmd flag') [CompletionResult]::new('--flag', 'flag', [CompletionResultType]::ParameterName, 'cmd flag') [CompletionResult]::new('--flg', 'flg', [CompletionResultType]::ParameterName, 'cmd flag') [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('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') break } diff --git a/clap_complete/tests/snapshots/feature_sample.ps1 b/clap_complete/tests/snapshots/feature_sample.ps1 index 6f6dc5b9ec49..4d6853ee5c0f 100644 --- a/clap_complete/tests/snapshots/feature_sample.ps1 +++ b/clap_complete/tests/snapshots/feature_sample.ps1 @@ -22,12 +22,12 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { $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('-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('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') @@ -37,7 +37,7 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { [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('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') break } diff --git a/clap_complete/tests/snapshots/quoting.ps1 b/clap_complete/tests/snapshots/quoting.ps1 index 57b6691a4268..cccbc79c5527 100644 --- a/clap_complete/tests/snapshots/quoting.ps1 +++ b/clap_complete/tests/snapshots/quoting.ps1 @@ -29,7 +29,7 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { [CompletionResult]::new('--expansions', 'expansions', [CompletionResultType]::ParameterName, 'Execute the shell command with $SHELL') [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('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('cmd-single-quotes', 'cmd-single-quotes', [CompletionResultType]::ParameterValue, 'Can be ''always'', ''auto'', or ''never''') [CompletionResult]::new('cmd-double-quotes', 'cmd-double-quotes', [CompletionResultType]::ParameterValue, 'Can be "always", "auto", or "never"') diff --git a/clap_complete/tests/snapshots/special_commands.ps1 b/clap_complete/tests/snapshots/special_commands.ps1 index f9e6a9da029e..6a16ed3a67a6 100644 --- a/clap_complete/tests/snapshots/special_commands.ps1 +++ b/clap_complete/tests/snapshots/special_commands.ps1 @@ -22,12 +22,12 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { $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('-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('-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, 'tests other things') @@ -40,7 +40,7 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { [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('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') break } @@ -48,21 +48,21 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { [CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'the other 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('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') break } 'my-app;some-cmd-with-hyphens' { [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('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') break } 'my-app;some-hidden-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('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') break } diff --git a/clap_complete/tests/snapshots/sub_subcommands.ps1 b/clap_complete/tests/snapshots/sub_subcommands.ps1 index 7256794316e4..3880c2442ae8 100644 --- a/clap_complete/tests/snapshots/sub_subcommands.ps1 +++ b/clap_complete/tests/snapshots/sub_subcommands.ps1 @@ -22,12 +22,12 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { $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('-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('-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') @@ -38,14 +38,14 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { [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('-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('-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)') @@ -55,7 +55,7 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { [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('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') break } diff --git a/clap_complete/tests/snapshots/value_hint.ps1 b/clap_complete/tests/snapshots/value_hint.ps1 index fc2bd878c7f1..b228caf3c16f 100644 --- a/clap_complete/tests/snapshots/value_hint.ps1 +++ b/clap_complete/tests/snapshots/value_hint.ps1 @@ -37,7 +37,7 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { [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('-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')