How to program for command line switches

Posted on 2003-03-14
Medium Priority
Last Modified: 2010-04-07
I saw emoreau's class example but didn't know how to use it.


Question by:jazz4sale
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 70

Expert Comment

by:Éric Moreau
ID: 8138810
which class? I can surely help if it is from me?

Accepted Solution

Sweat earned 100 total points
ID: 8139427

If you are talking about picking up switches from the command line to your program:

   myprogram.exe /s /p /l

then you would do something like the following in your Sub Main() section.

Sub Main()
   Dim sArray() As String, sCmdLine As String

   sCmdLine = Command()
   If sCmdLine <> "" Then
      sArray = Split(sCmdLine, " ")
      ' Your sArray elements would look like this:
      ' sArray(0) "/s"
      ' sArray(1) "/p"
      ' sArray(2) "/l"
      ' From there you'd set boolean or values to do whatever is needed based on the command line switches.

   End If

End Sub

If you aren't talking about picking up command line switches for your program, then please ignore this.



Assisted Solution

Shaka913 earned 100 total points
ID: 8145355
Here is how I do it in VB, in the Form OnLoad event

Private Sub Form_Load()
    Dim zI As Integer
    Dim zArgs() As String
     Dim i As Integer
    zArgs = Split(Command$, " ")
   For i = LBound(zArgs) To UBound(zArgs)
      Select Case Left(LCase(zArgs(i)), 2)
      Case "-f"
        mFileName = Mid$(zArgs(i), 3)
      Case "-n"
        mFullFilename = Mid(zArgs(i), 3)
      Case "-c"
        mConnectCatalog = Mid$(zArgs(i), 3)
      Case "-d"
        mConnectDataSource = Mid$(zArgs(i), 3)
      Case "-i"
        mFileID = CLng(Mid$(zArgs(i), 3))
      Case Else
         MsgBox "Invalid argument: " & zArgs(i)
      End Select
   Next i


beware that this assumes that the switches are case INsensitive.

Hope this helps.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8155815
How is this going? are you going to pick an answer?

Expert Comment

ID: 8900067
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
LVL 49

Expert Comment

ID: 9699739
Moderator, my recommended disposition is:

    Split points between: Sweat and Shaka913

DanRollins -- EE database cleanup volunteer

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question