Command line parameters

General FreeBASIC programming questions.
KLovell
Posts: 15
Joined: Sep 28, 2015 7:10

Command line parameters

Postby KLovell » Oct 26, 2017 8:14

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.
fxm
Posts: 9012
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Command line parameters

Postby fxm » Oct 26, 2017 9:14

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
MrSwiss
Posts: 3125
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Command line parameters

Postby MrSwiss » Oct 26, 2017 16:33

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:
    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)
Parse a "unknown" amount of parameters:

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
KLovell
Posts: 15
Joined: Sep 28, 2015 7:10

Re: Command line parameters

Postby KLovell » Nov 21, 2017 8:14

Thanks for this. It tells me exactly what I needed to know. :-)

Return to “General”

Who is online

Users browsing this forum: Google [Bot] and 4 guests