Sign in to follow this  
Followers 0
boomingranny

cmdline issue (is it just me or is it a bug?)

5 posts in this topic

What would you expect the code below to do?

If $CmdLine[$CmdLine[0]] = "inevertypedthis" Then 
    MsgBox (0, "wtf","wtf")
EndIf

because it is showing the msgbox...

am i doing something wrong?!

Share this post


Link to post
Share on other sites



ahh, nm i think it is converting the text to a number... :) still confusing though...

Share this post


Link to post
Share on other sites

If a string is used as a number, an implicit call to Number() function is done. So if it doesn't contain a valid number, it will be assumed to equal 0.

In your example $CmdLine[0] is 0. However using == comparison gives proper result.

If $CmdLine[$CmdLine[0]] == "inevertypedthis" Then 
    MsgBox (0, "wtf","wtf")
EndIf

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

In your example $CmdLine[0] is 0. However using == comparison gives proper result.

If $CmdLine[$CmdLine[0]] == "inevertypedthis" Then 
    MsgBox (0, "wtf","wtf")
EndIf
Correct in fact:

$CmdLine[0] is number of parameters

$CmdLine[1] is param 1 (after the script name)

$CmdLine[2] is param 2 etc

...

$CmdLine[$CmdLine[0]] is one way to get the last parameter...

So if your script is run like this:

AutoIt3.exe myscript.au3 param1 "this is another param"

$CmdLine[0] equals... 2

$CmdLine[1] equals... param1

$CmdLine[2] equals... this is another param

Edited by Jango

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Poor logic and the idea of using "==" does nothing to improve it sorry to say. If "$CMDLINE[$CMDLINE[0]]" equates to "$CMDLINE[0]" then any comparison to the string is flawed as "$CMDLINE[0]" contains a number. Using "==" is forcing case sensitivity between strings thus is not a solution.

This is what I would do in such a case accepting you actually want the last parameter if any were passed.

If $CMDLINE[0] And $CMDLINE[$CMDLINE[0]] = "inevertypedthis" Then
    MsgBox (0, "wtf","wtf")
EndIf

Now you know that you are not comparing a string with "$CMDLINE[0]" as it cannot happen.

Edited by MHz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0