g.parser.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  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">
  7. </head>
  8. <body bgcolor="white">
  9. <img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
  10. <h2>NAME</h2>
  11. <em><b>g.parser</b></em>
  12. <h2>SYNOPSIS</h2>
  13. <b>g.parser help</b><br>
  14. <b>g.parser</b> [-<b>s</b>] [-<b>t</b>] <em>filename</em> [<em>argument</em>,...]
  15. <h3>Flags:</h3>
  16. <dl>
  17. <dt><b>-t</b></dt>
  18. <dd>Print strings for translation</dd>
  19. <dt><b>-s</b></dt>
  20. <dd>Write option values to stdout instead of reinvoking script</dd>
  21. </dl>
  22. <h2>DESCRIPTION</h2>
  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.
  27. <h2>OPTIONS</h2>
  28. Unless the <b>-s</b> switch is used, the arguments are stored in
  29. environment variables for use in your scripts. These variables are
  30. named "GIS_FLAG_&lt;NAME&gt;" for flags and "GIS_OPT_&lt;NAME&gt;" for
  31. options. The names of variables are converted to upper case. For
  32. example if an option with key <b>input</b> was defined in the script
  33. header, the value will be available in variable <b>GIS_OPT_INPUT</b>
  34. and the value of flag with key <b>f</b> will be available in variable
  35. <b>GIS_FLAG_F</b>.
  36. <p>For flags, the value will be "1" if the flag was given, and "0" otherwise.
  37. <p>If the <b>-s</b> switch is used, the options and flags are written to
  38. stdout in the form <em>opt_&lt;name&gt;=&lt;value&gt;</em> and
  39. <em>flag_&lt;name&gt;=&lt;value&gt;</em>, preceded by the string
  40. <b>@ARGS_PARSED@</b>. If this string doesn't appear as the first line
  41. of stdout, it indicates that the script was invoked with a switch such
  42. as <b>--html-description</b>. In this case, the data written by
  43. <em>g.parser</em> to stdout should be copied to the script's stdout
  44. verbatim.
  45. <p>Typical header definitions are as follows:
  46. <div class="code"><pre>
  47. #%module
  48. #% description: g.parser test script
  49. #%end
  50. #%flag
  51. #% key: f
  52. #% description: A flag
  53. #%end
  54. #%option
  55. #% key: raster
  56. #% type: string
  57. #% gisprompt: old,cell,raster
  58. #% description: Raster input map
  59. #% required : yes
  60. #%end
  61. </pre></div>
  62. With <tt>{NULL}</tt> it is possible to suppress a predefined <tt>description</tt>
  63. or <tt>label</tt>.
  64. <h2>NOTES</h2>
  65. An option can be instructed to allow multiple inputs by adding the
  66. following line:
  67. <pre>#% multiple : yes</pre>
  68. While this will only directly change the <i>Usage</i> section of the help
  69. screen, the option's environmental string may be easily parsed from within
  70. a script. For example, individual comma separated identities for an option
  71. named "input" can be parsed with the following Bash shell code:
  72. <div class="code"><pre>IFS=,
  73. for opt in $GIS_OPT_INPUT ; do
  74. ... "$opt"
  75. done
  76. </pre></div>
  77. <p>A "<tt>guisection</tt>" field may be added to each option and flag to specify
  78. that the options should appear in multiple tabs in the auto-generated GUI.
  79. Any options without a <tt>guisection</tt> field go into the "Options" tab.
  80. For example:
  81. <pre>#% guisection: tabname</pre>
  82. would put that option in a tab named <i>tabname</i>.
  83. <p>A "<tt>key_desc</tt>" field may be added to each option to specify the text that
  84. appears in the module's usage help section. For example:
  85. <pre>#% key_desc: filename</pre>
  86. added to an <b>input</b> option would create the usage summary
  87. <tt>[input=filename]</tt>.
  88. <p>If a script is run with --o, G_parser() will
  89. set <tt>GRASS_OVERWRITE=1</tt>, which has the same effect as passing
  90. --o to every module which is run from the script. Similarly, passing
  91. --q or --v will set <tt>GRASS_VERBOSE</tt> to 0 or 3 respectively,
  92. which has the same effect as passing --q or --v to every module which
  93. is run from the script. Rather than checking whether --o, --q or --v
  94. were used, you should be checking <tt>GRASS_OVERWRITE</tt> and/or
  95. <tt>GRASS_VERBOSE</tt> instead. If those variables are set, the
  96. script should behave the same way regardless of whether they were set
  97. by --o, --q or --v being passed to the script or set by other means.
  98. <h2>AUTOMATED SCRIPT CREATION</h2>
  99. The flag <b>--script</b> added to a GRASS command, generates shell
  100. output. To write out a <em>g.parser</em> boilerplate for easy
  101. prototyping of shell scripts, the flag <b>--script</b> can be added
  102. to any GRASS command. Example:
  103. <div class="code"><pre>
  104. v.in.db --script
  105. </pre></div>
  106. <h2>Help page template (HTML)</h2>
  107. The flag <b>--html-description</b> added to a GRASS command
  108. generates a related help page template in HTML. Example:
  109. <div class="code"><pre>
  110. v.in.db --html-description
  111. </pre></div>
  112. <h2>GUI window parser (XML)</h2>
  113. The flag <b>--interface-description</b> added to a GRASS command
  114. generates a related help page template in XML. Example:
  115. <div class="code"><pre>
  116. v.in.db --interface-description
  117. </pre></div>
  118. <h2>Web Processing Service (WPS)</h2>
  119. The flag <b>--wps-process-description</b> added to a GRASS command
  120. generates a Web Processing Service process description. Example:
  121. <div class="code"><pre>
  122. v.in.db --wps-process-description
  123. </pre></div>
  124. <h2>reStructuredText (REST)</h2>
  125. The flag <b>--rest-description</b> added to a GRASS command
  126. generates a related help page template in reStructuredText. Example:
  127. <div class="code"><pre>
  128. v.in.db --rest-description
  129. </pre></div>
  130. <h2>TRANSLATION</h2>
  131. <em>g.parser</em> provides some support for translating the options of
  132. scripts. If called with the -t switch before the script filename like
  133. this
  134. <div class="code"><pre>
  135. g.parser -t somescriptfile
  136. </pre></div>
  137. <em>g.parser</em> will print the text of the translatable options to
  138. <tt>stdout</tt>, one per line, and exit. This is for internal use within
  139. the build system to prepare GRASS scripts for translation.
  140. <h2>EXAMPLES</h2>
  141. <h3>Example code for SHELL</h3>
  142. <div class="code"><pre>
  143. #!/bin/sh
  144. # g.parser demo script for shell programing
  145. #%module
  146. #% description: g.parser test script
  147. #%end
  148. #%flag
  149. #% key: f
  150. #% description: A flag
  151. #%end
  152. #%option
  153. #% key: raster
  154. #% type: string
  155. #% gisprompt: old,cell,raster
  156. #% description: Raster input map
  157. #% required : yes
  158. #%end
  159. #%option
  160. #% key: vector
  161. #% type: string
  162. #% gisprompt: old,vector,vector
  163. #% description: Vector input map
  164. #% required : yes
  165. #%end
  166. #%option
  167. #% key: option1
  168. #% type: string
  169. #% description: An option
  170. #% required : no
  171. #%end
  172. if [ -z "$GISBASE" ] ; then
  173. echo "You must be in GRASS GIS to run this program." 1&gt;&amp;2
  174. exit 1
  175. fi
  176. if [ "$1" != "@ARGS_PARSED@" ] ; then
  177. exec g.parser "$0" "$@"
  178. fi
  179. #### add your code below ####
  180. echo ""
  181. if [ $GIS_FLAG_F -eq 1 ] ; then
  182. echo "Flag -f set"
  183. else
  184. echo "Flag -f not set"
  185. fi
  186. # test if parameter present:
  187. if [ -n "$GIS_OPT_OPTION1" ] ; then
  188. echo "Value of GIS_OPT_OPTION1: '$GIS_OPT_OPTION1'"
  189. fi
  190. echo "Value of GIS_OPT_RASTER: '$GIS_OPT_RASTER'"
  191. echo "Value of GIS_OPT_VECTOR: '$GIS_OPT_VECTOR'"
  192. </pre></div>
  193. <h3>Example code for Python</h3>
  194. <div class="code"><pre>
  195. #!/usr/bin/env python
  196. # g.parser demo script for python programing
  197. #%module
  198. #% description: g.parser test script (python)
  199. #%end
  200. #%flag
  201. #% key: f
  202. #% description: A flag
  203. #%end
  204. #%option
  205. #% key: raster
  206. #% type: string
  207. #% gisprompt: old,cell,raster
  208. #% description: Raster input map
  209. #% required : yes
  210. #%end
  211. #%option
  212. #% key: vector
  213. #% type: string
  214. #% gisprompt: old,vector,vector
  215. #% description: Vector input map
  216. #% required : yes
  217. #%end
  218. #%option
  219. #% key: option1
  220. #% type: string
  221. #% description: An option
  222. #% required : no
  223. #%end
  224. import os
  225. import sys
  226. import grass.script as grass
  227. def main():
  228. flag_f = flags['f']
  229. option1 = options['option1']
  230. raster = options['raster']
  231. vector = options['vector']
  232. #### add your code here ####
  233. if flag_f:
  234. print "Flag -f set"
  235. else:
  236. print "Flag -f not set"
  237. # test if parameter present:
  238. if option1:
  239. print "Value of option1= option: '%s'" % option1
  240. print "Value of raster= option: '%s'" % raster
  241. print "Value of vector= option: '%s'" % vector
  242. #### end of your code ####
  243. return 0
  244. if __name__ == "__main__":
  245. options, flags = grass.parser()
  246. main()
  247. </pre></div>
  248. The <tt>test.py</tt> script will provide following help text:
  249. <div class="code"><pre>
  250. ./test.py --help
  251. Description:
  252. g.parser test script (python)
  253. Usage:
  254. test1.py [-f] raster=string vector=string [option1=string]
  255. [--verbose] [--quiet]
  256. Flags:
  257. -f A flag
  258. --v Verbose module output
  259. --q Quiet module output
  260. Parameters:
  261. raster Raster input map
  262. vector Vector input map
  263. option1 An option
  264. </pre></div>
  265. <h3>Example code for Perl</h3>
  266. <div class="code"><pre>
  267. #!/usr/bin/perl -w
  268. use strict;
  269. # g.parser demo script
  270. #%module
  271. #% description: g.parser test script (perl)
  272. #% keywords: keyword1, keyword2
  273. #%end
  274. #%flag
  275. #% key: f
  276. #% description: A flag
  277. #%end
  278. #%option
  279. #% key: raster
  280. #% type: string
  281. #% gisprompt: old,cell,raster
  282. #% description: Raster input map
  283. #% required : yes
  284. #%end
  285. #%option
  286. #% key: vector
  287. #% type: string
  288. #% gisprompt: old,vector,vector
  289. #% description: Vector input map
  290. #% required : yes
  291. #%end
  292. #%option
  293. #% key: option1
  294. #% type: string
  295. #% description: An option
  296. #% required : no
  297. #%end
  298. if ( !$ENV{'GISBASE'} ) {
  299. printf(STDERR "You must be in GRASS GIS to run this program.\n");
  300. exit 1;
  301. }
  302. if( $ARGV[0] ne '@ARGS_PARSED@' ){
  303. my $arg = "";
  304. for (my $i=0; $i < @ARGV;$i++) {
  305. $arg .= " $ARGV[$i] ";
  306. }
  307. system("$ENV{GISBASE}/bin/g.parser $0 $arg");
  308. exit;
  309. }
  310. #### add your code here ####
  311. print "\n";
  312. if ( $ENV{'GIS_FLAG_F'} eq "1" ){
  313. print "Flag -f set\n"
  314. }
  315. else {
  316. print "Flag -f not set\n"
  317. }
  318. printf ("Value of GIS_OPT_option1: '%s'\n", $ENV{'GIS_OPT_OPTION1'});
  319. printf ("Value of GIS_OPT_raster: '%s'\n", $ENV{'GIS_OPT_RASTER'});
  320. printf ("Value of GIS_OPT_vect: '%s'\n", $ENV{'GIS_OPT_VECTOR'});
  321. #### end of your code ####
  322. </pre></div>
  323. The <tt>test.pl</tt> script will provide a GUI and usage help text similar
  324. to the other examples above.
  325. <h2>SEE ALSO</h2>
  326. <em>
  327. <a href="g.filename.html">g.filename</a>,
  328. <a href="g.findfile.html">g.findfile</a>,
  329. <a href="g.tempfile.html">g.tempfile</a>,
  330. </em>
  331. and the <tt>SUBMITTING_SCRIPTS</tt> file in the GRASS source code.
  332. <p>Related Wiki pages:
  333. <a href="http://grasswiki.osgeo.org/wiki/Category:Linking_to_other_languages">Using GRASS with other programming languages</a>
  334. <h2>AUTHOR</h2>
  335. Glynn Clements
  336. <p><i>Last changed: $Date$</i>
  337. </body>
  338. </html>