[msh] $args.lenght or $args.Count and other $args oddities.

Discussion in 'Scripting' started by /\\/\\o\\/\\/, Nov 12, 2005.

  1. I'm a bit confused about $parms behavour :

    Lenght works in the If but not later, as Count does work in both cases.

    see the test function :

    MSH>function lol {$Args.lenght ; if($args.length -ne 0){"not0";"L $args
    - $($Args.lenght)";"C $args - $($Args.Count)"}}
    MSH>lol a1
    not0
    L a1 -
    C a1 - 1
    MSH>lol a1 a2
    not0
    L a1 a2 -
    C a1 a2 - 2

    I think this as strange as Count is an alias to Lenght ( see Second
    Example of the Get-Method below sig.)

    after a tip from JS earlyer in the NG about use of the , I could find
    the Properties (I also find this confusing (see 2 examples below):

    Also 1 or more $args does not realy matter here (as it does when you use
    $args[0] and it has only 1 argument in the Function, this sometimes
    gives an error too.)

    I came to this as I wanted to make my own More (the current More is to
    limited I think), as this uses if($args.length -ne 0).

    also I would like to know if there is a quick check for boolean
    Parameters ( Switches ) like -e or input parameters like text.txt.

    if you look at the current More Function I think You get why I ask this
    (decide to use $input or not) because I could give a switch but not a file.

    so Im realy strugling with getting arguments handled decently in MSH
    functions.

    (at the moment it looks to me if it is more easy to make a CMDlet as a
    function if you have to handle combinations of Parameters)

    gr /\/\o\/\/

    MSH>function lol {$Args | gm}
    MSH>lol 1 2


    TypeName: System.Int32

    Name MemberType Definition
    ---- ---------- ----------
    CompareTo Method System.Int32 CompareT
    Equals Method System.Boolean Equals
    GetHashCode Method System.Int32 GetHashC
    GetType Method System.Type GetType()
    GetTypeCode Method System.TypeCode GetTy
    ToString Method System.String ToStrin


    MSH>lol lol


    TypeName: System.String



    MSH>function lol {,$Args | gm}
    MSH>lol 1


    TypeName: System.Object[]

    Name MemberType Definition
    ---- ---------- ----------
    Count AliasProperty Count = Length
    .....
    Length Property System.Int32 Length {get;}

    MSH>function lol {$Args | gm}
    MSH>lol 1 2


    TypeName: System.Int32

    Name MemberType Definition
    ---- ---------- ----------
    CompareTo Method System.Int32 CompareT
    Equals Method System.Boolean Equals
    GetHashCode Method System.Int32 GetHashC
    GetType Method System.Type GetType()
    GetTypeCode Method System.TypeCode GetTy
    ToString Method System.String ToStrin


    MSH>lol lol


    TypeName: System.String
     
    /\\/\\o\\/\\/, Nov 12, 2005
    #1
    1. Advertisements

  2. Where is the More function Defined ?
    (it's the only function I can not find in my profiles)



    gr /\/\o\/\/
     
    /\\/\\o\\/\\/, Nov 12, 2005
    #2
    1. Advertisements

  3. hm, i think you have misssppellled "Length" as "lenght"

    Arrg, forget that one please.

    gr /\/\o\/\/
     
    /\\/\\o\\/\\/, Nov 13, 2005
    #3
  4. also found the checks I was looking for :

    Function ArgTest {
    if($args.length -ne 0) {
    if ($args -eq '-e') {write-host "E param given"}
    $InParamGiven = $False
    $args | foreach {if (([string]$_).substring(0,1) -ne '-')
    {$InParamGiven = $true}}
    "Inparm = $InParamGiven"
    }
    }

    the $args -eq '-e' will check all elements in the array

    and the other will look if there are nameless parameters.

    I think this will be usefull to more PLL,
    and hope that makes up my typo-mistake a bit

    gr /\/\o\/\/
     
    /\\/\\o\\/\\/, Nov 13, 2005
    #4
  5. Try this:

    (dir function:more).definition

    Jacques
     
    Jacques Barathon [MS], Nov 13, 2005
    #5
  6. thx, I have the Definition, it will show up normaly,only I can not find
    where it is defined. (in any profile file) so I got curious

    But I can overrule it in my Profile, that is the Most important part ;-)
    (as it is a bit limited)

    I wrote a new one that :

    1) will NOT leave his StatusMessages around messing up the output

    2) Will show the filePosition as Percentage

    3) Supports the P Command that let's YOU give a number of lines

    4) Supports the = Command that gives you the current Linenumber

    5) Supports the F Command that skips to the next file.

    6) Supports the -C argument to clear the Screen First.

    Who is interested can find it here :

    http://mow001.blogspot.com/2005/11/enhanced-more-function-for-msh.html

    gr /\/\o\/\/
     
    /\\/\\o\\/\\/, Nov 13, 2005
    #6
  7. /\\/\\o\\/\\/

    Keith Hill Guest

    Nice work! This does point out one deficiency in the MSH script language:
    the lack of C#'s using statement for disposing disposable objects. The
    problem with your current implementation is that once you run it on a file
    you can no longer write to that file in the MSH session. You need to
    sprinkle some $fs.Close calls in your code everywhere where you exit the
    function e.g.:

    "Q" {$fs.Close();$eof = $true;$Quit = $true}

    A "using" block would allow you to grab the $fs variable and then no matter
    how the using block is exited (exception or normally) the variable will get
    disposed e.g.:

    using ($fs = (get-item $file).OpenText()) {
    ....
    }
     
    Keith Hill, Nov 13, 2005
    #7
  8. Did you check, if using the OpenText it will close after getting out of
    scope (and does this locking problem, only goes for the lastfile, or all
    files)?

    (as the SB does count the bytes also, you can also Add that value after
    every page written to get the value) than you can use the textreader.)
    but as I did not care much about performance (viewing per page), I just
    took this solution to get the Position working (also had some Double
    newline problems, so maybe I should go back to the OpenText.).

    but ofcourse, You right about the Closing of the Files, I should make
    that more of a habbit.(as i'm not a programmer I tend to be a bit
    careless with releasing, just counting on it to get out of
    scope.)especialy when I'm just reading.

    Also the Ctrl-C I should handle myself I think to get this done.
    (does I ugly exit now)

    also nice was the difference between the ReadKey of Console and of the
    $host Object.(in handeling shift / control)

    also I'm still a bit finding my "style" in MSH and expect to change it
    for the coming time,i never intended it to be production quality, but
    keeping files locked after the function is gone out of scope is not
    good, I will look into that.

    thx for the remark, did not see this yet.

    gr /\/\o\/\/
     
    /\\/\\o\\/\\/, Nov 13, 2005
    #8
    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.