VB6Parse / Library / Interaction / command_dollar

VB6 Library Reference

Command$ Function

Returns the argument portion of the command line used to launch Microsoft Visual Basic or an executable program developed with Visual Basic. The dollar sign suffix ($) explicitly indicates that this function returns a String type (not a Variant).

Syntax

Command$()

Parameters

None. The Command$ function takes no arguments.

Return Value

Returns a String containing the command-line arguments passed to the program. If no arguments were passed, returns an empty string (""). The return value is always a String type (never Variant).

Remarks

Command Line Processing

When an application is launched with:

MyApp.exe /debug file.txt "long filename.doc"

Command$() returns:

/debug file.txt "long filename.doc"

Typical Uses

  1. Processing startup parameters - Read switches and configuration flags
  2. File path handling - Accept file paths to open at startup
  3. Debug modes - Enable special debugging or logging modes
  4. Automation - Support scripted or automated workflows
  5. Configuration - Pass runtime configuration without config files
  6. Batch processing - Process multiple files or operations
  7. Integration - Allow other applications to control behavior

Basic Examples

' Example 1: Get command line arguments
Sub Main()
Dim cmdLine As String
cmdLine = Command$()
MsgBox "Arguments: " & cmdLine
End Sub
' Example 2: Check if arguments provided
Sub Main()
If Command$() <> "" Then
MsgBox "Arguments: " & Command$()
Else
MsgBox "No arguments"
End If
End Sub
' Example 3: Simple file opener
Sub Main()
Dim filename As String
filename = Trim$(Command$())
If filename <> "" Then
OpenFile filename
End If
End Sub
' Example 4: Check for debug mode
Sub Main()
If InStr(Command$(), "/debug") > 0 Then
EnableDebugMode
End If
End Sub

Common Patterns

Processing Multiple Arguments

Function ParseArguments() As Collection
Dim args As String
Dim result As New Collection
Dim parts() As String

args = Command$()
If args = "" Then
Set ParseArguments = result
Exit Function
End If

parts = Split(args, " ")
Dim i As Integer
For i = LBound(parts) To UBound(parts)
If Trim$(parts(i)) <> "" Then
result.Add Trim$(parts(i))
End If
Next i

Set ParseArguments = result
End Function

Processing Switches and Parameters

Sub Main()
Dim args As String
args = Command$()

' Check for various switches
If InStr(args, "/debug") > 0 Then
App.LogMode = 1
End If

If InStr(args, "/silent") > 0 Then
App.SilentMode = True
End If

If InStr(args, "/verbose") > 0 Then
App.VerboseMode = True
End If
End Sub

Opening File from Command Line

Sub Main()
Dim filename As String
filename = Trim$(Command$())

If filename <> "" Then
' Remove surrounding quotes if present
If Left$(filename, 1) = Chr$(34) Then
filename = Mid$(filename, 2)
End If
If Right$(filename, 1) = Chr$(34) Then
filename = Left$(filename, Len(filename) - 1)
End If

' Verify file exists and open it
If Dir$(filename) <> "" Then
LoadDocument filename
Else
MsgBox "File not found: " & filename
End If
End If
End Sub

Named Parameter Extraction

Function GetParameter(paramName As String) As String
Dim args As String
Dim pos As Integer
Dim endPos As Integer
Dim result As String

args = " " & Command$() & " "
pos = InStr(1, args, "/" & paramName & ":", vbTextCompare)

If pos > 0 Then
pos = pos + Len(paramName) + 2
endPos = InStr(pos, args, " ")
If endPos > pos Then
result = Mid$(args, pos, endPos - pos)
End If
End If

GetParameter = result
End Function

Logging Startup Arguments

Sub Main()
Dim args As String
Dim logFile As Integer

args = Command$()

logFile = FreeFile
Open App.Path & "\startup.log" For Append As #logFile
Print #logFile, Now & " - Started with args: " & args
Close #logFile
End Sub

Configuration from Command Line

Sub Main()
Dim args As String
args = UCase$(Command$())

' Set configuration based on arguments
If InStr(args, "/SERVER:") > 0 Then
App.ServerName = GetParameter("server")
End If

If InStr(args, "/PORT:") > 0 Then
App.Port = Val(GetParameter("port"))
End If

If InStr(args, "/USER:") > 0 Then
App.UserName = GetParameter("user")
End If
End Sub

Help Display

Sub Main()
Dim args As String
args = LCase$(Trim$(Command$()))

If args = "/?" Or args = "-?" Or args = "/help" Or args = "-help" Then
DisplayHelp
End
End If
End Sub

Sub DisplayHelp()
Dim helpText As String
helpText = "Usage: MyApp [options]" & vbCrLf
helpText = helpText & "/debug    - Enable debug mode" & vbCrLf
helpText = helpText & "/silent   - Run in silent mode" & vbCrLf
helpText = helpText & "/file:xxx - Open specified file" & vbCrLf
MsgBox helpText
End Sub

Batch Mode Processing

Sub Main()
Dim args As String
Dim files() As String
Dim i As Integer

args = Command$()

If InStr(args, "/batch") > 0 Then
' Parse file list
files = Split(Replace$(args, "/batch", ""), " ")

For i = LBound(files) To UBound(files)
If Trim$(files(i)) <> "" Then
ProcessFile Trim$(files(i))
End If
Next i

End  ' Exit after batch processing
End If
End Sub

Error Recovery

Sub Main()
On Error GoTo ErrorHandler

Dim args As String
args = Command$()

' Process arguments
If args <> "" Then
ProcessCommandLine args
End If

Exit Sub

ErrorHandler:
MsgBox "Error processing command line: " & args & vbCrLf & _
"Error: " & Err.Description
End
End Sub

Case-Insensitive Switch Detection

Function HasSwitch(switchName As String) As Boolean
Dim args As String
args = " " & UCase$(Command$()) & " "
switchName = " /" & UCase$(switchName) & " "
HasSwitch = (InStr(args, switchName) > 0)
End Function

Best Practices

  1. Always trim the result to remove leading/trailing spaces
  2. Handle the case where no arguments are provided (empty string)
  3. Use case-insensitive comparison for switches and parameters
  4. Document expected command-line format in your application
  5. Validate arguments before using them
  6. Provide meaningful error messages for invalid arguments
  7. Consider implementing a /help or /? switch
  8. Use Command$ instead of Command for better performance
  9. Be careful with quoted strings - they may include the quotes
  10. Log startup arguments for debugging and support purposes

Performance Considerations

Platform Notes

Security Considerations

  1. Never execute command-line arguments directly as code
  2. Validate all file paths before accessing files
  3. Sanitize arguments before using in SQL queries or shell commands
  4. Limit accepted argument values to known good values when possible
  5. Log suspicious or malformed command-line arguments

Limitations

← Back to Interaction | View all functions