I've developed this some time ago to make sure I really get the parameters exaclty as typed by the user:
Val(getCommand$(-1)) returns the number of params
getCommand$(0) returns the name of the executable/command
getCommand$(1) and further return the params
Code: (Select All)
Function getCommand$ (n%)
$If WIN Then
Static cmd$(100), ccount As Integer
If cmd$(0) = "" Then
Declare Library
Function GetCommandLineA%& ()
End Declare
Dim m As _MEM, ms As String * 1000
a%& = GetCommandLineA: m = _Mem(a%&, Len(ms)): ms = _MemGet(m, m.OFFSET, String * 1000)
ccount = -1: p0% = 1: p% = 1
Do While Asc(ms, p%) > 0
Select Case Mid$(ms, p%, 1)
Case Chr$(34)
qq% = 1 - qq%
Case "'"
q% = 1 - q%
Case " "
If q% = 0 And qq% = 0 Then
ccount = ccount + 1
cmd$(ccount) = _Trim$(Mid$(ms, p0%, p% - p0%))
first$ = Left$(cmd$(ccount), 1): last$ = Right$(cmd$(ccount), 1)
If first$ = last$ _AndAlso InStr("'" + Chr$(34), first$) > 0 Then
cmd$(ccount) = Mid$(cmd$(ccount), 2, Len(cmd$(ccount)) - 2)
End If
If cmd$(ccount) = "" Then ccount = ccount - 1
p0% = p% + 1
End If
Case Else
End Select
p% = p% + 1
Loop
If p% > p0% Then
ccount = ccount + 1
cmd$(ccount) = _Trim$(Mid$(ms, p0%, p% - p0%))
first$ = Left$(cmd$(ccount), 1): last$ = Right$(cmd$(ccount), 1)
If first$ = last$ _AndAlso InStr("'" + Chr$(34), first$) > 0 Then
cmd$(ccount) = Mid$(cmd$(ccount), 2, Len(cmd$(ccount)) - 2)
End If
If cmd$(ccount) = "" Then ccount = ccount - 1
End If
_MemFree m
End If
If n% < 0 Then
getCommand$ = _Trim$(Str$(ccount))
ElseIf n% <= ccount Then
getCommand$ = cmd$(n%)
Else
getCommand$ = ""
End If
$Else
getCommand$ = Command$(n%)
$End If
End Function
Val(getCommand$(-1)) returns the number of params
getCommand$(0) returns the name of the executable/command
getCommand$(1) and further return the params
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience