g.parser.html 18 KB

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