g.parser.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <!-- meta page name: g.parser -->
  2. <!-- meta page name description: Provides full parser support for GRASS scripts. -->
  3. <h2>KEYWORDS</h2>
  4. <a href="general.html">general</a>, <a href="topic_support.html">support</a>, <a href="keywords.html#scripts">scripts</a>
  5. <h2>SYNOPSIS</h2>
  6. <b>g.parser --help</b><br>
  7. <b>g.parser</b> [-<b>s</b>] [-<b>t</b>] [-<b>n</b>] <em>filename</em> [<em>argument</em>,...]
  8. <h3>Flags:</h3>
  9. <dl>
  10. <dt><b>-t</b></dt>
  11. <dd>Print strings for translation</dd>
  12. <dt><b>-s</b></dt>
  13. <dd>Write option values to standard output instead of reinvoking script</dd>
  14. <dt><b>-n</b></dt>
  15. <dd>Write option values to standard output separated by null character</dd>
  16. </dl>
  17. <h2>DESCRIPTION</h2>
  18. The <em>g.parser</em> module provides full parser support for GRASS
  19. scripts, including an auto-generated GUI interface, help page
  20. template, and command line option checking. In this way a simple
  21. script can very quickly be made into a full-fledged GRASS module.
  22. <h2>OPTIONS</h2>
  23. Unless the <b>-s</b> or <b>-n</b> switch is used, the arguments are stored in
  24. environment variables for use in your scripts. These variables are
  25. named "GIS_FLAG_&lt;NAME&gt;" for flags and "GIS_OPT_&lt;NAME&gt;" for
  26. options. The names of variables are converted to upper case. For
  27. example if an option with key <b>input</b> was defined in the script
  28. header, the value will be available in variable <b>GIS_OPT_INPUT</b>
  29. and the value of flag with key <b>f</b> will be available in variable
  30. <b>GIS_FLAG_F</b>.
  31. <p>
  32. For flags, the value will be "1" if the flag was given, and "0" otherwise.
  33. <p>
  34. If the <b>-s</b> or <b>-n</b> switch is used, the options and flags are written to
  35. standard output in the form <em>opt_&lt;name&gt;=&lt;value&gt;</em> and
  36. <em>flag_&lt;name&gt;=&lt;value&gt;</em>, preceded by the string
  37. <b>@ARGS_PARSED@</b>. If this string doesn't appear as the first line
  38. of standard output, it indicates that the script was invoked with a switch such
  39. as <b>--html-description</b>. In this case, the data written by
  40. <em>g.parser</em> to standard output should be copied to the script's standard output
  41. verbatim.
  42. If the <b>-s</b> switch is used, the options and flags are separated
  43. by newlines. If the <b>-n</b> switch is used, the options and flags
  44. are separated by null characters.
  45. <p>
  46. Typical header definitions are as follows:
  47. <div class="code"><pre>
  48. #%module
  49. #% description: g.parser test script
  50. #%end
  51. #%flag
  52. #% key: f
  53. #% description: A flag
  54. #%end
  55. #%option
  56. #% key: raster
  57. #% type: string
  58. #% gisprompt: old,cell,raster
  59. #% description: Raster input map
  60. #% required: yes
  61. #%end
  62. </pre></div>
  63. With <tt>{NULL}</tt> it is possible to suppress a predefined <tt>description</tt>
  64. or <tt>label</tt>.
  65. <p>
  66. The parsers allows using predefined <em>standardized options and
  67. flags</em>, see the list
  68. 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>
  69. in the programmer manual. Eg. the option
  70. <div class="code"><pre>
  71. #%option
  72. #% key: raster
  73. #% type: string
  74. #% gisprompt: old,cell,raster
  75. #% description: Raster input map
  76. #% required: yes
  77. #%end
  78. </pre></div>
  79. can be easily defined as
  80. <div class="code"><pre>
  81. #%option G_OPT_R_MAP
  82. #% key: raster
  83. #%end
  84. </pre></div>
  85. The parser allows defining predefined <em>rules</em>
  86. for used options.
  87. The syntax of the rules section is following:
  88. <div class="code"><pre>
  89. #%rules
  90. #% exclusive: capfile_output, capfile
  91. #%end
  92. </pre></div>
  93. The parser also allows defining "OR" conditions, e.g. requiring raster
  94. OR vector (for details, see below), e.g.for options:
  95. <div class="code"><pre>
  96. #%rules
  97. #% required: raster, vector
  98. #%end
  99. </pre></div>
  100. and e.g., for flags:
  101. <div class="code"><pre>
  102. #%rules
  103. #% required: -i,-d,-c
  104. #%end
  105. </pre></div>
  106. <h2>NOTES</h2>
  107. An option can be instructed to allow multiple inputs by adding the
  108. following line:
  109. <div class="code"><pre>
  110. #% multiple: yes
  111. </pre></div>
  112. While this will only directly change the <i>Usage</i> section of the help
  113. screen, the option's environmental string may be easily parsed from within
  114. a script. For example, individual comma separated identities for an option
  115. named "input" can be parsed with the following Bash shell code:
  116. <div class="code"><pre>IFS=,
  117. for opt in $GIS_OPT_INPUT ; do
  118. ... "$opt"
  119. done
  120. </pre></div>
  121. <p>
  122. A "<tt>guisection</tt>" field may be added to each option and flag to
  123. specify that the options should appear in multiple tabs in the
  124. auto-generated GUI. Any options without a <tt>guisection</tt> field
  125. go into the "Required" or "Options" tab. For example:
  126. <div class="code"><pre>
  127. #% guisection: tabname
  128. </pre></div>
  129. would put that option in a tab named <i>tabname</i>.
  130. <p>
  131. A "<tt>key_desc</tt>" field may be added to each option to specify the text that
  132. appears in the module's usage help section. For example:
  133. <div class="code"><pre>
  134. #% key_desc: filename
  135. </pre></div>
  136. added to an <b>input</b> option would create the usage summary
  137. <tt>[input=filename]</tt>.
  138. <p>
  139. If a script is run with <b>--o</b>, the parser will
  140. set <tt>GRASS_OVERWRITE=1</tt>, which has the same effect as passing
  141. <b>--o</b> to every module which is run from the script. Similarly, passing
  142. <b>--q</b> or <b>--v</b> will set <tt>GRASS_VERBOSE</tt> to 0 or 3 respectively,
  143. which has the same effect as passing <b>--q</b> or <b>--v</b> to every module which
  144. is run from the script. Rather than checking whether <b>--o</b>, <b>--q</b> or <b>--v</b>
  145. were used, you should be checking <tt>GRASS_OVERWRITE</tt> and/or
  146. <tt>GRASS_VERBOSE</tt> instead. If those variables are set, the script
  147. should behave the same way regardless of whether they were set
  148. by <b>--o</b>, <b>--q</b> or <b>--v</b> being passed to the script or
  149. set by other means.
  150. <h2>Conditional parameters</h2>
  151. Marking an option as "required" will result in the parser raising a
  152. fatal error if the option is not given, with one exception: if a flag
  153. has the <tt>suppress_required</tt> option, and that flag is given, all
  154. requirements are ignored. This feature is intended for flags which
  155. abandon "normal operation" for the module; e.g. <em>r.in.gdal</em>'s
  156. <b>-f</b> flag (list supported formats) uses it.
  157. <br>
  158. But in general, an option cannot be marked as required if it is
  159. optional except for the special case of a <tt>suppress_required</tt> flag.
  160. The parser has the ability to specify option relationships.
  161. <p>
  162. For C, the relevant functions are those in
  163. <a href="http://grass.osgeo.org/programming7/parser__dependencies_8c.html">lib/gis/parser_dependencies.c</a>.
  164. <p>
  165. For scripts, relationships are specified using a "rules" section, e.g.
  166. <div class="code"><pre>
  167. #%rules
  168. #% required: altitude,elevation
  169. #%end
  170. </pre></div>
  171. specifies that at least one of those options must be given. Both
  172. options and flags can be specified (a leading "<b>-</b>" denotes a flag).
  173. The available rule types are:
  174. <ul>
  175. <li> <tt>exclusive</tt>: at most one of the options may be given</li>
  176. <li> <tt>required</tt>: at least one of the options must be given</li>
  177. <li> <tt>requires</tt>: if the first option is given, at least one of the
  178. subsequent options must also be given</li>
  179. <li> <tt>requires_all</tt>: if the first option is given, all of the
  180. subsequent options must also be given</li>
  181. <li> <tt>excludes</tt>: if the first option is given, none of the
  182. subsequent options may be given</li>
  183. <li> <tt>collective</tt>: all or nothing; if any option is given, all
  184. must be given</li>
  185. </ul>
  186. <h2>AUTOMATED SCRIPT CREATION</h2>
  187. The flag <b>--script</b> added to a GRASS command, generates shell
  188. output. To write out a <em>g.parser</em> boilerplate for easy
  189. prototyping of Python scripts, the flag <b>--script</b> can be added
  190. to any GRASS command. Example:
  191. <div class="code"><pre>
  192. v.in.db --script
  193. </pre></div>
  194. <h2>Help page template (HTML)</h2>
  195. The flag <b>--html-description</b> added to a GRASS command
  196. generates a related help page template in HTML. Example:
  197. <div class="code"><pre>
  198. v.in.db --html-description
  199. </pre></div>
  200. <h2>GUI window parser (XML)</h2>
  201. The flag <b>--interface-description</b> added to a GRASS command
  202. generates a related help page template in XML. Example:
  203. <div class="code"><pre>
  204. v.in.db --interface-description
  205. </pre></div>
  206. <h2>Web Processing Service (WPS)</h2>
  207. The flag <b>--wps-process-description</b> added to a GRASS command
  208. generates a Web Processing Service process description. Example:
  209. <div class="code"><pre>
  210. v.in.db --wps-process-description
  211. </pre></div>
  212. <h2>reStructuredText</h2>
  213. The flag <b>--rst-description</b> added to a GRASS command
  214. generates module interface description in reStructuredText, a lightweight
  215. markup language. Example:
  216. <div class="code"><pre>
  217. v.in.db --rst-description
  218. </pre></div>
  219. reStructuredText is sometimes abbreviated as reST, ReST, or RST.
  220. The commonly used file extension is <tt>.rst</tt>.
  221. Don't be confused with Representational State Transfer (REST) technology.
  222. <h2>TRANSLATION</h2>
  223. <em>g.parser</em> provides some support for translating the options of
  224. scripts. If called with the -t switch before the script filename like
  225. this
  226. <div class="code"><pre>
  227. g.parser -t somescriptfile
  228. </pre></div>
  229. <em>g.parser</em> will print the text of the translatable options to
  230. standard output, one per line, and exit. This is for internal use within
  231. the build system to prepare GRASS scripts for translation.
  232. <h2>EXAMPLES</h2>
  233. All examples below autogenerate the graphical user interface when invoked
  234. without parameters of flags:
  235. <p>
  236. <center>
  237. <img src="g_parser_test.png" alt="Autogenerated GUI window">
  238. </center>
  239. <p>
  240. To run properly, the script needs to be copied into a directory listed
  241. in <tt>$GRASS_ADDON_PATH</tt> environmental variable with the
  242. executable flag being set.
  243. <p>
  244. The script will provide a GUI (as above) and the following usage help
  245. text:
  246. <div class="code"><pre>
  247. test.py|sh|pl --help
  248. Description:
  249. g.parser test script (python)
  250. Usage:
  251. test.sh [-f] raster=string vector=string [option1=string]
  252. [--verbose] [--quiet]
  253. Flags:
  254. -f A flag
  255. --v Verbose module output
  256. --q Quiet module output
  257. Parameters:
  258. raster Raster input map
  259. vector Vector input map
  260. option1 An option
  261. </pre></div>
  262. <h3>Example code for Python</h3>
  263. <div class="code"><pre>
  264. #!/usr/bin/env python
  265. # g.parser demo script for python programming
  266. #%module
  267. #% description: g.parser test script (python)
  268. #% keyword: keyword1
  269. #% keyword: keyword2
  270. #%end
  271. #%flag
  272. #% key: f
  273. #% description: A flag
  274. #%end
  275. #%option G_OPT_R_MAP
  276. #% key: raster
  277. #% required: yes
  278. #%end
  279. #%option G_OPT_V_MAP
  280. #% key: vector
  281. #%end
  282. #%option
  283. #% key: option1
  284. #% type: string
  285. #% description: An option
  286. #% required: no
  287. #%end
  288. import os
  289. import sys
  290. import grass.script as grass
  291. def main():
  292. flag_f = flags['f']
  293. option1 = options['option1']
  294. raster = options['raster']
  295. vector = options['vector']
  296. #### add your code here ####
  297. if flag_f:
  298. print "Flag -f set"
  299. else:
  300. print "Flag -f not set"
  301. # test if parameter present:
  302. if option1:
  303. print "Value of option1 option: '%s'" % option1
  304. print "Value of raster option: '%s'" % raster
  305. print "Value of vector option: '%s'" % vector
  306. #### end of your code ####
  307. return 0
  308. if __name__ == "__main__":
  309. options, flags = grass.parser()
  310. sys.exit(main())
  311. </pre></div>
  312. <h3>Example code for SHELL</h3>
  313. <div class="code"><pre>
  314. #!/bin/sh
  315. # g.parser demo script for shell programming
  316. #%module
  317. #% description: g.parser test script (shell)
  318. #%end
  319. #%flag
  320. #% key: f
  321. #% description: A flag
  322. #%end
  323. #%option G_OPT_R_MAP
  324. #% key: raster
  325. #% required: yes
  326. #%end
  327. #%option G_OPT_V_MAP
  328. #% key: vector
  329. #%end
  330. #%option
  331. #% key: option1
  332. #% type: string
  333. #% description: An option
  334. #% required: no
  335. #%end
  336. if [ -z "$GISBASE" ] ; then
  337. echo "You must be in GRASS GIS to run this program." 1&gt;&amp;2
  338. exit 1
  339. fi
  340. if [ "$1" != "@ARGS_PARSED@" ] ; then
  341. exec g.parser "$0" "$@"
  342. fi
  343. #### add your code below ####
  344. echo ""
  345. if [ $GIS_FLAG_F -eq 1 ] ; then
  346. g.message message="Flag -f set"
  347. else
  348. g.message message="Flag -f not set"
  349. fi
  350. # test if parameter present:
  351. if [ -n "$GIS_OPT_OPTION1" ] ; then
  352. echo "Value of GIS_OPT_OPTION1: '$GIS_OPT_OPTION1'"
  353. fi
  354. g.message message="Value of GIS_OPT_option1: '$GIS_OPT_option1'"
  355. g.message message="Value of GIS_OPT_raster: '$GIS_OPT_raster'"
  356. g.message message="Value of GIS_OPT_vect: '$GIS_OPT_vector'"
  357. #### end of your code ####
  358. </pre></div>
  359. <h3>Example code for Perl</h3>
  360. <div class="code"><pre>
  361. #!/usr/bin/perl -w
  362. use strict;
  363. # g.parser demo script
  364. #%module
  365. #% description: g.parser test script (perl)
  366. #% keyword: keyword1
  367. #% keyword: keyword2
  368. #%end
  369. #%flag
  370. #% key: f
  371. #% description: A flag
  372. #%end
  373. #%option G_OPT_R_MAP
  374. #% key: raster
  375. #% required: yes
  376. #%end
  377. #%option G_OPT_V_MAP
  378. #% key: vector
  379. #%end
  380. #%option
  381. #% key: option1
  382. #% type: string
  383. #% description: An option
  384. #% required: no
  385. #%end
  386. if ( !$ENV{'GISBASE'} ) {
  387. printf(STDERR "You must be in GRASS GIS to run this program.\n");
  388. exit 1;
  389. }
  390. if( $ARGV[0] ne '@ARGS_PARSED@' ){
  391. my $arg = "";
  392. for (my $i=0; $i < @ARGV;$i++) {
  393. $arg .= " $ARGV[$i] ";
  394. }
  395. system("$ENV{GISBASE}/bin/g.parser $0 $arg");
  396. exit;
  397. }
  398. #### add your code here ####
  399. print "\n";
  400. if ( $ENV{'GIS_FLAG_F'} eq "1" ){
  401. print "Flag -f set\n"
  402. }
  403. else {
  404. print "Flag -f not set\n"
  405. }
  406. printf ("Value of GIS_OPT_option1: '%s'\n", $ENV{'GIS_OPT_OPTION1'});
  407. printf ("Value of GIS_OPT_raster: '%s'\n", $ENV{'GIS_OPT_RASTER'});
  408. printf ("Value of GIS_OPT_vect: '%s'\n", $ENV{'GIS_OPT_VECTOR'});
  409. #### end of your code ####
  410. </pre></div>
  411. <h3>Easy creation of a script</h3>
  412. By using the <b>--script</b> flag with any GRASS GIS module (must be run in
  413. a GRASS GIS session) header, description, keywords, parameters, flags and
  414. a template main Python script section will be printed in the terminal which
  415. can be saved to a file and used for further script programming.
  416. <p>
  417. In this example, the module <em>v.what.rast</em> is used as an example.
  418. The output is shown below:
  419. <div class="code"><pre>
  420. v.what.rast --script
  421. #!/usr/bin/env python
  422. ############################################################################
  423. #
  424. # MODULE: v.what.rast_wrapper
  425. # AUTHOR(S): username
  426. # PURPOSE: Wrapper for v.what.rast
  427. # COPYRIGHT: (C) 2017 by username, and the GRASS Development Team
  428. #
  429. # This program is free software; you can redistribute it and/or modify
  430. # it under the terms of the GNU General Public License as published by
  431. # the Free Software Foundation; either version 2 of the License, or
  432. # (at your option) any later version.
  433. #
  434. # This program is distributed in the hope that it will be useful,
  435. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  436. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  437. # GNU General Public License for more details.
  438. #
  439. ############################################################################
  440. #%module
  441. #% description: Uploads raster values at positions of vector points to the table.
  442. #% keyword: vector, sampling, raster, position, querying, attribute table, surface information
  443. #%end
  444. #%flag
  445. #% key: i
  446. #% description: Interpolate values from the nearest four cells
  447. #%end
  448. #%flag
  449. #% key: p
  450. #% description: Print categories and values instead of updating the database
  451. #%end
  452. #%option
  453. #% key: map
  454. #% type: string
  455. #% required: yes
  456. #% multiple: no
  457. #% key_desc: name
  458. #% label: Name of vector points map for which to edit attributes
  459. #% description: Or data source for direct OGR access
  460. #% gisprompt: old,vector,vector
  461. #%end
  462. #%option
  463. #% key: layer
  464. #% type: string
  465. #% required: no
  466. #% multiple: no
  467. #% label: Layer number or name
  468. #% description: Vector features can have category values in different layers. This number determines which layer to use. When used with direct OGR access this is the layer name.
  469. #% answer: 1
  470. #% gisprompt: old,layer,layer
  471. #%end
  472. #%option
  473. #% key: type
  474. #% type: string
  475. #% required: no
  476. #% multiple: yes
  477. #% options: point,centroid
  478. #% description: Input feature type
  479. #% answer: point
  480. #%end
  481. #%option
  482. #% key: raster
  483. #% type: string
  484. #% required: yes
  485. #% multiple: no
  486. #% key_desc: name
  487. #% description: Name of existing raster map to be queried
  488. #% gisprompt: old,cell,raster
  489. #%end
  490. #%option
  491. #% key: column
  492. #% type: string
  493. #% required: no
  494. #% multiple: no
  495. #% key_desc: name
  496. #% description: Name of attribute column to be updated with the query result
  497. #% gisprompt: old,dbcolumn,dbcolumn
  498. #%end
  499. #%option
  500. #% key: where
  501. #% type: string
  502. #% required: no
  503. #% multiple: no
  504. #% key_desc: sql_query
  505. #% label: WHERE conditions of SQL statement without 'where' keyword
  506. #% description: Example: income < 1000 and population >= 10000
  507. #%end
  508. import sys
  509. import grass.script as grass
  510. def main():
  511. # put code here
  512. return 0
  513. if __name__ == "__main__":
  514. options, flags = grass.parser()
  515. sys.exit(main())
  516. </pre></div>
  517. <h2>SEE ALSO</h2>
  518. <em>
  519. <a href="g.filename.html">g.filename</a>,
  520. <a href="g.findfile.html">g.findfile</a>,
  521. <a href="g.tempfile.html">g.tempfile</a>
  522. </em>
  523. <p>
  524. Overview table: <a href="parser_standard_options.html">Parser standard options</a>
  525. <p>
  526. <a href="http://trac.osgeo.org/grass/wiki/Submitting/Python">Submitting rules for Python</a>
  527. <p>
  528. Related Wiki pages:
  529. <a href="http://grasswiki.osgeo.org/wiki/Category:Linking_to_other_languages">Using GRASS GIS with other programming languages</a>
  530. <h2>AUTHOR</h2>
  531. Glynn Clements
  532. <p>
  533. <i>Last changed: $Date$</i>