Using Command Line Arguments

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: " & strFilenameEnd 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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.