<!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 - Interaction">
<title>command - 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</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.</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 String containing the command-line arguments passed to the program. If no arguments
were passed, returns an empty string ("").</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Command</code> function provides access to the command-line arguments that were passed when the
application was started. This is commonly used for:
- Processing startup parameters
- Accepting file paths to open
- Enabling debug or special modes
- Configuring application behavior at launch
<strong>Important Characteristics:</strong>
- Returns only the arguments, not the executable path
- Arguments are returned as a single string
- Multiple arguments are separated by spaces (unless quoted)
- Quoted strings are preserved but quotes may be included in the result
- Leading and trailing spaces are typically trimmed
- Returns empty string ("") if no arguments provided
- Case is preserved as entered</p>
<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="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">' Get command line arguments
Sub Main()
Dim cmdLine As String
cmdLine = Command()
If cmdLine <> "" Then
MsgBox "Arguments: " & cmdLine
Else
MsgBox "No arguments provided"
End If
End Sub</code></pre>
<h3 id="processing-switches">Processing Switches</h3>
<pre><code class="language-vbnet">Sub Main()
Dim args As String
args = Command()
If InStr(args, "/debug") > 0 Then
App.LogMode = 1 ' Enable debug logging
End If
If InStr(args, "/silent") > 0 Then
App.SilentMode = True
End If
End Sub</code></pre>
<h3 id="opening-a-file-from-command-line">Opening a File from Command Line</h3>
<pre><code class="language-vbnet">Sub Main()
Dim filename As String
filename = Trim(Command())
If filename <> "" Then
' Remove 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
' Open the file
If Dir(filename) <> "" Then
OpenDocument filename
End If
End If
End Sub</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="parsing-multiple-arguments">Parsing Multiple Arguments</h3>
<pre><code class="language-vbnet">Function ParseCommandLine() As Collection
Dim args As New Collection
Dim cmdLine As String
Dim arg As String
Dim pos As Integer
Dim inQuotes As Boolean
Dim i As Integer
Dim ch As String
cmdLine = Trim(Command())
If cmdLine = "" Then Exit Function
arg = ""
inQuotes = False
For i = 1 To Len(cmdLine)
ch = Mid(cmdLine, i, 1)
If ch = Chr(34) Then ' Quote character
inQuotes = Not inQuotes
ElseIf ch = " " And Not inQuotes Then
If arg <> "" Then
args.Add arg
arg = ""
End If
Else
arg = arg & ch
End If
Next i
If arg <> "" Then args.Add arg
Set ParseCommandLine = args
End Function</code></pre>
<h3 id="named-parameters">Named Parameters</h3>
<pre><code class="language-vbnet">Function GetParameter(paramName As String) As String
Dim cmdLine As String
Dim pos As Integer
Dim endPos As Integer
Dim value As String
cmdLine = " " & Command() & " "
pos = InStr(1, cmdLine, "/" & paramName & ":", vbTextCompare)
If pos = 0 Then
pos = InStr(1, cmdLine, "-" & paramName & ":", vbTextCompare)
End If
If pos > 0 Then
pos = InStr(pos, cmdLine, ":") + 1
endPos = InStr(pos, cmdLine, " ")
If endPos > pos Then
value = Mid(cmdLine, pos, endPos - pos)
GetParameter = Trim(value)
End If
End If
End Function
' Usage:
' MyApp.exe /server:localhost /port:8080
' server = GetParameter("server") ' Returns "localhost"
' port = GetParameter("port") ' Returns "8080"</code></pre>
<h3 id="switch-detection">Switch Detection</h3>
<pre><code class="language-vbnet">Function HasSwitch(switchName As String) As Boolean
Dim cmdLine As String
cmdLine = " " & LCase(Command()) & " "
HasSwitch = InStr(cmdLine, " /" & LCase(switchName)) > 0 Or _
InStr(cmdLine, " -" & LCase(switchName)) > 0
End Function
' Usage:
' MyApp.exe /debug /verbose
' If HasSwitch("debug") Then ...</code></pre>
<h3 id="file-association-handler">File Association Handler</h3>
<pre><code class="language-vbnet">Sub Main()
Dim filename As String
filename = GetCommandLineFile()
If filename <> "" Then
' Application was launched by double-clicking a file
LoadFile filename
Else
' Application was launched normally
ShowStartupDialog
End If
End Sub
Function GetCommandLineFile() As String
Dim cmdLine As String
cmdLine = Trim(Command())
' Remove surrounding quotes
If Left(cmdLine, 1) = Chr(34) And Right(cmdLine, 1) = Chr(34) Then
cmdLine = Mid(cmdLine, 2, Len(cmdLine) - 2)
End If
' Check if it's a file (not a switch)
If Left(cmdLine, 1) <> "/" And Left(cmdLine, 1) <> "-" Then
If Dir(cmdLine) <> "" Then
GetCommandLineFile = cmdLine
End If
End If
End Function</code></pre>
<h3 id="configuration-file-loading">Configuration File Loading</h3>
<pre><code class="language-vbnet">Sub Main()
Dim configFile As String
configFile = GetParameter("config")
If configFile = "" Then
configFile = App.Path & "\default.cfg"
End If
LoadConfiguration configFile
End Sub</code></pre>
<h3 id="debug-mode-activation">Debug Mode Activation</h3>
<pre><code class="language-vbnet">Public DebugMode As Boolean
Sub Main()
Dim cmdLine As String
cmdLine = LCase(Trim(Command()))
DebugMode = (InStr(cmdLine, "/debug") > 0) Or _
(InStr(cmdLine, "-debug") > 0) Or _
(InStr(cmdLine, "/d") > 0)
If DebugMode Then
MsgBox "Debug mode enabled"
End If
End Sub</code></pre>
<h3 id="multiple-file-processing">Multiple File Processing</h3>
<pre><code class="language-vbnet">Sub Main()
Dim files() As String
Dim i As Integer
files = GetCommandLineFiles()
For i = LBound(files) To UBound(files)
ProcessFile files(i)
Next i
End Sub
Function GetCommandLineFiles() As String()
Dim cmdLine As String
Dim args As Collection
Dim result() As String
Dim i As Integer
Dim count As Integer
Set args = ParseCommandLine()
' Count files (skip switches)
For i = 1 To args.Count
If Left(args(i), 1) <> "/" And Left(args(i), 1) <> "-" Then
count = count + 1
End If
Next i
If count > 0 Then
ReDim result(1 To count)
count = 0
For i = 1 To args.Count
If Left(args(i), 1) <> "/" And Left(args(i), 1) <> "-" Then
count = count + 1
result(count) = args(i)
End If
Next i
End If
GetCommandLineFiles = result
End Function</code></pre>
<h3 id="automation-mode">Automation Mode</h3>
<pre><code class="language-vbnet">Sub Main()
If HasSwitch("auto") Or HasSwitch("batch") Then
' Run in automated mode without UI
RunBatchProcess
End
Else
' Show normal UI
Form1.Show
End If
End Sub</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="complex-argument-parser">Complex Argument Parser</h3>
<pre><code class="language-vbnet">Type CommandLineArg
Name As String
Value As String
IsSwitch As Boolean
End Type
Function ParseAdvancedCommandLine() As Collection
Dim args As New Collection
Dim cmdLine As String
Dim tokens As Collection
Dim i As Integer
Dim token As String
Dim arg As CommandLineArg
cmdLine = Command()
Set tokens = ParseCommandLine()
For i = 1 To tokens.Count
token = tokens(i)
If Left(token, 1) = "/" Or Left(token, 1) = "-" Then
arg.IsSwitch = True
' Remove leading / or -
token = Mid(token, 2)
' Check for name:value format
If InStr(token, ":") > 0 Then
arg.Name = Left(token, InStr(token, ":") - 1)
arg.Value = Mid(token, InStr(token, ":") + 1)
ElseIf InStr(token, "=") > 0 Then
arg.Name = Left(token, InStr(token, "=") - 1)
arg.Value = Mid(token, InStr(token, "=") + 1)
Else
arg.Name = token
arg.Value = "True"
End If
Else
arg.IsSwitch = False
arg.Name = ""
arg.Value = token
End If
args.Add arg
Next i
Set ParseAdvancedCommandLine = args
End Function</code></pre>
<h3 id="environment-variable-expansion">Environment Variable Expansion</h3>
<pre><code class="language-vbnet">Function ExpandCommandLine() As String
Dim cmdLine As String
Dim startPos As Integer
Dim endPos As Integer
Dim varName As String
Dim varValue As String
cmdLine = Command()
' Expand %VARIABLE% syntax
Do
startPos = InStr(cmdLine, "%")
If startPos = 0 Then Exit Do
endPos = InStr(startPos + 1, cmdLine, "%")
If endPos = 0 Then Exit Do
varName = Mid(cmdLine, startPos + 1, endPos - startPos - 1)
varValue = Environ(varName)
cmdLine = Left(cmdLine, startPos - 1) & varValue & Mid(cmdLine, endPos + 1)
Loop
ExpandCommandLine = cmdLine
End Function</code></pre>
<h3 id="help-text-display">Help Text Display</h3>
<pre><code class="language-vbnet">Sub Main()
If HasSwitch("?") Or HasSwitch("help") Then
ShowHelp
End
End If
' Normal startup
Form1.Show
End Sub
Sub ShowHelp()
Dim helpText As String
helpText = "MyApp - Command Line Options" & vbCrLf & vbCrLf
helpText = helpText & "/debug Enable debug mode" & vbCrLf
helpText = helpText & "/config:file Load configuration from file" & vbCrLf
helpText = helpText & "/silent Run in silent mode" & vbCrLf
helpText = helpText & "/auto Run in automated mode" & vbCrLf
helpText = helpText & "/help or /? Show this help" & vbCrLf
MsgBox helpText, vbInformation
End Sub</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function SafeGetCommand() As String
On Error GoTo ErrorHandler
SafeGetCommand = Command()
Exit Function
ErrorHandler:
' Command() rarely fails, but handle just in case
SafeGetCommand = ""
End Function</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Command()</code> is a fast function with minimal overhead</li>
<li>Result is cached, so multiple calls don't re-query the OS</li>
<li>Consider caching the result in a module-level variable if used frequently</li>
<li>Parsing complex command lines can be expensive; cache parsed results</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="cache-the-result">Cache the Result</h3>
<pre><code class="language-vbnet">Public g_CommandLine As String
Sub Main()
g_CommandLine = Command()
' Use g_CommandLine throughout the application
If InStr(g_CommandLine, "/debug") > 0 Then
' ...
End If
End Sub</code></pre>
<h3 id="validate-arguments">Validate Arguments</h3>
<pre><code class="language-vbnet">Sub Main()
Dim cmdLine As String
cmdLine = Command()
If cmdLine <> "" Then
If Not ValidateCommandLine(cmdLine) Then
MsgBox "Invalid command line arguments", vbCritical
End
End If
End If
End Sub</code></pre>
<h3 id="use-sub-main-for-command-line-apps">Use <code>Sub Main()</code> for Command Line Apps</h3>
<pre><code class="language-vbnet">Sub Main()
' Process command line before showing any UI
ProcessCommandLine
' Then show UI or continue processing
Form1.Show
End Sub</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Returns only arguments, not the executable path (use App.Path and App.EXEName instead)</li>
<li>No built-in parsing; returns raw string</li>
<li>Quote handling is not automatic</li>
<li>Limited to approximately 32KB of text on some Windows versions</li>
<li>No standard format for arguments (application must define its own conventions)</li>
<li>Different from C/C++ argv[] which provides separate argument array</li>
</ul>
<h2 id="related-functions-and-properties">Related Functions and Properties</h2>
<ul>
<li><code>App.Path</code>: Returns the path where the application is located</li>
<li><code>App.EXEName</code>: Returns the executable filename without extension</li>
<li><code>Environ</code>: Gets environment variable values</li>
<li><code>Shell</code>: Executes external programs with command lines</li>
</ul>
<h2 id="platform-considerations">Platform Considerations</h2>
<ul>
<li>Windows: Uses <code>GetCommandLine</code> API internally</li>
<li>Command line length limits vary by Windows version</li>
<li>Unicode characters may require special handling</li>
<li>Some special characters may need escaping in batch files</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>