Windows Vista Forums

Using a list to supply arguments
  1. #1


    RickB Guest

    Using a list to supply arguments

    I need to wrap an arbitrary script call with some code.

    function some-function ($a,$b) {"`$a is '$a' and `$b is '$b'"}
    function Wrapper ($fcn) {
    Some_wrapper_code
    &$fcn $args
    }
    Wrapper some-function arbitrary args

    I can't seem to figure out how to expand/explode $args
    so that $fcn gets the same value for $args that Wrapper got.

    Rather than
    $a is 'arbitrary' and $b is 'args'
    I end up with
    $a is 'arbitrary args' and $b is ''

    Since the function is arbitrary I can't know the number or type of
    parameters in advance.



      My System SpecsSystem Spec

  2. #2


    Keith Hill [MVP] Guest

    Re: Using a list to supply arguments

    "RickB" <rbielaws@xxxxxx> wrote in message
    news:c1431c21-960d-4556-a831-f1257065f48f@xxxxxx

    > I need to wrap an arbitrary script call with some code.
    >
    > function some-function ($a,$b) {"`$a is '$a' and `$b is '$b'"}
    > function Wrapper ($fcn) {
    > Some_wrapper_code
    > &$fcn $args
    > }
    > Wrapper some-function arbitrary args
    >
    > I can't seem to figure out how to expand/explode $args
    > so that $fcn gets the same value for $args that Wrapper got.
    >
    > Rather than
    > $a is 'arbitrary' and $b is 'args'
    > I end up with
    > $a is 'arbitrary args' and $b is ''
    >
    > Since the function is arbitrary I can't know the number or type of
    > parameters in advance.
    V2 CTP has a splatting operator that might help ya:

    PS> function some-function ($a,$b) {"`$a is '$a' and `$b is '$b'"}
    PS> function Wrapper ($fcn) { "Some_wrapper_code"; &$fcn @args}
    PS> Wrapper "some-function" "hello" "world"
    Some_wrapper_code
    $a is 'hello' and $b is 'world'

    --
    Keith




      My System SpecsSystem Spec

  3. #3


    Oisin (x0n) Grehan [MVP] Guest

    Re: Using a list to supply arguments

    On Mar 25, 4:56*pm, RickB <rbiel...@xxxxxx> wrote:

    > I need to wrap an arbitrary script call with some code.
    >
    > function some-function ($a,$b) {"`$a is '$a' and `$b is '$b'"}
    > function Wrapper ($fcn) {
    > * * Some_wrapper_code
    > * * &$fcn $args}
    >
    > Wrapper some-function arbitrary args
    >
    > I can't seem to figure out how to expand/explode $args
    > so that $fcn gets the same value for $args that Wrapper got.
    >
    > Rather than
    > $a is 'arbitrary' and $b is 'args'
    > I end up with
    > $a is 'arbitrary args' and $b is ''
    >
    > Since the function is arbitrary I can't know the number or type of
    > parameters in advance.
    As Keith mentioned, v2 has a splatting operator. V1 does not. However:

    function some-function ($a,$b) {"`$a is '$a' and `$b is '$b'"}

    function wrapper ($fcn) {
    # wrapper code

    $Args | % -begin {
    $exp = "$fcn " } -process {
    $exp += "$_ " } -end {
    Invoke-Expression $exp
    }
    }


    PS> wrapper some-function x y
    $a is 'x' and $b is 'y'

    yeah, its smells funny, but it works.

    - Oisin

      My System SpecsSystem Spec

  4. #4


    RickB Guest

    Re: Using a list to supply arguments

    On Mar 25, 7:00*pm, "Keith Hill [MVP]"
    <r_keith_h...@xxxxxx_no_spam_I> wrote:

    > "RickB" <rbiel...@xxxxxx> wrote in message
    >
    > news:c1431c21-960d-4556-a831-f1257065f48f@xxxxxx
    >
    >
    >
    >
    >

    > > I need to wrap an arbitrary script call with some code.
    >

    > > function some-function ($a,$b) {"`$a is '$a' and `$b is '$b'"}
    > > function Wrapper ($fcn) {
    > > * *Some_wrapper_code
    > > * *&$fcn $args
    > > }
    > > Wrapper some-function arbitrary args
    >

    > > I can't seem to figure out how to expand/explode $args
    > > so that $fcn gets the same value for $args that Wrapper got.
    >

    > > Rather than
    > > $a is 'arbitrary' and $b is 'args'
    > > I end up with
    > > $a is 'arbitrary args' and $b is ''
    >

    > > Since the function is arbitrary I can't know the number or type of
    > > parameters in advance.
    >
    > V2 CTP has a splatting operator that might help ya:
    >
    > PS> function some-function ($a,$b) {"`$a is '$a' and `$b is '$b'"}
    > PS> function Wrapper ($fcn) { "Some_wrapper_code"; &$fcn @args}
    > PS> Wrapper "some-function" "hello" "world"
    > Some_wrapper_code
    > $a is 'hello' and $b is 'world'
    >
    > --
    > Keith- Hide quoted text -
    >
    > - Show quoted text -
    I knew I'd seen it someplace but couldn't remember where.
    I tried @$args before writing so I was on the right track.
    Thanks

      My System SpecsSystem Spec

  5. #5


    RickB Guest

    Re: Using a list to supply arguments

    On Mar 25, 8:53*pm, "Oisin (x0n) Grehan [MVP]" <ois...@xxxxxx>
    wrote:

    > On Mar 25, 4:56*pm, RickB <rbiel...@xxxxxx> wrote:
    >
    >
    >
    >
    >

    > > I need to wrap an arbitrary script call with some code.
    >

    > > function some-function ($a,$b) {"`$a is '$a' and `$b is '$b'"}
    > > function Wrapper ($fcn) {
    > > * * Some_wrapper_code
    > > * * &$fcn $args}
    >

    > > Wrapper some-function arbitrary args
    >

    > > I can't seem to figure out how to expand/explode $args
    > > so that $fcn gets the same value for $args that Wrapper got.
    >

    > > Rather than
    > > $a is 'arbitrary' and $b is 'args'
    > > I end up with
    > > $a is 'arbitrary args' and $b is ''
    >

    > > Since the function is arbitrary I can't know the number or type of
    > > parameters in advance.
    >
    > As Keith mentioned, v2 has a splatting operator. V1 does not. However:
    >
    > * function some-function ($a,$b) {"`$a is '$a' and `$b is '$b'"}
    >
    > * function wrapper ($fcn) {
    > * * * # wrapper code
    >
    > * * * $Args | % -begin {
    > * * * * * $exp = "$fcn " } -process {
    > * * * * * $exp += "$_ " } -end {
    > * * * * * Invoke-Expression $exp
    > * * * }
    > * *}
    >
    > PS> wrapper some-function x y
    > $a is 'x' and $b is 'y'
    >
    > yeah, its smells funny, but it works.
    >
    > - Oisin- Hide quoted text -
    >
    > - Show quoted text -
    I have 2.0 and so I thought I wouldn't need to use this.
    It turns out that one machine I need my stuff to run on
    can't run 2.0 .. forcing me to be 1.0 compatible.

    Now I finally tried your method and discover it only works
    when the arguments are all strings. Some of my params
    are objects so they didn't make it thru.

    This finally did work though.

    $Args | % -begin {
    $exp = "$fcn "
    $cntr = 0} -process {
    set-variable ('tmp'+ ++$cntr) $_
    $exp += "`$$('tmp' + $cntr) " } -end {
    Invoke-Expression $exp}

      My System SpecsSystem Spec

Using a list to supply arguments problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Power supply help naturalphenomen Software 5 18 Sep 2009
Bad power supply?? Gene Vista music pictures video 0 19 Jul 2009
Need new power supply bobtran General Discussion 10 02 May 2009
Power Supply Vista_32bit Vista hardware & devices 11 14 Dec 2008
Getting arguments from STDIN when command line arguments are missing Audun Gjerken PowerShell 4 05 Mar 2007