vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
<!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 &quot;long filename.doc&quot;</code></pre>
<p><code>Command$()</code> returns:</p>
<pre><code class="language-text">/debug file.txt &quot;long filename.doc&quot;</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">&#x27; Example 1: Get command line arguments
Sub Main()
    Dim cmdLine As String
    cmdLine = Command$()
    MsgBox &quot;Arguments: &quot; &amp; cmdLine
End Sub</code></pre>
<pre><code class="language-vbnet">&#x27; Example 2: Check if arguments provided
Sub Main()
    If Command$() &lt;&gt; &quot;&quot; Then
        MsgBox &quot;Arguments: &quot; &amp; Command$()
    Else
        MsgBox &quot;No arguments&quot;
    End If
End Sub</code></pre>
<pre><code class="language-vbnet">&#x27; Example 3: Simple file opener
Sub Main()
    Dim filename As String
    filename = Trim$(Command$())
    If filename &lt;&gt; &quot;&quot; Then
        OpenFile filename
    End If
End Sub</code></pre>
<pre><code class="language-vbnet">&#x27; Example 4: Check for debug mode
Sub Main()
    If InStr(Command$(), &quot;/debug&quot;) &gt; 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 = &quot;&quot; Then
        Set ParseArguments = result
        Exit Function
    End If
    parts = Split(args, &quot; &quot;)
    Dim i As Integer
    For i = LBound(parts) To UBound(parts)
        If Trim$(parts(i)) &lt;&gt; &quot;&quot; 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$()
    &#x27; Check for various switches
    If InStr(args, &quot;/debug&quot;) &gt; 0 Then
        App.LogMode = 1
    End If
    If InStr(args, &quot;/silent&quot;) &gt; 0 Then
        App.SilentMode = True
    End If
    If InStr(args, &quot;/verbose&quot;) &gt; 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 &lt;&gt; &quot;&quot; Then
        &#x27; 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
        &#x27; Verify file exists and open it
        If Dir$(filename) &lt;&gt; &quot;&quot; Then
            LoadDocument filename
        Else
            MsgBox &quot;File not found: &quot; &amp; 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 = &quot; &quot; &amp; Command$() &amp; &quot; &quot;
    pos = InStr(1, args, &quot;/&quot; &amp; paramName &amp; &quot;:&quot;, vbTextCompare)
    If pos &gt; 0 Then
        pos = pos + Len(paramName) + 2
        endPos = InStr(pos, args, &quot; &quot;)
        If endPos &gt; 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 &amp; &quot;\startup.log&quot; For Append As #logFile
    Print #logFile, Now &amp; &quot; - Started with args: &quot; &amp; 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$())
    &#x27; Set configuration based on arguments
    If InStr(args, &quot;/SERVER:&quot;) &gt; 0 Then
        App.ServerName = GetParameter(&quot;server&quot;)
    End If
    If InStr(args, &quot;/PORT:&quot;) &gt; 0 Then
        App.Port = Val(GetParameter(&quot;port&quot;))
    End If
    If InStr(args, &quot;/USER:&quot;) &gt; 0 Then
        App.UserName = GetParameter(&quot;user&quot;)
    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 = &quot;/?&quot; Or args = &quot;-?&quot; Or args = &quot;/help&quot; Or args = &quot;-help&quot; Then
        DisplayHelp
        End
    End If
End Sub
Sub DisplayHelp()
    Dim helpText As String
    helpText = &quot;Usage: MyApp [options]&quot; &amp; vbCrLf
    helpText = helpText &amp; &quot;/debug    - Enable debug mode&quot; &amp; vbCrLf
    helpText = helpText &amp; &quot;/silent   - Run in silent mode&quot; &amp; vbCrLf
    helpText = helpText &amp; &quot;/file:xxx - Open specified file&quot; &amp; 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, &quot;/batch&quot;) &gt; 0 Then
        &#x27; Parse file list
        files = Split(Replace$(args, &quot;/batch&quot;, &quot;&quot;), &quot; &quot;)
        For i = LBound(files) To UBound(files)
            If Trim$(files(i)) &lt;&gt; &quot;&quot; Then
                ProcessFile Trim$(files(i))
            End If
        Next i
        End  &#x27; 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$()
    &#x27; Process arguments
    If args &lt;&gt; &quot;&quot; Then
        ProcessCommandLine args
    End If
    Exit Sub
ErrorHandler:
    MsgBox &quot;Error processing command line: &quot; &amp; args &amp; vbCrLf &amp; _
           &quot;Error: &quot; &amp; 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 = &quot; &quot; &amp; UCase$(Command$()) &amp; &quot; &quot;
    switchName = &quot; /&quot; &amp; UCase$(switchName) &amp; &quot; &quot;
    HasSwitch = (InStr(args, switchName) &gt; 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>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>