1
<!-- meta page name: g.parser -->
2
<!-- meta page name description: Provides full parser support for GRASS scripts. -->
5
<a href="general.html">general</a>, <a href="topic_support.html">support</a>, <a href="keywords.html#scripts">scripts</a>
8
<b>g.parser --help</b><br>
9
<b>g.parser</b> [-<b>s</b>] [-<b>t</b>] [-<b>n</b>] <em>filename</em> [<em>argument</em>,...]
14
<dd>Print strings for translation</dd>
16
<dd>Write option values to standard output instead of reinvoking script</dd>
18
<dd>Write option values to standard output separated by null character</dd>
23
The <em>g.parser</em> module provides full parser support for GRASS
24
scripts, including an auto-generated GUI interface, help page
25
template, and command line option checking. In this way a simple
26
script can very quickly be made into a full-fledged GRASS module.
31
Unless the <b>-s</b> or <b>-n</b> switch is used, the arguments are stored in
32
environment variables for use in your scripts. These variables are
33
named "GIS_FLAG_<NAME>" for flags and "GIS_OPT_<NAME>" for
34
options. The names of variables are converted to upper case. For
35
example if an option with key <b>input</b> was defined in the script
36
header, the value will be available in variable <b>GIS_OPT_INPUT</b>
37
and the value of flag with key <b>f</b> will be available in variable
41
For flags, the value will be "1" if the flag was given, and "0" otherwise.
44
If the <b>-s</b> or <b>-n</b> switch is used, the options and flags are written to
45
standard output in the form <em>opt_<name>=<value></em> and
46
<em>flag_<name>=<value></em>, preceded by the string
47
<b>@ARGS_PARSED@</b>. If this string doesn't appear as the first line
48
of standard output, it indicates that the script was invoked with a switch such
49
as <b>--html-description</b>. In this case, the data written by
50
<em>g.parser</em> to standard output should be copied to the script's standard output
53
If the <b>-s</b> switch is used, the options and flags are separated
54
by newlines. If the <b>-n</b> switch is used, the options and flags
55
are separated by null characters.
58
Typical header definitions are as follows:
60
<div class="code"><pre>
62
#% description: g.parser test script
66
#% description: A flag
71
#% gisprompt: old,cell,raster
72
#% description: Raster input map
77
With <tt>{NULL}</tt> it is possible to suppress a predefined <tt>description</tt>
81
The parsers allows using predefined <em>standardized options and
82
flags</em>, see the list
83
of <a href="http://grass.osgeo.org/programming7/parser__standard__options_8c.html#a1a5da9db1229a9bbc59d16ae84540bb8">options</a> and <a href="http://grass.osgeo.org/programming7/parser__standard__options_8c.html#ad081e95e5d4dc3daab9c820d962e6902">flags</a>
84
in the programmer manual. Eg. the option
86
<div class="code"><pre>
90
#% gisprompt: old,cell,raster
91
#% description: Raster input map
96
can be easily defined as
98
<div class="code"><pre>
104
The parser allows defining predefined <em>rules</em>
107
The syntax of the rules section is following:
109
<div class="code"><pre>
111
#% exclusive: capfile_output, capfile
115
The parser also allows defining "OR" conditions, e.g. requiring raster
116
OR vector (for details, see below), e.g.for options:
118
<div class="code"><pre>
120
#% required: raster, vector
126
<div class="code"><pre>
128
#% required: -i,-d,-c
134
An option can be instructed to allow multiple inputs by adding the
136
<div class="code"><pre>
140
While this will only directly change the <i>Usage</i> section of the help
141
screen, the option's environmental string may be easily parsed from within
142
a script. For example, individual comma separated identities for an option
143
named "input" can be parsed with the following Bash shell code:
145
<div class="code"><pre>IFS=,
146
for opt in $GIS_OPT_INPUT ; do
152
A "<tt>guisection</tt>" field may be added to each option and flag to
153
specify that the options should appear in multiple tabs in the
154
auto-generated GUI. Any options without a <tt>guisection</tt> field
155
go into the "Required" or "Options" tab. For example:
156
<div class="code"><pre>
157
#% guisection: tabname
159
would put that option in a tab named <i>tabname</i>.
162
A "<tt>key_desc</tt>" field may be added to each option to specify the text that
163
appears in the module's usage help section. For example:
164
<div class="code"><pre>
165
#% key_desc: filename
167
added to an <b>input</b> option would create the usage summary
168
<tt>[input=filename]</tt>.
171
If a script is run with <b>--o</b>, the parser will
172
set <tt>GRASS_OVERWRITE=1</tt>, which has the same effect as passing
173
<b>--o</b> to every module which is run from the script. Similarly, passing
174
<b>--q</b> or <b>--v</b> will set <tt>GRASS_VERBOSE</tt> to 0 or 3 respectively,
175
which has the same effect as passing <b>--q</b> or <b>--v</b> to every module which
176
is run from the script. Rather than checking whether <b>--o</b>, <b>--q</b> or <b>--v</b>
177
were used, you should be checking <tt>GRASS_OVERWRITE</tt> and/or
178
<tt>GRASS_VERBOSE</tt> instead. If those variables are set, the script
179
should behave the same way regardless of whether they were set
180
by <b>--o</b>, <b>--q</b> or <b>--v</b> being passed to the script or
183
<h2>Conditional parameters</h2>
185
Marking an option as "required" will result in the parser raising a
186
fatal error if the option is not given, with one exception: if a flag
187
has the <tt>suppress_required</tt> option, and that flag is given, all
188
requirements are ignored. This feature is intended for flags which
189
abandon "normal operation" for the module; e.g. <em>r.in.gdal</em>'s
190
<b>-f</b> flag (list supported formats) uses it.
192
But in general, an option cannot be marked as required if it is
193
optional except for the special case of a <tt>suppress_required</tt> flag.
194
The parser has the ability to specify option relationships.
197
For C, the relevant functions are those in
198
<a href="http://grass.osgeo.org/programming7/parser__dependencies_8c.html">lib/gis/parser_dependencies.c</a>.
201
For scripts, relationships are specified using a "rules" section, e.g.
203
<div class="code"><pre>
205
#% required: altitude,elevation
209
specifies that at least one of those options must be given. Both
210
options and flags can be specified (a leading "<b>-</b>" denotes a flag).
212
The available rule types are:
215
<li> <tt>exclusive</tt>: at most one of the options may be given</li>
216
<li> <tt>required</tt>: at least one of the options must be given</li>
217
<li> <tt>requires</tt>: if the first option is given, at least one of the
218
subsequent options must also be given</li>
219
<li> <tt>requires_all</tt>: if the first option is given, all of the
220
subsequent options must also be given</li>
221
<li> <tt>excludes</tt>: if the first option is given, none of the
222
subsequent options may be given</li>
223
<li> <tt>collective</tt>: all or nothing; if any option is given, all
227
<h2>AUTOMATED SCRIPT CREATION</h2>
229
The flag <b>--script</b> added to a GRASS command, generates shell
230
output. To write out a <em>g.parser</em> boilerplate for easy
231
prototyping of shell scripts, the flag <b>--script</b> can be added
232
to any GRASS command. Example:
234
<div class="code"><pre>
239
<h2>Help page template (HTML)</h2>
241
The flag <b>--html-description</b> added to a GRASS command
242
generates a related help page template in HTML. Example:
244
<div class="code"><pre>
245
v.in.db --html-description
249
<h2>GUI window parser (XML)</h2>
251
The flag <b>--interface-description</b> added to a GRASS command
252
generates a related help page template in XML. Example:
254
<div class="code"><pre>
255
v.in.db --interface-description
258
<h2>Web Processing Service (WPS)</h2>
260
The flag <b>--wps-process-description</b> added to a GRASS command
261
generates a Web Processing Service process description. Example:
263
<div class="code"><pre>
264
v.in.db --wps-process-description
267
<h2>reStructuredText</h2>
269
The flag <b>--rst-description</b> added to a GRASS command
270
generates module interface description in reStructuredText, a lightweight
271
markup language. Example:
273
<div class="code"><pre>
274
v.in.db --rst-description
277
reStructuredText is sometimes abbreviated as reST, ReST, or RST.
278
The commonly used file extension is <tt>.rst</tt>.
279
Don't be confused with Representational State Transfer (REST) technology.
283
<em>g.parser</em> provides some support for translating the options of
284
scripts. If called with the -t switch before the script filename like
287
<div class="code"><pre>
288
g.parser -t somescriptfile
291
<em>g.parser</em> will print the text of the translatable options to
292
standard output, one per line, and exit. This is for internal use within
293
the build system to prepare GRASS scripts for translation.
298
All examples below autogenerate the graphical user interface when invoked
299
without parameters of flags:
303
<img src="g_parser_test.png" alt="Autogenerated GUI window">
307
To run properly, the script needs to be copied into a directory listed
308
in <tt>$GRASS_ADDON_PATH</tt> environmental variable with the
309
executable flag being set.
312
The script will provide a GUI (as above) and the following usage help
315
<div class="code"><pre>
319
g.parser test script (python)
322
test.sh [-f] raster=string vector=string [option1=string]
323
[--verbose] [--quiet]
327
--v Verbose module output
328
--q Quiet module output
331
raster Raster input map
332
vector Vector input map
336
<h3>Example code for Python</h3>
338
<div class="code"><pre>
339
#!/usr/bin/env python
341
# g.parser demo script for python programing
344
#% description: g.parser test script (python)
350
#% description: A flag
362
#% description: An option
369
import grass.script as grass
373
option1 = options['option1']
374
raster = options['raster']
375
vector = options['vector']
377
#### add your code here ####
382
print "Flag -f not set"
384
# test if parameter present:
386
print "Value of option1 option: '%s'" % option1
388
print "Value of raster option: '%s'" % raster
389
print "Value of vector option: '%s'" % vector
391
#### end of your code ####
395
if __name__ == "__main__":
396
options, flags = grass.parser()
400
<h3>Example code for SHELL</h3>
402
<div class="code"><pre>
405
# g.parser demo script for shell programing
408
#% description: g.parser test script (shell)
412
#% description: A flag
424
#% description: An option
428
if [ -z "$GISBASE" ] ; then
429
echo "You must be in GRASS GIS to run this program." 1>&2
433
if [ "$1" != "@ARGS_PARSED@" ] ; then
434
exec g.parser "$0" "$@"
437
#### add your code below ####
441
if [ $GIS_FLAG_F -eq 1 ] ; then
442
g.message message="Flag -f set"
444
g.message message="Flag -f not set"
447
# test if parameter present:
448
if [ -n "$GIS_OPT_OPTION1" ] ; then
449
echo "Value of GIS_OPT_OPTION1: '$GIS_OPT_OPTION1'"
452
g.message message="Value of GIS_OPT_option1: '$GIS_OPT_option1'"
453
g.message message="Value of GIS_OPT_raster: '$GIS_OPT_raster'"
454
g.message message="Value of GIS_OPT_vect: '$GIS_OPT_vector'"
456
#### end of your code ####
459
<h3>Example code for Perl</h3>
461
<div class="code"><pre>
465
# g.parser demo script
468
#% description: g.parser test script (perl)
474
#% description: A flag
486
#% description: An option
490
if ( !$ENV{'GISBASE'} ) {
491
printf(STDERR "You must be in GRASS GIS to run this program.\n");
495
if( $ARGV[0] ne '@ARGS_PARSED@' ){
497
for (my $i=0; $i < @ARGV;$i++) {
498
$arg .= " $ARGV[$i] ";
500
system("$ENV{GISBASE}/bin/g.parser $0 $arg");
504
#### add your code here ####
506
if ( $ENV{'GIS_FLAG_F'} eq "1" ){
507
print "Flag -f set\n"
510
print "Flag -f not set\n"
513
printf ("Value of GIS_OPT_option1: '%s'\n", $ENV{'GIS_OPT_OPTION1'});
514
printf ("Value of GIS_OPT_raster: '%s'\n", $ENV{'GIS_OPT_RASTER'});
515
printf ("Value of GIS_OPT_vect: '%s'\n", $ENV{'GIS_OPT_VECTOR'});
517
#### end of your code ####
524
<a href="g.filename.html">g.filename</a>,
525
<a href="g.findfile.html">g.findfile</a>,
526
<a href="g.tempfile.html">g.tempfile</a>
530
<a href="http://trac.osgeo.org/grass/wiki/Submitting/Python">Submitting rules for Python</a>
534
<a href="http://grasswiki.osgeo.org/wiki/Category:Linking_to_other_languages">Using GRASS GIS with other programming languages</a>
541
<i>Last changed: $Date: 2015-01-25 18:56:33 +0100 (Sun, 25 Jan 2015) $</i>