Table of Contents

5.8.2 Projects

This chapter presents three projects that demonstrate the use of options and arguments for Gambas programs in different ways.

Another interesting variant is presented in the' Chapter 2.6 Sharing Gambas programs - installation package' in a well-documented project.

5.8.2.1 Project 1 - Gambas program with 3 arguments

In this project for color selection in a ColorChooser, you can optionally transfer exactly 3 color values (RGB) to the program in order to define an individual start value for the color to be displayed after the program start.

The source code of the Gambas project' ColorSelectA1' is similar to the script in chapter 5.8.0.

' Gambas class file
 
Public Sub Form_Open()
  Dim sArgument As String
  Dim R, G, B As Integer
 
  FMain.Center
  FMain.Resizable = False
 
  FMain.Text = "Programme with 3 arguments (optional)"
 
  If Application.Args.Count <> 4 Then
     Print "Error! At least 1 argument is missing."
     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.SelectedColor = Color.RGB(R, G, B)
 
End ' Form_Open

With Application. Args, Gambas provides an object with all the arguments of the Gambas program that you can use like an array:

In this point, working with the array of arguments in Gambas differs from determining the number of arguments for a bash script in the console!

When called in a console:

hans@linux:~$ gbx3 $HOME/ColorSelectA1 -- 225 110 60

the Gambas interpreter starts the program for color selection with the default color ochre:

Farbauswahl
Figure 5.8.2.1.1: Color selection program - default color via 3 arguments

The next time you call up the program, you receive a note and the program is started with a standard color value (? source code) in this case:

hans@linux:~$ gbx3 $HOME/E/ColorSelectA1 -- 50 120
Hint! At least 1 argument is missing.

5.8.2.2 Project 2 - Gambas program with 3 arguments

The special features of this second project can be described in this way:

The source code reflects the above-mentioned special features:

' Gambas class file
 
Public aArgumente As String[]
 
Public Sub Form_Open()
  FColor.Center
  FColor.Resizable = False
  FColor.Text = "Programme with 3 arguments (optional)"
 
' String array as a copy of the array of all arguments
  aArgumente = Args.All ' aArgumente[0] = ProgrammName
 
  If GetErrorVector(aArgumente).ToString(True) = "[0 0 0 0 0 0 0]" Then
     ColorChooser1.Value = Color.RGB(Val(aArgumente[1]), Val(aArgumente[2]), Val(aArgumente[3]))
  Else
     ErrorAnalysis(GetErrorVector(aArgumente))
  Endif ' Without error ?
 
End ' Form_Open
 
Public Sub ColorChooser1_Change()
  Dim cColor As Integer
 
  cColor = ColorChooser1.SelectedColor ' Synonym for ColorChooser1.Value, DataType: Integer
 
End ' ColorChooser1_Change()
 
Private Function GetErrorVector(aArgs As String[]) As Vector
  Dim vFA As Vector ' vFA → Error-Array
  Dim i As Integer = 1
 
  vFA = New Vector(7, False)
  vFA = [0, 0, 0, 0, 0, 0, 0]
' (1) Determine number of arguments
  If aArgs.Count = 1 Then vFA = [3, 0, 0, 0, 0, 0, 0]
  If aArgs.Count = 2 Then vFA = [2, 0, 0, 0, 0, 0, 0]
  If aArgs.Count = 3 Then vFA = [1, 0, 0, 0, 0, 0, 0]
 
  If aArgs.Count = 4 Then
     For i = 1 To aArgs.Max
       If Not IsInteger(aArgs[i]) Then ' (2) Check whether integer number
          vFA[i] = 1
       Else
          If Val(aArgs[i]) < 0 Or Val(aArgs[i]) > 255 Then ' (3) Test for the interval [0..255]
             vFA[i + 3] = 1
          Endif
       Endif
     Next
  Endif
 
  Return vFA ' Return Error Vector
 
End ' Function Error Vector(..)
 
Private Sub ErrorAnalysis(vVektor As Vector)
  Dim i, j As Integer
 
  Print "ERROR!"
' Differentiated evaluation error vector: element A0 (number of missing arguments)
  If vVektor[0] <> 0 Then
     If vVektor[0] = 1 Then
        Print "It lacks precisely "; vVektor[0]; " Argument!"
        Inc j
     Else
       Print "It lacks precisely "; vVektor[0]; " Arguments!"
       Inc j
     Endif
  Endif
' Evaluation Error Vector: A1-A3 (Which argument cannot be converted to integer?)
  For i = 1 To 3
    If vVektor[i] <> 0 Then
       Print "Das "; i; ". Argument is not an integer."
       Inc j
    Endif
  Next
' Evaluation error vector: A4-A6 (Which argument is not in the interval [0..255]?)
  For i = 4 To 6
    If vVektor[i] <> 0 Then
       Print "Das "; i - 3; ". Argument is not in the interval [0..255]!"
       Inc j
    Endif
  Next
 
  Print "Therefore, 3 standard colour values are used."
  ColorChooser1.Value = Color.RGB(220, 20, 180) ' Start arguments - colour: pink
 
End ' ErrorAnalysis(vVektor As Vector)

Here you can see three calls of the program for color selection in the console and the corresponding program reactions depending on the number and type of arguments passed:

hans@linux:~$ gbx3 $HOME/E/ColorSelectA2 -- 222 133
Hint!
Exactly 1 argument is missing!
Therefore 3 default colour values are used.
hans@linux:~$
hans@linux:~$ gbx3 $HOME/E/ColorSelectA2 -- 22O, 344 1E2
Hint!
The 1st argument is not an integer.
The 3rd argument is not an integer number.
The 2nd argument is not in the interval [0..255]!
Therefore 3 standard colour values are used.
hans@linux:~$
hans@linux:~$ gbx3 $HOME/E/ColorSelectA2 -- "50 120 30" ' Exactly 1 character string is transferred
Hint!
Exactly 2 arguments are missing!
Therefore 3 standard colour values are used.
hans@linux:~$

At these two program starts, the arguments determine the same default color:

hans@linux:~$ gbx3 $HOME/E/ColorSelectA2 -- "50" "120" "30"
hans@linux:~$ gbx3 $HOME/E/ColorSelectA2 -- 50 120 30

5.8.2.3 Project 3 - Gambas program with option and arguments

Project 3 (? Chapter 24.8.1 Projects to demonstrate the component gb. map) allows you to display maps of openstreetmap. org (OSM). To view a map you need

It makes sense, depending on the intended purpose

hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape j -- 52,7904 11,7533 15
hans@linux:~$ gbx3 $HOME/E/OSMapA -- -s n -- 52.7904 11.7533 15

Galapagos
Figure 5.8.2.3.3.1: Galapagos (– –shape j – 0.01 -90.002 7)

The project archive can be found in the download area. If you unpack the archive into the $HOME/E path, you can immediately test the program with these calls in a console:

hans@linux:~$ gbx3 $HOME/E/OSMapA
hans@linux:~$ gbx3 $HOME/E/OSMapA -- -h
hans@linux:~$ gbx3 $HOME/E/OSMapA -- --help
hans@linux:~$ gbx3 $HOME/E/OSMapA -- -v
hans@linux:~$ gbx3 $HOME/E/OSMapA -- -V
hans@linux:~$ gbx3 $HOME/E/OSMapA -- --version

hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape j -- 52,7904 1,0 6
hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape n -- 52.7904 1.0 6
hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape j -- 0 0 3

hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape j -- -30,0 -50,9 3
hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape y -- 52.7904 1.0 6

hans@linux:~$ gbx3 $HOME/E/OSMapA -- -- 52.7904 1.0 6
hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape n
hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape -- 52.7904 1.0 6

hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape j -- 0.01 -90.002 7

hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape j -- 52.7904 331.0 6
hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape j -- 52.7904 12

hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape j -- 52.7904 1.0 1o
hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape j -- 52.7904 1.0 19
hans@linux:~$ gbx3 $HOME/E/OSMapA -- --shape j -- 52.7904 1.0 11.5

hans@linux:~$ gbx3 $HOME/E/OSMapA -- -30,0 -50,9 4
hans@linux:~$ gbx3 $HOME/E/OSMapA -- 30,0 50,9 4

In the project source code, you can see that the error functions in error handling provide numeric values as function values, as shown in the following section of the source code:

Private Function IsDecimalDegree(sDegree As String, sFlag As String) As Integer
  If Upper(sFlag) Not Like "{LAT,LON}" Then Return 1

  If Upper(sFlag) = "LAT" Then
     If IsFloat(sDegree) Then
        If (Val(sDegree) <= -90) Or (Val(sDegree) > 90) Then
           Return 2
        Else
           Return 0
        Endif
     Else
        Return 3
     Endif
  Endif

  If Upper(sFlag) = "LON" Then
     If IsFloat(sDegree) Then
        If (Val(sDegree) <= -180) Or (Val(sDegree) > 180) Then
           Return 4
        Else
           Return 0
        Endif
     Else
        Return 5
     Endif
  Endif

End ' Function IsDecimalDegree(..)

Download