User Tools

Site Tools


k5:k5.3:k5.3.3:start

5.3.3 Gambas programs with parameters

You are sure to know how to start programs with parameters in the console. The following command starts the editor program' gedit', opens the file' set_color. sh' in the home directory and places the cursor in line 5 at the 9th position:

hans@linux:~$ gedit ./set_color.sh +5:9

5.3.3.1 Excursion - Programs with parameters

The function of the presented program (bash script)' set_color. sh' consists of assigning values internally to the parameters $1 to $3 via the command' let' 3, depending on the number of parameters passed, and then assigning values to the variables R, G and B or the variables R, G and B with the values of the exactly 3 passed parameters. A (colour) value is then calculated and displayed with the variables R, G and B.

#!/bin/bash
#
if [ $# -ne 3 ]		# If the number of parameters is not equal to 3, then ...
  then
    set 220 20 180 	# → $1=220 , $2=20 , $3=180
    R=$1
    G=$2
    B=$3
  else
    R=$1
    G=$2
    B=$3
fi
let color=$R*256*256+$G*256+$B
echo colour value = $color
echo Continue with ENTER...
read dummy

In general:

  • $1 to $k form a parameter list of the passed parameters, in which the order also corresponds to a ranking order.
  • The first argument $0 contains the name of the started program.
  • The value of $# returns the number of parameters $1… $k passed.

The script' set_color. sh' is started first with exactly three parameters and then only with two parameters. The parameters are not checked for validity in the context of RGB color values:

hans@linux:~$ chmod +x set_color.sh # Make the script executable
hans@linux:~$ ./set_color.sh 10 20 128
Colour value = 660608
Continue with ENTER...

hans@linux:~$ $HOME/set_color.sh 0 128
Colour value = 14423220
Continue with ENTER...

5.3.3.2 Gambas programs with parameters

Of course, you can also start a Gambas program with parameters in a console:

hans@linux:~$ gbx3 $HOME/color_select -- 225 110 60
hans@linux:~$ gbx3 ./color_select -- 225 110 60
hans@linux:~$ gbr3 $HOME/color_select/color_select.gambas -- 225 110 60

As a separator between the Gambas project path and the arguments,“–” is used. This is also indicated in the help of gbx3: Usage: gbx3[options][<project file>] [– <arguments>]/, if <arguments> is interpreted as a placeholder for a parameter list separated by blanks.

With Application. Args, Gambas provides an array with the given arguments of the program from the console. The first argument Application. Args[0] is (? $0) is always the name of the started Gambas program and the property Application. Args. Count returns the number of all - not only the passed - parameters. In this point, working with the array of arguments in Gambas differs from determining the number of arguments in a bash script, because the source code of the Gambas project' color_select' is similar to the above-mentioned bash script. With the values of the 3 parameters - which are not checked in this variant - a color value is calculated, which serves as default value for the used component ColorChooser1:

Public Sub Form_Open()
 
  Dim R, G, B As Integer
 
  FColor.Center
  FColor.Resizable = False
 
  If Application.Args.Count <> 4 Then
     R = 220
     G = 20
     B = 180
  Else
     R = Val(Application.Args[1])
     G = Val(Application.Args[2])
     B = Val(Application.Args[3])
  Endif '  Application.Args.Count <> 4 ?
 
  ColorChooser1.Value = Color.RGB(R, G, B)
 
End ' Form_Open

The call:

hans@linux:~$ gbx3 ./color_select -- 33 133 33

starts the program for color selection with the default color medium-green:

Color
Figure 5.3.3.2.1: Color selection program - default color via 3 parameters

The following modified section of the source code contains a check of the parameter values:

Public Sub Form_Open()
  Dim sMessage As String
 
  FColor.Center
  FColor.Resizable = False
 
  If Application.Args.Count <> 4 Then
     ColorChooser1.Value = Color.RGB(220, 20, 180) ' Colour: pink
  Else
     If ParameterTest(Application.Args[1], Application.Args[2], Application.Args[3]) = True Then
        ColorChooser1.Value = Color.RGB(Val(Application.Args[1]), Val(Application.Args[2]),
        Val(Application.Args[3]))
     Else
        sMessage = "At least one parameter has a wrong value!"
        sMessage &= gb.NewLine
        sMessage &= "Parameterliste: " & Application.Args[1] & Chr(32) & Application.Args[2] & Chr(32) &
                    Application.Args[3]
        sMessage &= gb.NewLine
        sMessage &= "The programme will be terminated immediately!"
        Message.Error(sMessage)
        FColor.Close
     Endif
  Endif ' Application.Args.Count = 4 ?
 
End ' Form_Open
 
Public Function ParameterTest(p_1 As String, p_2 As String, p_3 As String) As Boolean
  Dim bOK As Boolean
 
  If IsInteger(p_1) = True And IsInteger(p_2) = True And IsInteger(p_3) = True Then
     If (Val(p_1) >= 0) And (Val(p_1) <= 255) Then
        bOK = True
     Else
        bOK = False
     Endif
     If (Val(p_2) >= 0) And (Val(p_2) <= 255) Then
        bOK = True
     Else
        bOK = False
     Endif
     If (Val(p_3) >= 0) And (Val(p_3) <= 255) Then
        bOK = True
     Else
        bOK = False
     Endif
  Else
     bOK = False
  Endif
 
  Return bOK
 
End ' Function ParameterTest(...) As Boolean

This follows a call of the program in the console:

hans@linux:~$ gbx3 ./color_select -- 128 128 294

this error message:

ERROR
Figure 5.3.3.2.2: Error message - 294 ∉ [0…255]

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k5/k5.3/k5.3.3/start.txt · Last modified: 29.01.2022 (external edit)

Page Tools