Windows Vista Forums

passing [switch] parameters

  1. #1


    Jeff Guest

    Re: passing [switch] parameters

    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


      My System SpecsSystem Spec

  2. #2


    Jacques Barathon [MS] Guest

    Re: passing [switch] parameters

    <VRSki@newsgroup.nospam> wrote in message
    news:uIy9OaB0HHA.1168@TK2MSFTNGP02.phx.gbl...
    > 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.


    Switch parameters were meant to make it very simple:

    function X ([switch]$mySwitch) {
    Y -mySwitch:$mySwitch
    }

    Hope that helps,
    Jacques


      My System SpecsSystem Spec

  3. #3


    Guest

    passing [switch] parameters

    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



      My System SpecsSystem Spec

  4. #4


    Guest

    Re: passing [switch] parameters

    Of course! I am an iditiot -- I completely forgot about the good old colon!
    Thanks Jacues.


    "Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
    news:uZMcJtC0HHA.5772@TK2MSFTNGP02.phx.gbl...
    > <VRSki@newsgroup.nospam> wrote in message
    > news:uIy9OaB0HHA.1168@TK2MSFTNGP02.phx.gbl...
    >> 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.

    >
    > Switch parameters were meant to make it very simple:
    >
    > function X ([switch]$mySwitch) {
    > Y -mySwitch:$mySwitch
    > }
    >
    > Hope that helps,
    > Jacques




      My System SpecsSystem Spec

  5. #5


    Guest

    Re: passing [switch] parameters

    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
    >




      My System SpecsSystem Spec

passing [switch] parameters

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Parameters Between Subroutines John Quinn VB Script 2 23 Apr 2010
Passing Parameters to Ps1 files james PowerShell 2 11 Jan 2010
Passing parameters to a PS script Dan PowerShell 11 11 Nov 2009
Help with foreach-object and passing parameters Xanbar PowerShell 1 15 Nov 2006
passing parameters to script (ps1) IT Staff PowerShell 1 19 Oct 2006