Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using Command Line Arguments

Even with a GUI available to you, there are some cases in which you need to build a VB application that doesn't have a front-end. In these cases, one of the easiest ways to control the function of the application is through command line arguments.


advertisement
Editor's Note: Code in this Solution has been updated since its original publication.

ne of the virtually forgotten features of Visual Basic is the ability to accept command-line parameters when an application is executed. Even with a GUI available to you, there are some cases in which you need to build a VB application that doesn't have a front-end. In these cases, one of the easiest ways to control the function of the application is through command line arguments.

Command line arguments are passed into Visual Basic by way of the Command$ variable. This variable is a very long string with all the arguments written exactly as the user entered them. When running an application, you would add them to the shortcut or the Run line. However, you may also need to use arguments in testing within the VB environment. To do this:

1. Select Properties from the Project menu.
2. Click the Make tab.
3. As shown in Figure 1, enter the arguments that you want to pass to the application when it runs in the development environment.

Interpreting the arguments is the next step. If you've ever had computer science courses dealing with compilers and parsers, this should be fairly straightforward for you. For this application, we're going to allow two options:

-d indicates debug mode
-f allows a filename to be specified immediately after the -f flag.

Once the application has started, we need to determine if any of these arguments were supplied, and store the values in some variables for later use. The code is shown below:



Sub Main() Dim a_strArgs() As String Dim blnDebug As Boolean Dim strFilename As String Dim i As Integer a_strArgs = Split(Command$, " ") For i = LBound(a_strArgs) To UBound(a_strArgs) Select Case LCase(a_strArgs(i)) Case "-d", "/d" ' debug mode blnDebug = True Case "-f", "/f" ' filename specified If i = UBound(a_strArgs) Then MsgBox "Filename not specified." Else i = i + 1 End If If Left(a_strArgs(i), 1) = "-" Or Left(a_strArgs(i), 1) = "/" Then MsgBox "Invalid filename." Else strFilename = a_strArgs(i) End If Case Else MsgBox "Invalid argument: " & a_strArgs(i) End Select Next i MsgBox "Debug mode: " & blnDebug MsgBox "Filename: " & strFilename End Sub

The Split function breaks the command line into an array of arguments. This one function saves us many lines of parsing code. We then loop through the array and attempt to determine what arguments were passed in. If we find a -d, we set a Boolean to indicate that we are in debug mode. If we find a -f, we have an extra step—to determine if a filename was passed in and if it had a valid name. Check to make sure there is another argument following the -f, then look at the filename to see if it begins with a dash or slash, which would indicate another argument. You should expand this error handling to actually open the file, just to make sure that you can. Finally, print out the arguments that were passed in.

This is a handy technique that can save you from having to deal with the registry or a database just to get some arguments into your application. Download sample code here.



   
Eric Smith has been a senior consultant with Andersen Consulting, specializing in client/server development using Oracle and Visual Basic, and was senior content developer (Visual Basic) at inquiry.com before it was purchased by Fawcette Technical Publications. He joined the Information Strategies team in September 1997 and is now an independent consultant. He has written or contributed to "Visual Basic 6 Bible" (IDG Books Worldwide, 1998), "Using Visual Basic 6" (Que), and "Inside VBScript with ActiveX" (New Riders Press, 1997). He is certified in Visual Basic 5.0 and Windows Architecture (1 and 2). Reach him here.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap