Windows Vista Forums

Command Line Argument Types

  1. #1


    John Smith Guest

    Command Line Argument Types

    I am retrieveing command line arguments passed to a PS1 script. I find that:



    a. ./myscript 5 $args[0].GetType() gives Int32
    b. ./myscript -5 $args[0].GetType() gives String
    c. ./myscript (-5) $args[0].GetType() gives Int32

    Is this how it is supposed to work?

    Thanks

      My System SpecsSystem Spec

  2. #2


    Lucvdv Guest

    Re: Command Line Argument Types

    On Wed, 20 Dec 2006 17:58:01 -0800, John Smith
    <JohnSmith@discussions.microsoft.com> wrote:

    > I am retrieveing command line arguments passed to a PS1 script. I find that:
    >
    > a. ./myscript 5 $args[0].GetType() gives Int32
    > b. ./myscript -5 $args[0].GetType() gives String
    > c. ./myscript (-5) $args[0].GetType() gives Int32
    >
    > Is this how it is supposed to work?


    Interesting, I didn't know that.

    It's the same with functions, but I suppose most people would use the
    parentheses there out of habit and never notice it:

    function testit($arg){ $arg.GetType() }


    testit 5

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Int32 System.ValueType


    testit -5

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True String System.Object


    testit (-5)

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Int32 System.ValueType


      My System SpecsSystem Spec

  3. #3


    Jacques Barathon [MS] Guest

    Re: Command Line Argument Types

    "Lucvdv" <replace_name@null.net> wrote in message
    news:h56lo2h8vqnfe9tfs9nh0jgbv95ns967lk@4ax.com...
    <...>
    > It's the same with functions, but I suppose most people would use the
    > parentheses there out of habit and never notice it:


    Not a good habit. Be careful with parentheses when you pass arguments to
    functions. Parentheses mean that you are passing everything they enclose as
    *one* argument that will be evaluated as an expression:

    PS> function testarg {
    >> "`$args[0]: $($args[0])"
    >> "`$args[1]: $($args[1])"
    >> }
    >>

    PS> testarg (1,5)
    $args[0]: 1 5
    $args[1]:
    PS> testarg (1 5)
    Unexpected token '5' in expression or statement.
    At line:1 char:13
    + testarg (1 5) <<<<
    PS> testarg 1 5
    $args[0]: 1
    $args[1]: 5

    Jacques


      My System SpecsSystem Spec

  4. #4


    Keith Hill [MVP] Guest

    Re: Command Line Argument Types

    "Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
    news:eK7YNUUJHHA.3668@TK2MSFTNGP02.phx.gbl...
    > "Lucvdv" <replace_name@null.net> wrote in message
    > news:h56lo2h8vqnfe9tfs9nh0jgbv95ns967lk@4ax.com...
    > <...>
    >> It's the same with functions, but I suppose most people would use the
    >> parentheses there out of habit and never notice it:

    >
    > Not a good habit. Be careful with parentheses when you pass arguments to
    > functions. Parentheses mean that you are passing everything they enclose
    > as *one* argument that will be evaluated as an expression:


    I *still* get bit by that one from time to time. It might be easier to
    remember not to use the parens on the call if it weren't for the fact that
    when calling .NET methods you have to use parens (well at least the call
    works as expected if you do).

    --
    Keith



      My System SpecsSystem Spec

  5. #5


    Jacques Barathon [MS] Guest

    Re: Command Line Argument Types

    "Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospam> wrote in message
    news:uU750zdJHHA.4760@TK2MSFTNGP03.phx.gbl...
    <...>
    > I *still* get bit by that one from time to time. It might be easier to
    > remember not to use the parens on the call if it weren't for the fact that
    > when calling .NET methods you have to use parens (well at least the call
    > works as expected if you do).


    Calling any method of any object always requires parens:

    "hello".replace "h","c" >> won't work

    Jacques


      My System SpecsSystem Spec

Command Line Argument Types

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Open notepad with a command-line argument Daz VB Script 3 19 May 2008
Variable arguments and argument types Chris Warwick PowerShell 8 28 May 2007
Re: Variable arguments and argument types klumsy@xtra.co.nz PowerShell 0 23 May 2007
Re: Variable arguments and argument types klumsy@xtra.co.nz PowerShell 0 23 May 2007
Re: Variable arguments and argument types klumsy@xtra.co.nz PowerShell 0 23 May 2007