This gets the full commandline "as is", which can be helpful in times where you just want the commandline as a single string, or when you need to preserve space characters, or need to use speech characters which might otherwise throw off the parser, or dont want to require quotes encapsulation, etc.
The Windows version returns the full result from GetCommandLine(), which is in the form "Executable{space}Commandline", whereas the Linux + OSX versions just return "Commandline", however they also need to read the Executable part in order to get to the Commandline part, so it's very easy to change the result so that they all return "Executable{space}Commandline" (simply return sExePath+" "+sCmdline instead of just sCmdline)
Code: Select all
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Import ""
GetCommandLineW()
EndImport
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
ImportC ""
_NSGetArgv() ;http://www.opensource.apple.com/source/Libc/Libc-167/headers.subproj/crt_externs.h
EndImport
CompilerEndIf
Procedure.s GetCmdLine()
Protected sExePath.s, sCmdline.s, *cmdline
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
*cmdline = GetCommandLineW()
If *cmdline: sCmdline = PeekS(*cmdline,-1,#PB_Unicode): EndIf
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
*cmdline = _NSGetArgv()
If *cmdline
*cmdline = PeekI(PeekI(*cmdline))
sExePath = PeekS(*cmdline,-1,#PB_UTF8)
sCmdline = PeekS(*cmdline+Len(sExePath)+1,-1,#PB_UTF8)
EndIf
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
hFile = ReadFile(#PB_Any, "/proc/self/cmdline")
If hFile
sExePath = ReadString(hFile, #PB_UTF8)
sCmdLine = ReadString(hFile, #PB_UTF8)
EndIf
CompilerEndIf
ProcedureReturn sCmdline
EndProcedure
Debug("Full CmdLine=" + GetCmdLine())
; IDE Options = PureBasic 5.31 (MacOS X - x64)
; CommandLine = MY DUMMY CMDLINE