1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4
<title>g.parser</title>
5
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6
<link rel="stylesheet" href="grassdocs.css" type="text/css">
10
<img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
14
<em><b>g.parser</b></em>
17
<b>g.parser help</b><br>
18
<b>g.parser</b> [-<b>s</b>] [-<b>t</b>] <em>filename</em> [<em>argument</em>,...]
23
<DD>Print strings for translation</DD>
25
<DD>Write option values to stdout instead of reinvoking script</DD>
30
The <em>g.parser</em> module provides full parser support for GRASS
31
scripts, including an auto-generated GUI interface, help page
32
template, and command line option checking. In this way a simple
33
script can very quickly be made into a full-fledged GRASS module.
38
Unless the <b>-s</b> switch is used, the arguments are stored in
39
environment variables for use in your scripts. These variables are
40
named "GIS_FLAG_<NAME>" for flags and "GIS_OPT_<NAME>" for
41
options. The names of variables are converted to upper case. For
42
example if an option with key <b>input</b> was defined in the script
43
header, the value will be available in variable <b>GIS_OPT_INPUT</b>
44
and the value of flag with key <b>f</b> will be available in variable
48
For flags, the value will be "1" if the flag was given, and "0" otherwise.
51
If the <b>-s</b> switch is used, the options and flags are written to
52
stdout in the form <em>opt_<name>=<value></em> and
53
<em>flag_<name>=<value></em>, preceded by the string
54
<b>@ARGS_PARSED@</b>. If this string doesn't appear as the first line
55
of stdout, it indicates that the script was invoked with a switch such
56
as <b>--html-description</b>. In this case, the data written by
57
<em>g.parser</em> to stdout should be copied to the script's stdout
61
Typical header definitions are as follows:
63
<div class="code"><pre>
65
#% description: g.parser test script
69
#% description: A flag
74
#% gisprompt: old,cell,raster
75
#% description: Raster input map
83
An option can be instructed to allow multiple inputs by adding the
85
<pre>#% multiple : yes</pre>
86
While this will only directly change the <i>Usage</i> section of the help
87
screen, the option's environmental string may be easily parsed from within
88
a script. For example, individual comma separated identities for an option
89
named "input" can be parsed with the following Bash shell code:
91
<div class="code"><pre>IFS=,
92
for opt in $GIS_OPT_INPUT ; do
98
A "<tt>guisection</tt>" field may be added to each option and flag to specify
99
that the options should appear in multiple tabs in the auto-generated GUI.
100
Any options without a <tt>guisection</tt> field go into the "Options" tab.
102
<pre>#% guisection: tabname</pre>
103
would put that option in a tab named <i>tabname</i>.
106
A "<tt>key_desc</tt>" field may be added to each option to specify the text that
107
appears in the module's usage help section. For example:
108
<pre>#% key_desc: filename</pre>
109
added to an <b>input</b> option would create the usage summary
110
<tt>[input=filename]</tt>.
113
If a script is run with --o, G_parser() will
114
set <tt>GRASS_OVERWRITE=1</tt>, which has the same effect as passing
115
--o to every module which is run from the script. Similarly, passing
116
--q or --v will set <tt>GRASS_VERBOSE</tt> to 0 or 3 respectively,
117
which has the same effect as passing --q or --v to every module which
118
is run from the script. Rather than checking whether --o, --q or --v
119
were used, you should be checking <tt>GRASS_OVERWRITE</tt> and/or
120
<tt>GRASS_VERBOSE</tt> instead. If those variables are set, the
121
script should behave the same way regardless of whether they were set
122
by --o, --q or --v being passed to the script or set by other means.
125
<h2>AUTOMATED SCRIPT CREATION</h2>
127
The flag <b>--script</b> added to a GRASS command, generates shell
128
output. To write out a <em>g.parser</em> boilerplate for easy
129
prototyping of shell scripts, the flag <b>--script</b> can be added
130
to any GRASS command. Example:
132
<div class="code"><pre>
137
<h2>Help page template (HTML)</h2>
139
The flag <b>--html-description</b> added to a GRASS command
140
generates a related help page template in HTML. Example:
142
<div class="code"><pre>
143
v.in.db --html-description
147
<h2>GUI window parser (XML)</h2>
149
The flag <b>--interface-description</b> added to a GRASS command
150
generates a related help page template in XML. Example:
152
<div class="code"><pre>
153
v.in.db --interface-description
156
<h2>GUI window parser (Tcl/Tk)</h2>
158
The flag <b>--tcltk</b> added to a GRASS command generates Tcl/Tk
159
code suitable for building the GUI interface. Example:
161
<div class="code"><pre>
168
<em>g.parser</em> provides some support for translating the options of
169
scripts. If called with the -t switch before the script filename like
172
<div class="code"><pre>
173
g.parser -t somescriptfile
176
<em>g.parser</em> will print the text of the translatable options to
177
<tt>stdout</tt>, one per line, and exit. This is for internal use within
178
the build system to prepare GRASS scripts for translation.
183
<h3>Example code for SHELL</h3>
185
<div class="code"><pre>
188
# g.parser demo script for shell programing
191
#% description: g.parser test script
195
#% description: A flag
200
#% gisprompt: old,cell,raster
201
#% description: Raster input map
207
#% gisprompt: old,vector,vector
208
#% description: Vector input map
214
#% description: An option
218
if [ -z "$GISBASE" ] ; then
219
echo "You must be in GRASS GIS to run this program." 1>&2
223
if [ "$1" != "@ARGS_PARSED@" ] ; then
224
exec g.parser "$0" "$@"
227
#### add your code below ####
230
if [ $GIS_FLAG_F -eq 1 ] ; then
233
echo "Flag -f not set"
236
# test if parameter present:
237
if [ -n "$GIS_OPT_OPTION1" ] ; then
238
echo "Value of GIS_OPT_OPTION1: '$GIS_OPT_OPTION1'"
241
echo "Value of GIS_OPT_RASTER: '$GIS_OPT_RASTER'"
242
echo "Value of GIS_OPT_VECTOR: '$GIS_OPT_VECTOR'"
247
<h3>Example code for Python</h3>
249
<div class="code"><pre>
250
#!/usr/bin/env python
252
# g.parser demo script for python programing
255
#% description: g.parser test script (python)
259
#% description: A flag
264
#% gisprompt: old,cell,raster
265
#% description: Raster input map
271
#% gisprompt: old,vector,vector
272
#% description: Vector input map
278
#% description: An option
285
import grass.script as grass
289
option1 = options['option1']
290
raster = options['raster']
291
vector = options['vector']
292
#### add your code here ####
297
print "Flag -f not set"
299
# test if parameter present:
301
print "Value of option1= option: '%s'" % option1
303
print "Value of raster= option: '%s'" % raster
304
print "Value of vector= option: '%s'" % vector
306
#### end of your code ####
310
if __name__ == "__main__":
311
options, flags = grass.parser()
315
The <tt>test.py</tt> script will provide following help text:
317
<div class="code"><pre>
321
g.parser test script (python)
324
test1.py [-f] raster=string vector=string [option1=string]
325
[--verbose] [--quiet]
329
--v Verbose module output
330
--q Quiet module output
333
raster Raster input map
334
vector Vector input map
339
<h3>Example code for Perl</h3>
341
<div class="code"><pre>
345
# g.parser demo script
348
#% description: g.parser test script (perl)
349
#% keywords: keyword1, keyword2
353
#% description: A flag
358
#% gisprompt: old,cell,raster
359
#% description: Raster input map
365
#% gisprompt: old,vector,vector
366
#% description: Vector input map
372
#% description: An option
376
if ( !$ENV{'GISBASE'} ) {
377
printf(STDERR "You must be in GRASS GIS to run this program.\n");
382
if( $ARGV[0] ne '@ARGS_PARSED@' ){
384
for (my $i=0; $i < @ARGV;$i++) {
385
$arg .= " $ARGV[$i] ";
387
system("$ENV{GISBASE}/bin/g.parser $0 $arg");
391
#### add your code here ####
393
if ( $ENV{'GIS_FLAG_F'} eq "1" ){
394
print "Flag -f set\n"
397
print "Flag -f not set\n"
400
printf ("Value of GIS_OPT_option1: '%s'\n", $ENV{'GIS_OPT_OPTION1'});
401
printf ("Value of GIS_OPT_raster: '%s'\n", $ENV{'GIS_OPT_RASTER'});
402
printf ("Value of GIS_OPT_vect: '%s'\n", $ENV{'GIS_OPT_VECTOR'});
404
#### end of your code ####
408
The <tt>test.pl</tt> script will provide a GUI and usage help text similar
409
to the other examples above.
415
<a HREF="d.ask.html">d.ask</a>,
416
<a HREF="d.menu.html">d.menu</a>,
417
<a HREF="g.ask.html">g.ask</a>,
418
<a HREF="g.filename.html">g.filename</a>,
419
<a HREF="g.findfile.html">g.findfile</a>,
420
<a HREF="g.tempfile.html">g.tempfile</a>,
423
and the <tt>SUBMITTING_SCRIPTS</tt> file in the GRASS source code.
426
<a href="http://grass.osgeo.org/wiki/Category:Linking_to_other_languages">Using GRASS with other programming languages</a>
433
<i>Last changed: $Date: 2010-03-21 15:26:08 +0100 (Sun, 21 Mar 2010) $</i>