[MSH] $args bug

Discussion in 'Scripting' started by Keith Hill [MVP], Jan 12, 2006.

  1. This doesn't seem right to me:

    MSH:48 # function huh { write-host "args[0] is $args[0]" }
    MSH:49 # huh 8.0
    args[0] is 8[0]

    and has in fact broken my "CD" replacement function.
     
    Keith Hill [MVP], Jan 12, 2006
    #1
    1. Advertisements

  2. MSH>function huh { write-host "args[0] is $($args[0])" }
    MSH>huh 8.0
    args[0] is 8

    hmm, also not what I expected

    gr /\/\o\/\/
     
    /\\/\\o\\/\\/ [MVP], Jan 12, 2006
    #2
    1. Advertisements

  3. MSH>function huh { write-host "args[0] is $args" }
    MSH>huh 8.0
    args[0] is 8

    no, same as before
     
    /\\/\\o\\/\\/ [MVP], Jan 12, 2006
    #3
  4. MSH>function huh { write-host "args[0] is $args" }
    function huh { write-host "type of args is $($args.GetType().Name)`ntype
    of args[0] is $($args[0].GetType().Name)" }

    MSH>huh 8.0
    type of args is Object[]
    type of args[0] is Double

    Beta 3 evaluates numeric arguments

    Neville.
     
    Neville Bagnall, Jan 12, 2006
    #4
  5. function huh {param([string]$first) write-host "type of first is
    $($first.GetType().Name)`nvalue of first is $first" }

    MSH>huh 8.0
    type of first is String
    value of first is 8.0

    Neville.
     
    Neville Bagnall, Jan 12, 2006
    #5
  6. Yeah that is what is happening. 8.0 is converted to a numeric (double) and
    then when you try to use it as a string you get the default formatter
    behavior for doubles (I guess) which doesn't display trailing zeros.

    Just out of curiousity, what problem did this change in behavior fix? This
    is going to make it a bit of a pain to specify directories with names like
    8.0 (->8), 9.0 (->9), 10.5020 (->10.502), 11.500 (->11.5), etc. For
    example:
    Directory: Microsoft.Management.Automation.Core\FileSystem::C:\Temp


    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    d---- 1/12/2006 10:42 PM 8

    Doh! That created a directory called 8 instead of 8.0. Better hope the user
    notices that MSH didn't do quite what they intended. :-(
     
    Keith Hill [MVP], Jan 13, 2006
    #6
  7. mkdir 8.0
    I don't know about mkdir (I zap all the legacy aliases) but new-item -type
    directory 8.0 works.
    This is because the expected parameter type is string.
    Functions and scripts can get the same behaviour using param([type]name)

    Neville.
     
    Neville Bagnall, Jan 13, 2006
    #7
  8. This problem is in both the "md" and "mkdir" functions because they use
    $args instead of strongly typed parameters.
     
    Keith Hill [MVP], Jan 13, 2006
    #8
  9. Keith Hill [MVP]

    lee.holmes Guest

    I've filed a bug on this for you.

    --
    Lee Holmes [MSFT]
    Microsoft Command Shell Development
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.

    -----Original Message-----
    From: Keith Hill [MVP]
    Posted At: Friday, January 13, 2006 10:31 AM
    Posted To: microsoft.public.windows.server.scripting
    Conversation: [MSH] $args bug
    Subject: Re: [MSH] $args bug

    This problem is in both the "md" and "mkdir" functions because they use
    $args instead of strongly typed parameters.
     
    lee.holmes, Jan 13, 2006
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.