GLBasic forum

Codesnippets => Code Snippets => Topic started by: SBlectric on 2011-Dec-27

Title: Command Line Seperator function
Post by: SBlectric on 2011-Dec-27
My first post! Woo!  :D

When I call GETCOMMANDLINE$() the commands are separated by either quotes (if they contain spaces) or just by spaces. Here is a function that will separate each one into elements of an array.

Check it out.  =D

Code (glbasic) Select

FUNCTION CmdLine: cmd$,e$[]

LOCAL a%,b%,exit%,pos%,endpos%

cmd$=REPLACE$(cmd$,CHR$(92),"/")

DIM e$[0]

WHILE 1

IF pos>LEN(cmd$)-1 THEN BREAK

SELECT ASC(MID$(cmd$,pos,1))

CASE 34 // if the value is an open quote

b=INSTR(cmd$,CHR$(34),pos+1)
IF b<pos+1
endpos=-1
exit=1
ELSE
endpos=b-1-(pos)
ENDIF

DIMPUSH e$[],MID$(cmd$,pos+1,endpos)
pos=b+1


CASE 32 // if it's a space
INC pos,1


DEFAULT // if it's any other character

b=INSTR(cmd$,CHR$(32),pos)
IF b<pos+1
endpos=-1
exit=1
ELSE
endpos=b-1-(pos-1)
ENDIF

DIMPUSH e$[],MID$(cmd$,pos,endpos)
pos=b


ENDSELECT

IF exit THEN BREAK
WEND

ENDFUNCTION

Title: Re: Command Line Seperator function
Post by: Kitty Hello on 2011-Dec-27
I think a solution already existed?
http://www.glbasic.com/forum/index.php?topic=2066.msg15078#msg15078 (http://www.glbasic.com/forum/index.php?topic=2066.msg15078#msg15078)

and.. hello, welcome to the forums.
Title: Re: Command Line Seperator function
Post by: SBlectric on 2011-Dec-27
Oh. Sorry, didn't see that. Cool!