Command line parameters
Command line parameters
Does anyone know how to get the parameters entered on the command line into a program when a FreeBASIC program is run from a shell or similar source? For example: "MyProgram.exe parm=parameter" entered in a Windows CMD shell. I would like to be able to use the parameter in the basic program.
Re: Command line parameters
KLovell wrote:"MyProgram.exe parm=parameter" entered in a Windows CMD shell.
Code: Select all
Dim As String parameter1
Dim As String sub_parameter11
Dim As String sub_parameter12
parameter1 = Command(1)
sub_parameter11 = Left(parameter1, Instr(Parameter1, "=") - 1)
sub_parameter12 = Mid(parameter1, Instr(Parameter1, "=") + 1)
Print parameter1
Print sub_parameter11
Print sub_parameter12
Sleep
parm=parameter
parm
parameter
Re: Command line parameters
A more dynamic approach, to command line parsing ...
But first, some general advice:
don't use "param=whatever" (just give the parameters 'value' instead)
parameter-list: 1st, 2nd, 3rd ... (is to be part of documentation or the Help-Screen)
Reason: makes parsing (in the program) that much easier ...
For arguments sake, let's assume, you want to open a Serial-Port:
But first, some general advice:
don't use "param=whatever" (just give the parameters 'value' instead)
parameter-list: 1st, 2nd, 3rd ... (is to be part of documentation or the Help-Screen)
Reason: makes parsing (in the program) that much easier ...
For arguments sake, let's assume, you want to open a Serial-Port:
- NOTE: the first 4 parameters are mandatory:
- 1st = COM-Ports number (1, 2, 3 ...), WIN/DOS, LIN "device-name"
- 2nd = Baud-Rate (300, 600, 1200, 2400 ...)
- 3rd = Parity-Checking method (even, odd, none)
- 4th = Data-Bits (7 or 8) ... we could also do as in 5th, below: assume default = 8
this is the mandatory parts end (all above, must be present) - 5th = Stop-Bit(s) (1 or 2), we simply assume default = 1
- ... (those parameters are purely optional)
Code: Select all
Dim As String t_param, param(Any) ' temp.-string | string array
Dim As Long num = 1 ' start index for Command()
ReDim param(0) ' initialize dynamic array (start size = 1 element)
t_param = Command(num) ' get first parameter
If t_param = "" Then ' if first = 'empty' set ERROR
param(num-1) = "ERROR: no parameter(s) given!"
Else ' no ERROR (aka: OK), store current, get mext
While t_param <> "" ' run until temp.-string = 'empty'
ReDim Preserve param(num-1) ' increase array size (keep data)
param(num-1) = t_param ' assign temporary string to array
num += 1 ' increase param number (to next)
t_param = Command(num) ' get next param (or 'empty')
Wend
End If
' now we have all parameters in the string array: param()
' just in case, we need a certain number of mandatory param's:
If UBound(param) < 3 Then ' assume: min.-parameters = 4
' give user a relevant message and, quit the prog. here
End If
' show it's content to user ... (even if only: ERROR-MSG)
For i As UInteger = 0 To UBound(param)
Print "array param("; i; ") = "; param(i)
Next
Print : Print "press a key --> quit! ";
Sleep
Re: Command line parameters
Thanks for this. It tells me exactly what I needed to know. :-)