GRASS logo

NAME

g.parser

DESCRIPTION

The command provides full parser support for GRASS scripts.

OPTIONS

The arguments are stored in environment variables, named GIS_FLAG_<NAME> for flags and GIS_OPT_<NAME> for options. The names of variables are converted to upper case. For example if option with key input was defined in script header the value will be available in variable GIS_OPT_INPUT and value of flag with key f will be available in variable GIS_FLAG_F. For flags, the value will be "1" if the flag was given, and "0" otherwise.

#!/bin/sh

# g.parser demo script

#%Module
#%  description: g.parser test script   
#%End
#%flag
#%  key: f
#%  description: a flag
#%END
#%option
#% key: raster
#% type: string
#% gisprompt: old,cell,raster
#% description: raster input map
#% required : yes
#%end
#%option
#% key: vector
#% type: string
#% gisprompt: old,vector,vector
#% description: vector input map
#% required : yes
#%end
#%option
#% key: option1
#% type: string
#% description: an option
#% required : yes
#%end

if [ "$1" != "@ARGS_PARSED@" ] ; then
  exec $GISBASE/etc/bin/cmd/g.parser "$0" "$@"
fi

#add your code here
echo ""
if [ $GIS_FLAG_F -eq 1 ] ; then
  echo "Flag -f set"
else
  echo "Flag -f not set"
fi

#test if parameter present:
if [ "$GIS_OPT_OPTION1" != "(null)" ] ; then
  echo "Value of GIS_OPT_OPTION1: '$GIS_OPT_OPTION1'"
fi

echo "Value of GIS_OPT_RASTER: '$GIS_OPT_RASTER'"
echo "Value of GIS_OPT_VECTOR: '$GIS_OPT_VECTOR'"

#add your code here

The test.sh script will provide following help text:

./test.sh --help

Description:
 g.parser test script
 
Usage:
 test.sh [-f] option=name
 
Flags:
  -f   a flag
 
Parameters:
  option   an option

NOTES

An option can be instructed to allow multiple inputs by adding the following line:
#% multiple : yes
While this will only directly change the Usage section of the help screen, the option's environmental string may be easily parsed from within a script. For example, individual comma separated identities for an option named "input" can be parsed with the following Bash shell code:
IFS=,
for opt in $GIS_OPT_INPUT ; do
	... "$opt"
done

TRANSLATION

g.parser provides some support for translating the options of scripts. If called with the -t switch before the script filename like this
g.parser -t somescriptfile
g.parser will print the text of the translatable options on standard out, one per line, and exit. This is for use in the build system to prepare for translation.

SEE ALSO

g.findfile

AUTHOR

Glynn Clements

Last changed: $Date$