From 1e04310b653618a8d5724518c682a56b86ad9595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Old=C5=99ich=20Jedli=C4=8Dka?= Date: Fri, 14 Apr 2023 08:06:00 +0100 Subject: [PATCH] Add support for PowerShell 7.2+ PowerShell 7.2 has changed the way arguments are passed to executables. This was originally an experimental feature in 7.2, but as of 7.3 it is built-in. A simple "" is now sufficient for passing empty arguments, no back-tick escaping is required. Fixes #1849 Merge https://github.com/spf13/cobra/pull/1916 --- templates/completion.pwsh.gotmpl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/templates/completion.pwsh.gotmpl b/templates/completion.pwsh.gotmpl index 681d6fa..6c6dfab 100644 --- a/templates/completion.pwsh.gotmpl +++ b/templates/completion.pwsh.gotmpl @@ -69,7 +69,16 @@ Register-ArgumentCompleter -CommandName '{{ .CMDVarName }}' -ScriptBlock { # If the last parameter is complete (there is a space following it) # We add an extra empty parameter so we can indicate this to the go method. __{{ .CMDVarName }}_debug "Adding extra empty parameter" - $RequestComp="$RequestComp" + ' `"`"' + # PowerShell 7.2+ changed the way how the arguments are passed to executables, + # so for pre-7.2 or when Legacy argument passing is enabled we need to use + if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or + ($PSVersionTable.PsVersion -lt [version]'7.3.0' -and -not [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -or + (($PSVersionTable.PsVersion -ge [version]'7.3.0' -or [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -and + $PSNativeCommandArgumentPassing -eq 'Legacy')) { + $RequestComp="$RequestComp" + ' `"`"' + } else { + $RequestComp="$RequestComp" + ' ""' + } } __{{ .CMDVarName }}_debug "Calling $RequestComp"