Application Arguments

BlitzMax Forums/BlitzMax Programming/Application Arguments

Retimer(Posted 2008) [#1]
I'm curious as to how I can run my app with a command-line argument and get the value of the argument in code.

Is this possible in blitzmax?


Ex:
TestApp.exe -intro 0


In code I would need to get the value of intro, and ensure that the introduction to my app is disabled. I know I can just throw a config file together but that is not what i'm looking for.

Any assistance is appreciated

Time


SebHoll(Posted 2008) [#2]
Look in the docs for the AppArgs$[] string array in the BRL.Blitz module. I've pasted it here for you to see:

' appargs.bmx
' print the command line arguments passed to the program at runtime

Print "Number of arguments = "+AppArgs.length

For a$=EachIn AppArgs
Print a$
Next


Edit: Plash - beat you by 49 seconds.... ;-)


plash(Posted 2008) [#3]
AppArgs:String[]; The docs should give you plenty information.

Example taken from docs.
' appargs.bmx
' print the command line arguments passed to the program at runtime

Print "Number of arguments = "+AppArgs.length

For a$=EachIn AppArgs
Print a$
Next


EDIT: lol.


JoshK(Posted 2008) [#4]
You need this:
Import brl.map
Import brl.retro



Private

Function ParseAppArgs()
	For n=1 To AppArgs.length-1
		indicator$=Left(AppArgs[n],1)
		key$=Lower(Right(AppArgs[n],AppArgs[n].length-1))
		Select indicator$
			Case "+"
				n:+1
				If n>=AppArgs.length Exit
				value$=AppArgs[n]
				AppSettings.insert key,value
			Case "-"
				AppSettings.insert key,"1"
		EndSelect
	Next
EndFunction

Public

Global AppSettings:TMap=New TMap

ParseAppArgs()
LoadSettings()

Rem
bbdoc:
EndRem
Function AppSetting$(key$,defaultvalue$="")
	If key="" Return
	key=Lower(key)
	value$=String(AppSettings.valueforkey(key$))
	If value=""
		Return defaultvalue
	Else
		Return value
	EndIf
EndFunction

Rem
bbdoc:
EndRem
Function SetAppSetting$(key$,value$)
	key=Lower(key)
	If value="" value=Null
	AppSettings.insert key,value
EndFunction

Rem
bbdoc:
EndRem
Function LoadSettings()
	Local stream:TStream
	Local s$,key$,value$
	Local keypair$[]
	stream=ReadFile(StripExt(AppFile)+".cfg" )
	If Not stream Return
	While Not stream.Eof()
		s=stream.ReadLine()
		keypair=s.split("=")
		key=Lower(Trim(keypair[0]))
		value=Replace(Trim(keypair[1]),"~q","")
		If keypair.length=2 AppSettings.insert key,value
	Wend
	stream.close()
EndFunction

Rem
bbdoc:
EndRem
Function SaveSettings()
	Local stream:TStream
	Local key$
	stream=WriteFile(StripExt(AppFile)+".cfg" )
	If Not stream Return
	For key=EachIn AppSettings.keys()
		stream.WriteLine key+"=~q"+String(AppSettings.valueforkey(key))+"~q"
	Next
	stream.close()
EndFunction



Retimer(Posted 2008) [#5]
Ahh I missed that in the docs =/ many thanks. Looks like it's a lot easier than I was expecting!