User Tools

Site Tools


5.8.0 Gambas programs with options and arguments

You know how to start programs in the console with program options and arguments from working with Linux. So the first command starts the editor program' gedit', opens the file' set_color. sh' in the home directory and places the cursor in the opened file in the 5th line at position 9:

hans@linux:~$ gedit ./ +5:9
hans@linux:~$ gnome-terminal --geometry 80x20+100+100 -t 'ROOT-TERMINAL' -e Digression - Programs with arguments

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

if [ $# -ne 3 ]		# If the number of arguments passed is not equal to 3, ...
    set 220 20 180 	# then set RGB default values: $1=220(R), $2=20(G) and $3=180(B) ...
  else			# otherwise set R, G and B from the three arguments
let color=$R*256*256+$G*256+$B # Calculate colour value
echo colour value = $color
echo Continue with ENTER...
read dummy

In general:

  • The value $* stands for the number of all program parameters!
  • The first argument $0 contains the name of the started program - this is default.
  • $1 to $k form an argument list of the arguments passed, in which the order also corresponds to a ranking order.
  • The value $# returns the number of arguments passed $1… $k.

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

hans@linux:~$ chmod +x # Das Skript ausführbar machen
hans@linux:~$ ./ 10 20 128 # 3 Argumente
Farbwert = 660608
Weiter mit ENTER...
hans@linux:~$ $HOME/ 0 128 # 2 Argumente
Farbwert = 14423220   (Kommentar: $1=220 , $2=20 , $3=180)
Weiter mit ENTER... Gambas program with arguments

Of course, you can also pass arguments to a Gambas program - which should be executed by the Gambas interpreter:

hans@linux:~$ gbx3 $HOME/ColorSelectBP -- 225 110 60
Anzahl der übergebenen Argumente = 3
0. Agument = ColorSelectBP
1. Agument = 225
2. Agument = 110
3. Agument = 60

As a separator symbol between the Gambas project directory and the arguments,“–” is used, as indicated in the help for the interpreter gbx3, if you interpret <arguments> as a placeholder for a list of arguments separated by spaces:

hans@linux:~$ gbx3 -h
Usage: gbx3 [options] [<project file>] [-- <arguments>] Gambas program with options and arguments

In the following two examples exactly one option k is passed to the interpreter gbx3. The $HOME/E/OSMapA project is started and a Gambas program option s and exactly three Gambas program arguments are passed:

hans@linux:~$ gbx3 -k $HOME/E/OSMapA -- -s n -- 52.7904 11.7533 15
hans@linux:~$ gbx3 -k $HOME/E/OSMapA -- --shape j -- -30 -50 4

The value of the -s or --shape% (j (a) or n (one)) option determines whether or not to display a symbol indicating the geographic start coordinate. A detailed description of the project can be found in? chapter 5.8.2 Projects.

Figure GeoMap with symbol

The use of options for a Gambas program is optional. In the next example, a Gambas project is passed to the interpreter - without using an option - but with three arguments:

hans@linux:~$ gbx3 $HOME/ColorSelectBP -- 225 110 60
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.8/start.txt · Last modified: 30.01.2022 (external edit)

Page Tools