CarlLindren Posted March 22, 2009 Share Posted March 22, 2009 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 More sharing options...
Authenticity Posted March 22, 2009 Share Posted March 22, 2009 $CmdLine[0] contains the number of parameters submitted so it will contain 0 if nothing is submitted. Link to comment Share on other sites More sharing options...
CarlLindren Posted March 22, 2009 Author Share Posted March 22, 2009 $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 More sharing options...
Authenticity Posted March 22, 2009 Share Posted March 22, 2009 Yes, because if $CmdLine[0] is 0 then $CmdLine[1] doesn't exist. Link to comment Share on other sites More sharing options...
CarlLindren Posted March 22, 2009 Author Share Posted March 22, 2009 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 More sharing options...
Authenticity Posted March 22, 2009 Share Posted March 22, 2009 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. Link to comment Share on other sites More sharing options...
TerarinK Posted March 22, 2009 Share Posted March 22, 2009 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now