parse command line 
Author Message
 parse command line

Does anybody know of a better way to parse command line variables then the
attached sample?  The reason I am counting spaces is because the way this
code reads the variables in it starts with the second variable and ends with
the first variable.  Because of this I need to know how many variables were
passed for my select statement that follows.

can someone make it give me the variables in order is what I am really
asking?

Option Explicit
Dim strPos, strpos2, CmdLine, i, CmdLineLen, currentChar, spacecount

Private Sub Form_Load()
CmdLineLen = Len(Command$)
spacecount = 0

For i = 1 To CmdLineLen
    currentChar = Mid(Command$, i, 1)
    If currentChar = " " Then
     spacecount = spacecount + 1
    End If
Next i
strPos = 1
For i = 1 To spacecount + 1
strPos = InStr(strPos + 1, Command$, " ")
strpos2 = InStr(strPos + 1, Command$, " ")
If strPos = "0" Then
    strPos = "1"
End If
If strpos2 = "0" Then
    strpos2 = CmdLineLen
End If

CmdLine = Mid(Command$, strPos, strpos2 - strPos + 1)
MsgBox Trim(CmdLine)
Next i
End
End Sub



Mon, 18 Mar 2002 03:00:00 GMT  
 parse command line
If you're using VB6, check out the Split function.
Quote:

> Does anybody know of a better way to parse command line variables then the
> attached sample?  The reason I am counting spaces is because the way this
> code reads the variables in it starts with the second variable and ends with
> the first variable.  Because of this I need to know how many variables were
> passed for my select statement that follows.

> can someone make it give me the variables in order is what I am really
> asking?

> Option Explicit
> Dim strPos, strpos2, CmdLine, i, CmdLineLen, currentChar, spacecount

> Private Sub Form_Load()
> CmdLineLen = Len(Command$)
> spacecount = 0

> For i = 1 To CmdLineLen
>     currentChar = Mid(Command$, i, 1)
>     If currentChar = " " Then
>      spacecount = spacecount + 1
>     End If
> Next i
> strPos = 1
> For i = 1 To spacecount + 1
> strPos = InStr(strPos + 1, Command$, " ")
> strpos2 = InStr(strPos + 1, Command$, " ")
> If strPos = "0" Then
>     strPos = "1"
> End If
> If strpos2 = "0" Then
>     strpos2 = CmdLineLen
> End If

> CmdLine = Mid(Command$, strPos, strpos2 - strPos + 1)
> MsgBox Trim(CmdLine)
> Next i
> End
> End Sub



Mon, 18 Mar 2002 03:00:00 GMT  
 parse command line
If not VB6, (or just to see how to do it) use a function that takes a
string, and strips off one item marked by a specified delimiter.  Just make
sure that the string ends with the delimiter or the method will fail ...

Private Sub Command1_Click()

   Dim sCommand As String
   Dim params() As String
   Dim thisParam As String
   Dim n As Long

  'get the command line, and add a trailing
  'delimiter to avoid an endless loop!!!!
   sCommand = Trim$(Command$()) & " "

   Do While sCommand > ""

      thisParam = StripItem(sCommand, " ")

      If thisParam > "" Then

         n = n + 1
         ReDim Preserve params(1 To n) As String
         params(n) = thisParam

      End If

   Loop

  'debug
   For n = LBound(params) To UBound(params)
      Print params(n)
   Next

End Sub

Function StripItem(startStrg As String, delimiter As String) As String

'this takes a string separated by delimiter,
'splits off 1 item, and shortens the string so
'that the next item is ready for removal.

   Dim pos As Integer

   pos = InStr(startStrg, delimiter)

   If pos Then
      StripItem = Mid$(startStrg, 1, pos - 1)
      startStrg = Mid$(startStrg, pos + 1, Len(startStrg))
   End If

End Function

--

Randy Birch, MVP Visual Basic

http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/

Please correspond only using the newsgroups so all can benefit.


| Does anybody know of a better way to parse command line variables then the
| attached sample?  The reason I am counting spaces is because the way this
| code reads the variables in it starts with the second variable and ends
with
| the first variable.  Because of this I need to know how many variables
were
| passed for my select statement that follows.
|
| can someone make it give me the variables in order is what I am really
| asking?
|
| Option Explicit
| Dim strPos, strpos2, CmdLine, i, CmdLineLen, currentChar, spacecount
|
| Private Sub Form_Load()
| CmdLineLen = Len(Command$)
| spacecount = 0
|
| For i = 1 To CmdLineLen
|     currentChar = Mid(Command$, i, 1)
|     If currentChar = " " Then
|      spacecount = spacecount + 1
|     End If
| Next i
| strPos = 1
| For i = 1 To spacecount + 1
| strPos = InStr(strPos + 1, Command$, " ")
| strpos2 = InStr(strPos + 1, Command$, " ")
| If strPos = "0" Then
|     strPos = "1"
| End If
| If strpos2 = "0" Then
|     strpos2 = CmdLineLen
| End If
|
| CmdLine = Mid(Command$, strPos, strpos2 - strPos + 1)
| MsgBox Trim(CmdLine)
| Next i
| End
| End Sub
|
|
|
|
|
|
|



Mon, 18 Mar 2002 03:00:00 GMT  
 parse command line
And if you need more sophisticated handling, there are
several classed floating around that handle command
lines..... I remember seeing one in Sybex's VBA Developer's
Handbook (Gitz/Gilbert), for example.

--
MichKa

-------------------------------------
don't send questions by e-mail unless
you're paying for it. (TANSTAAFL!) :-)

random junk of dubious value and
replica error and problem fixing at:
http://www.trigeminal.com


Quote:
> Private Sub Form_Load()
>   Dim lC&, vArray As Variant
>   vArray = Split(Command$, " ")
>   For lC = UBound(vArray) To 1 Step -1
>     MsgBox vArray(lC)
>   Next lC
> End Sub



Mon, 18 Mar 2002 03:00:00 GMT  
 parse command line
Private Sub Form_Load()
  Dim lC&, vArray As Variant
  vArray = Split(Command$, " ")
  For lC = UBound(vArray) To 1 Step -1
    MsgBox vArray(lC)
  Next lC
End Sub


Tue, 19 Mar 2002 03:00:00 GMT  
 parse command line
Try this:  :~)

'=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=
-=#
'Name:          GetCommandLine
'
'Description:   This function is used to process the command line arguments
if
'               any when the program is started.
'
'Parameters:    MaxArgs contains the total number of command line arguments.
'
'Returns:       The command line arguments in an array
'
Public Function GetCommandLine(Optional MaxArgs)
Dim vArgument, CmdLine, InArg, NumArgs
Dim CmdLnLen As Integer, I As Byte
Dim ArgArray()

'See if MaxArgs was provided.
If IsMissing(MaxArgs) Then MaxArgs = 10

  'Make array of the correct size.
  ReDim ArgArray(MaxArgs)
   NumArgs = 0
   InArg = False

   'Get command line arguments.
   CmdLine = Command()
   CmdLnLen = Len(Trim(CmdLine))

   'Go thru command line one character at a time.
   For I = 1 To CmdLnLen
     vArgument = Mid(CmdLine, I, 1)
      'Test for space or tab.
      If (vArgument <> " " And vArgument <> vbTab) Then
        'Neither space nor tab. Test if already in argument.
        If Not InArg Then
          'New argument begins. Test for too many arguments.
          If NumArgs = MaxArgs Then Exit For
             NumArgs = NumArgs + 1
             InArg = True
          End If
           'Add character to current argument.
           ArgArray(NumArgs) = ArgArray(NumArgs) + vArgument
        Else
           'Found a space or tab. Set InArg flag to False.
           InArg = False
        End If
   Next I

  'Resize array just enough to hold arguments.
  If NumArgs = 0 Then NumArgs = 1   'Array will always be of length 2
  ReDim Preserve ArgArray(NumArgs)

 'Return Array in Function name.
 GetCommandLine = ArgArray()
End Function

'=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=
-=#
'In form load (example)
aX = GetCommandLine
 With cboToggle
    .AddItem "Production", 0
    .AddItem "Development", 1
    Select Case aX(1)
      Case "T"
        .ListIndex = 1
      Case Else
        .ListIndex = 0
    End Select
 End With



Sat, 23 Mar 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. parsing command line

2. Help Parsing Command Line Parms

3. Parse Command Line

4. Parsing Command line Arguments in VB Script

5. reading/parsing command line arguments AND return codes from vbs's

6. Standard for parsing command line path

7. Command Line Argument Parsing

8. Code to parse the command line

9. Command Line Parsing

10. Looking for robust command line parse algorithm

11. Help: Command line parsing

12. getopt / command line parsing

 

 
Powered by phpBB® Forum Software