Sign in to follow this  
Followers 0
CarlLindren

$CmdLine[] conditional IF statement

7 posts in this topic

How do you handle a NULL value in a conditional IF statement?

Usage with an argument

C:> My_Program -h

Usage if the argument is not included

C:> My_Program

What I need to know is how to handle the IF statement when the user doesn't include a command line argument.

If $CmdLine[1] == "-d" Then
    Do Something
ElseIf $CmdLine[1 ]== "-w" Then
    Do Something
ElseIf$CmdLine[1] == NULL Then
    MsgBox(8192,"My Program", "Must enter an argument") 
EndIf

----------------------

Carl Lindgren

"Sometimes living to learn but always learning to live"

Share this post


Link to post
Share on other sites



$CmdLine[0] contains the number of parameters submitted so it will contain 0 if nothing is submitted.

Share this post


Link to post
Share on other sites

$CmdLine[0] contains the number of parameters submitted so it will contain 0 if nothing is submitted.

So, the statement, I deduce, would be...

If $CmdLine[1] == "-d" Then
    Do Something
ElseIf $CmdLine[1]== "-w" Then
    Do Something
ElseIf $CmdLine[0] == "0" Then
    MsgBox(8192,"My Program", "Must enter an argument") 
EndIf

However, it still errors out. Does it matter where I put that statement in the nest?

Share this post


Link to post
Share on other sites

Yes, because if $CmdLine[0] is 0 then $CmdLine[1] doesn't exist.

Share this post


Link to post
Share on other sites

Yes, because if $CmdLine[0] is 0 then $CmdLine[1] doesn't exist.

OK... The logic just hit me.

Wished the $CmdLine[] and Arrays were better documented. I assumed $CmdLine[0] was the name of the script as it is in other languages but after rereading the help file I do see it (my fault). Is there other anomalies regarding the $CmdLine[] or Arrays that would be helpful to know about? Like [*] [@]... Is the script name in the $CmdLine[] array at all?

Thanks for your help.

Share this post


Link to post
Share on other sites

Search the help file for words like "script", it's obvious. As for the question, you don't need to deserve one precious element ;] for the script name as it's available through the @ScriptName macro.

Share this post


Link to post
Share on other sites

This would be a faster way of doing it:

For $i = 1 To $CmdLine[0]
    Select
        Case $CmdLine[$i] == "-d"
            _Expression("First")
        Case $CmdLine[$i] == "-w"
            _Expression("Second")
        Case Else
            MsgBox(0, "My Program", "Must enter an agrument")
    EndSelect
Next

Func _Expression($sLabel)
    MsgBox(0, "", $sLabel & " Case expression was true")
EndFunc   ;==>_Expression

If you have more it will show it's speed then. It also accounts for 0 passing the $CmdLine[0] then. If you have the first such as -s for string following that then you need to account for that extra line count, as simple as putting a $i += 1


0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

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