Skip to content

Commit

Permalink
Add support for PowerShell 7.2+
Browse files Browse the repository at this point in the history
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 spf13/cobra#1916
  • Loading branch information
Oldřich Jedlička authored and hoshsadiq committed Apr 14, 2023
1 parent 25b93fd commit 1e04310
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion templates/completion.pwsh.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 1e04310

Please sign in to comment.