Jump to content

$CmdLine[] conditional IF statement


Recommended Posts

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"

Link to comment
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?

Link to comment
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.

Link to comment
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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...