<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - command_dollar - Interaction">
<title>command_dollar - Interaction - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/interaction/index.html">Interaction</a> / command_dollar</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="command-function">Command$ Function</h1>
<p>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 (<code>$</code>) explicitly
indicates that this function returns a <code>String</code> type (not a <code>Variant</code>).</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Command$()</code></pre>
<h2 id="parameters">Parameters</h2>
<p>None. The <code>Command$</code> function takes no arguments.</p>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>String</code> containing the command-line arguments passed to the program. If no arguments
were passed, returns an empty string (""). The return value is always a <code>String</code> type (never <code>Variant</code>).</p>
<h2 id="remarks">Remarks</h2>
<ul>
<li>The <code>Command$</code> function always returns a <code>String</code>, while <code>Command</code> (without <code>$</code>) can return a <code>Variant</code>.</li>
<li>Returns only the arguments, not the executable path or name.</li>
<li>Arguments are returned as a single string, exactly as passed to the application.</li>
<li>Multiple arguments are separated by spaces (unless quoted).</li>
<li>Quoted strings preserve internal spaces but quotes may be included in the result.</li>
<li>Leading and trailing spaces are typically trimmed by the system.</li>
<li>Returns empty string ("") if no arguments were provided.</li>
<li>Case is preserved as entered on the command line.</li>
<li>For better performance when you know the result is a string, use <code>Command$</code> instead of <code>Command</code>.</li>
</ul>
<h2 id="command-line-processing">Command Line Processing</h2>
<p>When an application is launched with:</p>
<pre><code class="language-text">MyApp.exe /debug file.txt "long filename.doc"</code></pre>
<p><code>Command$()</code> returns:</p>
<pre><code class="language-text">/debug file.txt "long filename.doc"</code></pre>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Processing startup parameters</strong> - Read switches and configuration flags</li>
<li><strong>File path handling</strong> - Accept file paths to open at startup</li>
<li><strong>Debug modes</strong> - Enable special debugging or logging modes</li>
<li><strong>Automation</strong> - Support scripted or automated workflows</li>
<li><strong>Configuration</strong> - Pass runtime configuration without config files</li>
<li><strong>Batch processing</strong> - Process multiple files or operations</li>
<li><strong>Integration</strong> - Allow other applications to control behavior</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<pre><code class="language-vbnet">' Example 1: Get command line arguments
Sub Main()
Dim cmdLine As String
cmdLine = Command$()
MsgBox "Arguments: " & cmdLine
End Sub</code></pre>
<pre><code class="language-vbnet">' Example 2: Check if arguments provided
Sub Main()
If Command$() <> "" Then
MsgBox "Arguments: " & Command$()
Else
MsgBox "No arguments"
End If
End Sub</code></pre>
<pre><code class="language-vbnet">' Example 3: Simple file opener
Sub Main()
Dim filename As String
filename = Trim$(Command$())
If filename <> "" Then
OpenFile filename
End If
End Sub</code></pre>
<pre><code class="language-vbnet">' Example 4: Check for debug mode
Sub Main()
If InStr(Command$(), "/debug") > 0 Then
EnableDebugMode
End If
End Sub</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="processing-multiple-arguments">Processing Multiple Arguments</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="processing-switches-and-parameters">Processing Switches and Parameters</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="opening-file-from-command-line">Opening File from Command Line</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="named-parameter-extraction">Named Parameter Extraction</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="logging-startup-arguments">Logging Startup Arguments</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="configuration-from-command-line">Configuration from Command Line</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="help-display">Help Display</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="batch-mode-processing">Batch Mode Processing</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="error-recovery">Error Recovery</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="case-insensitive-switch-detection">Case-Insensitive Switch Detection</h3>
<pre><code class="language-vbnet">Function HasSwitch(switchName As String) As Boolean
Dim args As String
args = " " & UCase$(Command$()) & " "
switchName = " /" & UCase$(switchName) & " "
HasSwitch = (InStr(args, switchName) > 0)
End Function</code></pre>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Command</code>: Returns command-line arguments as <code>Variant</code> instead of <code>String</code></li>
<li><code>App.Path</code>: Returns the path where the application executable is located</li>
<li><code>App.EXEName</code>: Returns the name of the executable file</li>
<li><code>Environ$</code>: Returns environment variable values</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li>Always trim the result to remove leading/trailing spaces</li>
<li>Handle the case where no arguments are provided (empty string)</li>
<li>Use case-insensitive comparison for switches and parameters</li>
<li>Document expected command-line format in your application</li>
<li>Validate arguments before using them</li>
<li>Provide meaningful error messages for invalid arguments</li>
<li>Consider implementing a <code>/help</code> or <code>/?</code> switch</li>
<li>Use <code>Command$</code> instead of <code>Command</code> for better performance</li>
<li>Be careful with quoted strings - they may include the quotes</li>
<li>Log startup arguments for debugging and support purposes</li>
</ol>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Command$</code> is slightly more efficient than <code>Command</code> because it avoids <code>Variant</code> overhead</li>
<li>The function is typically called once at startup, so performance is rarely a concern</li>
<li>Parsing complex command lines can be slow; cache the result if needed multiple times</li>
<li>Consider using a dedicated command-line parser for complex argument processing</li>
</ul>
<h2 id="platform-notes">Platform Notes</h2>
<ul>
<li>Command-line argument handling is consistent across Windows platforms</li>
<li>Maximum command-line length varies by Windows version (typically 8191 characters)</li>
<li>Arguments are passed by the operating system when the executable is launched</li>
<li>VB6 IDE does not allow setting command-line arguments for debugging</li>
<li>Use a shortcut or command prompt to test command-line arguments</li>
</ul>
<h2 id="security-considerations">Security Considerations</h2>
<ol>
<li>Never execute command-line arguments directly as code</li>
<li>Validate all file paths before accessing files</li>
<li>Sanitize arguments before using in SQL queries or shell commands</li>
<li>Limit accepted argument values to known good values when possible</li>
<li>Log suspicious or malformed command-line arguments</li>
</ol>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Returns arguments as a single string (manual parsing required for multiple arguments)</li>
<li>Does not provide the executable path or name (use <code>App.Path</code> and <code>App.EXEName</code>)</li>
<li>No built-in support for named parameters (must implement custom parsing)</li>
<li>Quote handling is system-dependent and may vary</li>
<li>Cannot distinguish between missing arguments and empty string argument</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to Interaction</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>