1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! # `Command` Function
//!
//! Returns the argument portion of the command line used to launch Microsoft Visual Basic or an
//! executable program developed with Visual Basic.
//!
//! ## Syntax
//!
//! ```vb
//! 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 ("").
//!
//! ## Remarks
//!
//! The `Command` 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
//!
//! **Important Characteristics:**
//!
//! - 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
//!
//! ## Command Line Processing
//!
//! When an application is launched with:
//! ```text
//! MyApp.exe /debug file.txt "long filename.doc"
//! ```
//!
//! `Command()` returns:
//! ```text
//! /debug file.txt "long filename.doc"
//! ```
//!
//! ## Examples
//!
//! ### Basic Usage
//!
//! ```vb
//! ' 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
//! ```
//!
//! ### Processing Switches
//!
//! ```vb
//! 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
//! ```
//!
//! ### Opening a File from Command Line
//!
//! ```vb
//! 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
//! ```
//!
//! ## Common Patterns
//!
//! ### Parsing Multiple Arguments
//!
//! ```vb
//! 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
//! ```
//!
//! ### Named Parameters
//!
//! ```vb
//! 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"
//! ```
//!
//! ### Switch Detection
//!
//! ```vb
//! 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 ...
//! ```
//!
//! ### File Association Handler
//!
//! ```vb
//! 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
//! ```
//!
//! ### Configuration File Loading
//!
//! ```vb
//! Sub Main()
//! Dim configFile As String
//!
//! configFile = GetParameter("config")
//!
//! If configFile = "" Then
//! configFile = App.Path & "\default.cfg"
//! End If
//!
//! LoadConfiguration configFile
//! End Sub
//! ```
//!
//! ### Debug Mode Activation
//!
//! ```vb
//! 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
//! ```
//!
//! ### Multiple File Processing
//!
//! ```vb
//! 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
//! ```
//!
//! ### Automation Mode
//!
//! ```vb
//! 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
//! ```
//!
//! ## Advanced Usage
//!
//! ### Complex Argument Parser
//!
//! ```vb
//! 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
//! ```
//!
//! ### Environment Variable Expansion
//!
//! ```vb
//! 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
//! ```
//!
//! ### Help Text Display
//!
//! ```vb
//! 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
//! ```
//!
//! ## Error Handling
//!
//! ```vb
//! Function SafeGetCommand() As String
//! On Error GoTo ErrorHandler
//!
//! SafeGetCommand = Command()
//! Exit Function
//!
//! ErrorHandler:
//! ' Command() rarely fails, but handle just in case
//! SafeGetCommand = ""
//! End Function
//! ```
//!
//! ## Performance Considerations
//!
//! - `Command()` is a fast function with minimal overhead
//! - Result is cached, so multiple calls don't re-query the OS
//! - Consider caching the result in a module-level variable if used frequently
//! - Parsing complex command lines can be expensive; cache parsed results
//!
//! ## Best Practices
//!
//! ### Cache the Result
//!
//! ```vb
//! 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
//! ```
//!
//! ### Validate Arguments
//!
//! ```vb
//! 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
//! ```
//!
//! ### Use `Sub Main()` for Command Line Apps
//!
//! ```vb
//! Sub Main()
//! ' Process command line before showing any UI
//! ProcessCommandLine
//!
//! ' Then show UI or continue processing
//! Form1.Show
//! End Sub
//! ```
//!
//! ## Limitations
//!
//! - Returns only arguments, not the executable path (use App.Path and App.EXEName instead)
//! - No built-in parsing; returns raw string
//! - Quote handling is not automatic
//! - Limited to approximately 32KB of text on some Windows versions
//! - No standard format for arguments (application must define its own conventions)
//! - Different from C/C++ argv[] which provides separate argument array
//!
//! ## Related Functions and Properties
//!
//! - `App.Path`: Returns the path where the application is located
//! - `App.EXEName`: Returns the executable filename without extension
//! - `Environ`: Gets environment variable values
//! - `Shell`: Executes external programs with command lines
//!
//! ## Platform Considerations
//!
//! - Windows: Uses `GetCommandLine` API internally
//! - Command line length limits vary by Windows version
//! - Unicode characters may require special handling
//! - Some special characters may need escaping in batch files
use cratestate;
use crateVBString;
use VBResult;
/// Implement VB6's `Command$` function.
///
/// Returns the command-line arguments as a `String`.