Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Command$ issues
#3
I've developed this some time ago to make sure I really get the parameters exaclty as typed by the user:

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
Reply


Messages In This Thread
Command$ issues - by eoredson - 04-09-2025, 04:23 AM
RE: Command$ issues - by DSMan195276 - 04-09-2025, 04:53 AM
RE: Command$ issues - by mdijkens - 04-09-2025, 02:53 PM
RE: Command$ issues - by hsiangch_ong - 04-09-2025, 03:15 PM
RE: Command$ issues - by eoredson - 04-09-2025, 09:59 PM
RE: Command$ issues - by eoredson - 04-14-2025, 02:03 AM
RE: Command$ issues - by mdijkens - 04-14-2025, 08:18 AM
RE: Command$ issues - by eoredson - 04-14-2025, 11:12 PM
RE: Command$ issues - by eoredson - 05-02-2025, 01:42 AM



Users browsing this thread: 1 Guest(s)