-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Type of issue
Missing information
Feedback
Ultimately, use of --% only makes sense when calling native (external) programs.
-
Direct use of
--%with native programs is by far the most common use case. -
The only scenario in which using
--%when calling PowerShell commands makes sense is to relay a raw argument list to a native program, using splatting.- In other words: only PowerShell commands that are wrappers for external-programs can meaningfully support
--%, where anything following--%is to be passed through to the external program as if--%had been used in direct invocation of said program.
- In other words: only PowerShell commands that are wrappers for external-programs can meaningfully support
Currently:
-
about_Parsingmakes no mention of the potential use of--%for indirect, PowerShell command-mediated calls to external programs. -
about_Special_Characterscurrently uses a PowerShell command incidentally in an effort to illustrate--%'s behavior, in a manner that is likely to cause confusion (see To avoid conceptual confusion, do not use a *PowerShell function* to demonstrate how --%, the stop-parsing token, works *fundamentally* #10889).
Here's an example of a PowerShell function acting as a wrapper for a native program:
function foo {
$env:SOMEVAR = 1
# Whatever follows --% in the invocation is captured in a *single* argument, $args[1]
Write-Verbose -Verbose "`$arg[1]: «$($args[1])»"
# Note: @args is needed for --% to be relayed properly
cmd /c echo @args
$env:SOMEVAR = $null
}
# Invoke with --%
foo --% a( "x< %SOMEVAR%Output:
VERBOSE: $arg[1]: «a( "x< %SOMEVAR%»
a( "x< 1
Note:
-
From the
foofunction's perspective, whatever follows--%is passed as a single, verbatim positional argument, in$args[1], as the verbose output demonstrates. -
It is splatting that enables relaying this verbatim content to the native program, relying on splatting recognizing the array element
--%($args[0]) as the stop-parsing token.- Caveat: If that element happens to be situationally
[psobject]-wrapped, this logic does not work - see Splatting with arrays containing--%, the stop-parsing token, doesn't work if the array element containing that token happens to be[psobject]-wrapped PowerShell/PowerShell#21260.
- Caveat: If that element happens to be situationally
Page URL
Content source URL
Author
Document Id
be402df0-6f04-9a29-5410-c54be1c11926