g.parser.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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>JSON</h2>
  207. The flag <b>--json</b> added to a GRASS command with parameters mandatorily
  208. to be specified generates a module interface description in JSON. Example:
  209. <div class="code"><pre>
  210. v.in.db driver=sqlite database=mysqlite.db table=pointsfile x=x y=y z=z key=idcol out=dtmpoints --json
  211. {
  212. "module": "v.in.db",
  213. "id": "v.in.db_1804289383",
  214. "inputs":[
  215. {"param": "table", "value": "pointsfile"},
  216. {"param": "driver", "value": "sqlite"},
  217. {"param": "database", "value": "mysqlite.db"},
  218. {"param": "x", "value": "x"},
  219. {"param": "y", "value": "y"},
  220. {"param": "z", "value": "z"},
  221. {"param": "key", "value": "idcol"}
  222. ],
  223. "outputs":[
  224. {"param": "output", "value": "dtmpoints"}
  225. ]
  226. }
  227. </pre></div>
  228. <h2>Web Processing Service (WPS)</h2>
  229. The flag <b>--wps-process-description</b> added to a GRASS command
  230. generates a Web Processing Service process description. Example:
  231. <div class="code"><pre>
  232. v.in.db --wps-process-description
  233. </pre></div>
  234. <h2>reStructuredText</h2>
  235. The flag <b>--rst-description</b> added to a GRASS command
  236. generates module interface description in reStructuredText, a lightweight
  237. markup language. Example:
  238. <div class="code"><pre>
  239. v.in.db --rst-description
  240. </pre></div>
  241. reStructuredText is sometimes abbreviated as reST, ReST, or RST.
  242. The commonly used file extension is <tt>.rst</tt>.
  243. Don't be confused with Representational State Transfer (REST) technology.
  244. <h2>TRANSLATION</h2>
  245. <em>g.parser</em> provides some support for translating the options of
  246. scripts. If called with the -t switch before the script filename like
  247. this
  248. <div class="code"><pre>
  249. g.parser -t somescriptfile
  250. </pre></div>
  251. <em>g.parser</em> will print the text of the translatable options to
  252. standard output, one per line, and exit. This is for internal use within
  253. the build system to prepare GRASS scripts for translation.
  254. <h2>EXAMPLES</h2>
  255. All examples below autogenerate the graphical user interface when invoked
  256. without parameters of flags:
  257. <p>
  258. <center>
  259. <img src="g_parser_test.png" alt="Autogenerated GUI window">
  260. </center>
  261. <p>
  262. To run properly, the script needs to be copied into a directory listed
  263. in <tt>$GRASS_ADDON_PATH</tt> environmental variable with the
  264. executable flag being set.
  265. <p>
  266. The script will provide a GUI (as above) and the following usage help
  267. text:
  268. <div class="code"><pre>
  269. test.py|sh|pl --help
  270. Description:
  271. g.parser test script (python)
  272. Usage:
  273. test.sh [-f] raster=string vector=string [option1=string]
  274. [--verbose] [--quiet]
  275. Flags:
  276. -f A flag
  277. --v Verbose module output
  278. --q Quiet module output
  279. Parameters:
  280. raster Raster input map
  281. vector Vector input map
  282. option1 An option
  283. </pre></div>
  284. <h3>Example code for Python</h3>
  285. <div class="code"><pre>
  286. #!/usr/bin/env python3
  287. # g.parser demo script for python programming
  288. #%module
  289. #% description: g.parser test script (python)
  290. #% keyword: keyword1
  291. #% keyword: keyword2
  292. #%end
  293. #%flag
  294. #% key: f
  295. #% description: A flag
  296. #%end
  297. #%option G_OPT_R_MAP
  298. #% key: raster
  299. #% required: yes
  300. #%end
  301. #%option G_OPT_V_MAP
  302. #% key: vector
  303. #%end
  304. #%option
  305. #% key: option1
  306. #% type: string
  307. #% description: An option
  308. #% required: no
  309. #%end
  310. import os
  311. import sys
  312. import grass.script as grass
  313. def main():
  314. flag_f = flags['f']
  315. option1 = options['option1']
  316. raster = options['raster']
  317. vector = options['vector']
  318. #### add your code here ####
  319. if flag_f:
  320. print "Flag -f set"
  321. else:
  322. print "Flag -f not set"
  323. # test if parameter present:
  324. if option1:
  325. print "Value of option1 option: '%s'" % option1
  326. print "Value of raster option: '%s'" % raster
  327. print "Value of vector option: '%s'" % vector
  328. #### end of your code ####
  329. return 0
  330. if __name__ == "__main__":
  331. options, flags = grass.parser()
  332. sys.exit(main())
  333. </pre></div>
  334. <h3>Example code for SHELL</h3>
  335. <div class="code"><pre>
  336. #!/bin/sh
  337. # g.parser demo script for shell programming
  338. #%module
  339. #% description: g.parser test script (shell)
  340. #%end
  341. #%flag
  342. #% key: f
  343. #% description: A flag
  344. #%end
  345. #%option G_OPT_R_MAP
  346. #% key: raster
  347. #% required: yes
  348. #%end
  349. #%option G_OPT_V_MAP
  350. #% key: vector
  351. #%end
  352. #%option
  353. #% key: option1
  354. #% type: string
  355. #% description: An option
  356. #% required: no
  357. #%end
  358. if [ -z "$GISBASE" ] ; then
  359. echo "You must be in GRASS GIS to run this program." 1&gt;&amp;2
  360. exit 1
  361. fi
  362. if [ "$1" != "@ARGS_PARSED@" ] ; then
  363. exec g.parser "$0" "$@"
  364. fi
  365. #### add your code below ####
  366. echo ""
  367. if [ $GIS_FLAG_F -eq 1 ] ; then
  368. g.message message="Flag -f set"
  369. else
  370. g.message message="Flag -f not set"
  371. fi
  372. # test if parameter present:
  373. if [ -n "$GIS_OPT_OPTION1" ] ; then
  374. echo "Value of GIS_OPT_OPTION1: '$GIS_OPT_OPTION1'"
  375. fi
  376. g.message message="Value of GIS_OPT_option1: '$GIS_OPT_option1'"
  377. g.message message="Value of GIS_OPT_raster: '$GIS_OPT_raster'"
  378. g.message message="Value of GIS_OPT_vect: '$GIS_OPT_vector'"
  379. #### end of your code ####
  380. </pre></div>
  381. <h3>Example code for Perl</h3>
  382. <div class="code"><pre>
  383. #!/usr/bin/perl -w
  384. use strict;
  385. # g.parser demo script
  386. #%module
  387. #% description: g.parser test script (perl)
  388. #% keyword: keyword1
  389. #% keyword: keyword2
  390. #%end
  391. #%flag
  392. #% key: f
  393. #% description: A flag
  394. #%end
  395. #%option G_OPT_R_MAP
  396. #% key: raster
  397. #% required: yes
  398. #%end
  399. #%option G_OPT_V_MAP
  400. #% key: vector
  401. #%end
  402. #%option
  403. #% key: option1
  404. #% type: string
  405. #% description: An option
  406. #% required: no
  407. #%end
  408. if ( !$ENV{'GISBASE'} ) {
  409. printf(STDERR "You must be in GRASS GIS to run this program.\n");
  410. exit 1;
  411. }
  412. if( $ARGV[0] ne '@ARGS_PARSED@' ){
  413. my $arg = "";
  414. for (my $i=0; $i < @ARGV;$i++) {
  415. $arg .= " $ARGV[$i] ";
  416. }
  417. system("$ENV{GISBASE}/bin/g.parser $0 $arg");
  418. exit;
  419. }
  420. #### add your code here ####
  421. print "\n";
  422. if ( $ENV{'GIS_FLAG_F'} eq "1" ){
  423. print "Flag -f set\n"
  424. }
  425. else {
  426. print "Flag -f not set\n"
  427. }
  428. printf ("Value of GIS_OPT_option1: '%s'\n", $ENV{'GIS_OPT_OPTION1'});
  429. printf ("Value of GIS_OPT_raster: '%s'\n", $ENV{'GIS_OPT_RASTER'});
  430. printf ("Value of GIS_OPT_vect: '%s'\n", $ENV{'GIS_OPT_VECTOR'});
  431. #### end of your code ####
  432. </pre></div>
  433. <h3>Easy creation of a script</h3>
  434. By using the <b>--script</b> flag with any GRASS GIS module (must be run in
  435. a GRASS GIS session) header, description, keywords, parameters, flags and
  436. a template main Python script section will be printed in the terminal which
  437. can be saved to a file and used for further script programming.
  438. <p>
  439. In this example, the module <em>v.what.rast</em> is used as an example.
  440. The output is shown below:
  441. <div class="code"><pre>
  442. v.what.rast --script
  443. #!/usr/bin/env python3
  444. ############################################################################
  445. #
  446. # MODULE: v.what.rast_wrapper
  447. # AUTHOR(S): username
  448. # PURPOSE: Wrapper for v.what.rast
  449. # COPYRIGHT: (C) 2017 by username, and the GRASS Development Team
  450. #
  451. # This program is free software; you can redistribute it and/or modify
  452. # it under the terms of the GNU General Public License as published by
  453. # the Free Software Foundation; either version 2 of the License, or
  454. # (at your option) any later version.
  455. #
  456. # This program is distributed in the hope that it will be useful,
  457. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  458. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  459. # GNU General Public License for more details.
  460. #
  461. ############################################################################
  462. #%module
  463. #% description: Uploads raster values at positions of vector points to the table.
  464. #% keyword: vector, sampling, raster, position, querying, attribute table, surface information
  465. #%end
  466. #%flag
  467. #% key: i
  468. #% description: Interpolate values from the nearest four cells
  469. #%end
  470. #%flag
  471. #% key: p
  472. #% description: Print categories and values instead of updating the database
  473. #%end
  474. #%option
  475. #% key: map
  476. #% type: string
  477. #% required: yes
  478. #% multiple: no
  479. #% key_desc: name
  480. #% label: Name of vector points map for which to edit attributes
  481. #% description: Or data source for direct OGR access
  482. #% gisprompt: old,vector,vector
  483. #%end
  484. #%option
  485. #% key: layer
  486. #% type: string
  487. #% required: no
  488. #% multiple: no
  489. #% label: Layer number or name
  490. #% 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.
  491. #% answer: 1
  492. #% gisprompt: old,layer,layer
  493. #%end
  494. #%option
  495. #% key: type
  496. #% type: string
  497. #% required: no
  498. #% multiple: yes
  499. #% options: point,centroid
  500. #% description: Input feature type
  501. #% answer: point
  502. #%end
  503. #%option
  504. #% key: raster
  505. #% type: string
  506. #% required: yes
  507. #% multiple: no
  508. #% key_desc: name
  509. #% description: Name of existing raster map to be queried
  510. #% gisprompt: old,cell,raster
  511. #%end
  512. #%option
  513. #% key: column
  514. #% type: string
  515. #% required: no
  516. #% multiple: no
  517. #% key_desc: name
  518. #% description: Name of attribute column to be updated with the query result
  519. #% gisprompt: old,dbcolumn,dbcolumn
  520. #%end
  521. #%option
  522. #% key: where
  523. #% type: string
  524. #% required: no
  525. #% multiple: no
  526. #% key_desc: sql_query
  527. #% label: WHERE conditions of SQL statement without 'where' keyword
  528. #% description: Example: income < 1000 and population >= 10000
  529. #%end
  530. import sys
  531. import grass.script as grass
  532. def main():
  533. # put code here
  534. return 0
  535. if __name__ == "__main__":
  536. options, flags = grass.parser()
  537. sys.exit(main())
  538. </pre></div>
  539. <h2>SEE ALSO</h2>
  540. <em>
  541. <a href="g.filename.html">g.filename</a>,
  542. <a href="g.findfile.html">g.findfile</a>,
  543. <a href="g.tempfile.html">g.tempfile</a>
  544. </em>
  545. <p>
  546. Overview table: <a href="parser_standard_options.html">Parser standard options</a>
  547. <p>
  548. <a href="http://trac.osgeo.org/grass/wiki/Submitting/Python">Submitting rules for Python</a>
  549. <p>
  550. Related Wiki pages:
  551. <a href="http://grasswiki.osgeo.org/wiki/Category:Linking_to_other_languages">Using GRASS GIS with other programming languages</a>
  552. <h2>AUTHOR</h2>
  553. Glynn Clements
  554. <p>
  555. <i>Last changed: $Date$</i>