CommandLine$ ( ) ????

BlitzMax Forums/BlitzMax Programming/CommandLine$ ( ) ????

Fabian.(Posted 2005) [#1]
Is there any way to get the command line as string and not as string array?

I need it to get path names correctly.

((I'm on Win32))


FlameDuck(Posted 2005) [#2]
Not AFAIK, but you can patch them all together as a single string with a simple loop if you are so inclined. Isn't it called AppArgs() tho'?. Anyway, here is a working example:
Strict

Local cmdline:String = ""

Local skip:Int = 0 ' Because we're not interested in the first element
For Local i:String = EachIn AppArgs
	If skip > 0
		cmdline:+i+" "
	Else
		skip:+1
	End If
Next

Print cmdline



marksibly(Posted 2005) [#3]

I need it to get path names correctly.



How do you mean?

Command line args are sent to apps as a string array anyway, so the AppArgs$[] array is as 'accurate' as you can get!

Note that AppArgs elements can have spaces in them, in situtations where filenames etc are quoted, eg:

RunApp "My File"

RunApp will get an AppArgs of length 2, where AppArgs[0]="RunApp" and AppArgs[1]="My File" (both without the quotes).


Difference(Posted 2005) [#4]
So RunApp My File

results in AppArgs[0]="RunApp" and AppArgs[1]="My" AppArgs[2]="File"? Maybe that's what Fabi is seeing.


Perturbatio(Posted 2005) [#5]
To provide the old functionality (and to tidy up FD's code a little :) ):
Strict

Print CommandLine()

Function CommandLine$()
	Local CmdLine:String
	
	For Local e:String = EachIn AppArgs[1..] 'start from the second element of the appargs array
		CmdLine:+ e +" " 'add each element to the CmdLine string
	Next
	Return CmdLine
End Function



FlameDuck(Posted 2005) [#6]
For Local e:String = EachIn AppArgs[1..] 'start from the second element of the appargs array
Oh that's so clever. I had no idea you could use slicing like that.


Fabian.(Posted 2005) [#7]
The example and the function from FlameDuck and Perturbatio are good, but work only if the original file name contains only one space, so the user of this code can't different between 'Files\My[space]File.xxx' and 'Files\My[space][space]File.xxx'.

marksibly:
Command line args are sent to apps as a string array anyway, so the AppArgs$[] array is as 'accurate' as you can get!


When you write a C/C++ programm you can get the command line with two ways:

int main( const char*args[], int argslenght ){...}
or
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ){...}

I think it's better to get both, AppArgs$[] and Commandline$().

-----
a little question:
How can I get a code box, spaces or other html-elements to the forum post? ((with 'spaces' I mean more than one space))

thank you!


Mark Tiffany(Posted 2005) [#8]
to answer your little question, use the forum tags <codebox> and </codebox> around your code, replacing <> with square brackets []


Fabian.(Posted 2005) [#9]
thanks a lot, Mark Tiffany !


Perturbatio(Posted 2005) [#10]
Oh that's so clever. I had no idea you could use slicing like that.

Nor did I till I tried it :)
I really am loving slices, they're the best thing since sliced arrays :)


Fabian.(Posted 2005) [#11]
In an Open-Watcom compiler I found this function:

But the MinGW compiler doesn't support this function.
If it would I could use a function like this:
Function CommandLine$ ( )
  Local LineLen = GetCmd ( Null , 0 ) + 1
  Local LinePtr:Byte Ptr = MemAlloc ( LineLen )
  Local Text$

  GetCmd LinePtr , LineLen
  Text = String.FromCString ( LinePtr )
  MemFree LinePtr
  Return Text

  Extern
    Function GetCmd ( LinePtr:Byte Ptr , LineLen ) = "_bgetcmd"
  EndExtern
EndFunction