I want to wrap a function, that is to create a new function such that it would automatically pass some arguments to the old function, like Python's partial functions. The arguments passed are the ones defined in the callee and not the caller. The important thing is that I don't want to refer to each of them explicitly (define them twice).
That is really done to save typing-in the same flags to complicated functions while allowing customization.
For example, in Python, I would do:
call_with_x=partial(call,x=1)
Or maybe use **kw and pass it to the callee in some cases.
This is my best try (based on Wrapper function in PowerShell: Pass remaining parameters):
function Let
{
[CmdletBinding()]
Param([parameter(mandatory=$true, position=0)][string]$Option,
[parameter(mandatory=$false, position=1, ValueFromRemainingArguments=$true)]$Remaining)
Get @Remaining
}
function Get
{
[CmdletBinding()]
Param([parameter(mandatory=$false, position=0)][string]$OptionA,
[parameter(mandatory=$true, position=1)][string]$OptionB)
Write-Host $OptionA, $OptionB
}
But Let -Option c -OptionA 1
Prints -OptionA 1
which is obviously not what I intended.
-OptionA 1cause you're only splatting that parameter. Maybe I'm not understanding what you're asking for, but you can splat all the parameters using@PSBoundParameters. The only issue would be that the "callee" function would have to have the same parameters as the caller function.PSBoundParametersseems like a good direction.$Remainingas[hashtable]without a need forValueFromRemainingArgumentsthenGet @Remainingwould work without problems as long as the hashtable being passed as argument has Keys matching the parameters formGet. This would also mean when callingletyou're actually building a hash with the parameters as Keys...DynamicParamidea is not bad either, you could build the remaining parameters at runtime by reading the AST ofGet(untested but I think that should work)(Get-Command Get).Parametersgives you all the parameters without the need for the AST.