For flags, the value will be "1" if the flag was given, and "0" otherwise.
Typical header definitions are as follows:
#%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
#% multiple : yesWhile 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
A "guisection" field may be added to each option and flag to specify that the options should appear in multiple tabs in the auto-generated GUI. Any options without a guisection field go into the "Options" tab. For example:
#% guisection: tabnamewould put that option in a tab named tabname.
A "key_desc" field may be added to each option to specify the text that appears in the module's usage help section. For example:
#% key_desc: filenameadded to an input option would create the usage summary [input=filename].
v.in.db --script
g.parser -t somescriptfile
#!/bin/sh # g.parser demo script for shell programing #%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 [ -z "$GISBASE" ] ; then echo "You must be in GRASS GIS to run this program." 1>&2 exit 1 fi if [ "$1" != "@ARGS_PARSED@" ] ; then exec $GISBASE/bin/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
#!/usr/bin/python # g.parser demo script for python programing import os import sys #%Module #% description: g.parser test script (python) #%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 def main(): #add your code here print "" if ( os.getenv('GIS_FLAG_F') == "1" ): print "Flag -f set" else: print "Flag -f not set" #test if parameter present: if ( os.getenv("GIS_OPT_OPTION1") != "" ): print "Value of GIS_OPT_OPTION1: '%s'" % os.getenv('GIS_OPT_OPTION1') print "Value of GIS_OPT_RASTER: '%s'" % os.getenv('GIS_OPT_RASTER') print "Value of GIS_OPT_VECTOR: '%s'" % os.getenv('GIS_OPT_VECTOR') #end of your code if __name__ == "__main__": args = "" for arg in sys.argv: args += arg+" " if !os.getenv("GISBASE"): print >> sys.stderr, "You must be in GRASS GIS to run this program." sys.exit(0) try: if ( sys.argv[1] != "@ARGS_PARSED@" ): os.system("g.parser %s " % (args)) except IndexError: os.system("g.parser %s" % (args)) if sys.argv[1] == "@ARGS_PARSED@": main();
The test.py script will provide following help text:
./test.py --help Description: g.parser test script (python) Usage: test.sh [-f] option=name Flags: -f a flag Parameters: option an option
#!/usr/bin/perl -w use strict; # g.parser demo script #%Module #% description: g.parser test script (perl) #% keywords: keyword1, keyword2 #%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 ( !$ENV{'GISBASE'} ) { printf(STDERR "You must be in GRASS GIS to run this program.\n"); exit 1; } if( $ARGV[0] ne '@ARGS_PARSED@' ){ my $arg = ""; for (my $i=0; $i < @ARGV;$i++) { $arg .= " $ARGV[$i] "; } system("$ENV{GISBASE}/bin/g.parser $0 $arg"); exit; } #add your code here print "\n"; if ( $ENV{'GIS_FLAG_F'} eq "1" ){ print "Flag -f set\n" } else { print "Flag -f not set\n" } printf ("Value of GIS_OPT_option1: '%s'\n", $ENV{'GIS_OPT_OPTION1'}); printf ("Value of GIS_OPT_raster: '%s'\n", $ENV{'GIS_OPT_RASTER'}); printf ("Value of GIS_OPT_vect: '%s'\n", $ENV{'GIS_OPT_VECTOR'}); #end of your code
The test.pl script will provide following help text:
./test.pl --help Description: g.parser test script (perl) Usage: test.sh [-f] option=name Flags: -f a flag Parameters: option an option
Last changed: $Date: 2007/01/31 16:01:52 $