Jeff,
Thanks for the post.
VR
"Jeff" <jeff.hillman@gmail.com> wrote in message
news:1185523190.063165.182320@k79g2000hse.googlegroups.com...
> On Jul 28, 1:24 pm, <VR...@newsgroup.nospam> wrote:
>> Please help!
>>
>> So, I have this function X, which among other parameters accepts a
>> switch.
>> It also calls another function, say Y, with exactly same set of
>> parameters/switches. The trick is that each one of those functions
>> ensures
>> there are no additional arguments passed to it.
>>
>> So, calling X is easy: it's either X or X -mySwitch
>> The question is how to call Y from X, in such way so that if mySwitch is
>> true, it gets passed to Y, if false it doesn't.
>>
>> I am looking to do it in one line, since there another 5-10 parameters
>> and
>> if I use an 'if' clause, things get messy.
>>
>> So, what I am looking for is the replacement for this:
>> if ($mySwitch) {Y -mySwitch} else {Y}
>>
>> Thanks in advance for any help.
>> VR
>>
>> Example:
>>
>> function X([switch] $mySwitch)
>> {
>> if ($args.Count -ne 0) {throw "no additional arguments allowed";}
>>
>> # do whatever ...
>>
>> # the line below is incorrect and causes the problem !!!!
>> # what is the correct way of passing that switch on, when the local
>> $mySwitch is true and not to pass it, when it's false?
>> Y -mySwitch $mySwitch }
>>
>> function Y([switch] $mySwitch)
>> {
>> # the line below will throw, since $args.Count is 1
>> if ($args.Count -ne 0) {throw "no additional arguments allowed";}
>>
>> # do whatever ...
>>
>> }
>>
>> # call X
>> X -mySwitch
>
> Depending on the other parameters, this might get you what you want:
>
> Invoke-Expression ($MyInvocation.Line -replace "X", "Y")
>
> This calls Y in exactly the same way X was called. As long as Y is in
> the same scope as X, any non-switch parameters passed to X should be
> evaluated accurately in the call to Y.
>
> Good luck.
>
> Jeff
>