User Tools

Site Tools


k5:k5.8:start

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 ./set_color.sh +5:9
hans@linux:~$ gnome-terminal --geometry 80x20+100+100 -t 'ROOT-TERMINAL' -e command.sh

5.8.0.1 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.

#!/bin/bash
if [ $# -ne 3 ]		# Wenn die Anzahl der übergebenen Argumente ungleich 3 ist, ...
  then
    set 220 20 180 	# dann RGB-Standardwerte setzen: $1=220(R), $2=20(G) und $3=180(B) ...
    R=$1
    G=$2
    B=$3
  else			# sonst R, G und B aus den drei Argumenten setzen
    R=$1
    G=$2
    B=$3
fi
let color=$R*256*256+$G*256+$B # Farbwert berechnen
echo Farbwert = $color
echo Weiter mit 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 set_color.sh # Das Skript ausführbar machen
hans@linux:~$ ./set_color.sh 10 20 128 # 3 Argumente
Farbwert = 660608 
Weiter mit ENTER... 
hans@linux:~$ $HOME/set_color.sh 0 128 # 2 Argumente
Farbwert = 14423220   (Kommentar: $1=220 , $2=20 , $3=180) 
Weiter mit ENTER... 

5.8.0.2 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>] 

5.8.0.3 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.

Map

Figure 5.8.0.3.1: 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 

Download

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: 04.01.2019 by honsek

Page Tools