gislib_cmdline_parsing.dox 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*!
  2. \page gislib_cmdline_parsing Command Line Parsing
  3. <!--
  4. extracted form gislib.dox and improved by Vaclav Petras, 2013
  5. Copyright 2004-2022 by the GRASS Development Team
  6. Published under GNU Free Documentation License
  7. -->
  8. \tableofcontents
  9. \section gislibcmd_Introduction Introduction
  10. This section describes a standard mechanism for
  11. command line parsing in GRASS. The system is usually referred as
  12. <em>parser</em>, G_parser() or <em>g.parser</em> (because of the related
  13. \gmod{g.parser} module).
  14. Use of the provided set of functions will standardize
  15. GRASS modules that expect command line arguments, creating a family of
  16. GRASS modules that is easy for users to learn.
  17. The standardization is important because as soon as a GRASS user
  18. familiarizes himself with the general form of command line input as
  19. defined by the parser, it will greatly simplify the necessity of
  20. remembering or at least guessing the required command line arguments
  21. for any GRASS command. It is strongly recommended, almost mandatory,
  22. that GRASS programmers use this set of functions for all command line
  23. parsing. With their use, the programmer is freed from the burden of
  24. generating user interface code for every command. The parser will
  25. limit the programmer to a pre-defined look and feel, but limiting the
  26. interface is well worth the shortened user learning curve. Moreover,
  27. system enables to generate module interface descriptions which can
  28. be used by GUI to generate a graphical interface for a module.
  29. \note There are also standard options and flags, which ensure even
  30. better standardization of names, values and descriptions.
  31. \section gislibcmd_Description Description
  32. The GRASS parser is the collection of functions and
  33. structures defined in the GRASS gis.h header file. These
  34. structures and functions allow the programmer to define the options and
  35. flags that make up the valid command line input of a GRASS command.
  36. The parser functions behave in one of three ways:
  37. <ul>
  38. <li> If no command line arguments are entered by the user, the parser
  39. searches for a completely interactive version of the command. If the
  40. interactive version is found, control is passed over to that
  41. version.
  42. <li> If command line arguments are entered but they are a subset of the
  43. options and flags that the programmer has defined as required
  44. arguments, three things happen. The parser will pass an error message
  45. to the user indicating which required options and/or flags were
  46. missing from the command line, the parser will then display a complete
  47. usage message for that command, and finally the parser will terminate
  48. the command.
  49. <li> If all necessary options and flags are entered on the command line
  50. by the user, the parser executes the command with the given options
  51. and flags.
  52. </ul>
  53. \note Python modules uses the same system but instead of functions
  54. and structures, comments in files are used to define options and flags.
  55. \section Parser_Interface Parser interface
  56. The parser functions described below use two structures as defined in
  57. the GRASS gis.h header file.
  58. This is a basic list of members of the Option and Flag
  59. structures. A comprehensive description of all elements of these two
  60. structures and their possible values can be found in
  61. \ref Complete_Structure_Members_Table.
  62. \subsection Option_structure Option structure
  63. The basic usage of the Option structure is as follows.
  64. You declare a pointer to the Option structure.
  65. \code
  66. struct Option *opt;
  67. \endcode
  68. And then you call G_define_option() function, which allocates memory for
  69. the Option structure and returns a pointer to it.
  70. \code
  71. opt = G_define_option();
  72. \endcode
  73. Then you set the structure members, basic members are:
  74. - <tt>key</tt> - The option name on the command line.
  75. - <tt>description</tt> - The option description to display to the user.
  76. - <tt>type</tt> - Variable type of the user's answer to the option.
  77. - <tt>required</tt> - Whether this option is mandatory or not.
  78. For the full list of members see the Option structure documentation.
  79. \subsection Flag_structure Flag structure
  80. The basic usage of the Flag structure is as follows.
  81. You declare a pointer to the Flag structure.
  82. \code
  83. struct Flag *flag;
  84. \endcode
  85. And then you call G_define_flag() function, which allocates memory for
  86. the Flag structure and returns a pointer to it.
  87. \code
  88. flag = G_define_flag()
  89. \endcode
  90. Then you set the structure members, basic members are:
  91. - <tt>key</tt> - A single letter flag name on the command line.
  92. - <tt>description</tt> - The flag description to display to the user.
  93. For the full list of members see the Flag structure documentation.
  94. \subsection Running_Parser Running Parser
  95. To process and check the command line parameters of your module,
  96. you need to call the G_parser() function.
  97. The command line parameters <i>argv</i> and the number of parameters
  98. <i>argc</i> from the main() routine should be passed directly to the
  99. G_parser() function. The function accepts the command line input
  100. entered by the user, and parses this input according to the input
  101. options and/or flags that were defined by the programmer.
  102. G_parser() returns 0 if successful. If not successful, it displays
  103. a usage statement, which describes the expected and/or required
  104. options and flags, and returns a non-zero value.
  105. \subsection Parser_Additional_checks Additional checks of command line parameters
  106. When the G_parser() function is not sufficient to check all the details
  107. about the options and flags and their combinations, the code has to
  108. implement the required logic through custom additional checks. The code will
  109. typically call the G_usage() function if these checks are not successful.
  110. G_usage() prints a usage message, which
  111. explains the allowed and the required command line
  112. input to the user. This description is given according to the
  113. programmer's definitions for options and flags. This function becomes
  114. useful when the user enters options and/or flags on the command line
  115. that are syntactically valid to the parser, but functionally invalid
  116. for the command (e.g. an invalid file name).
  117. For example, the parser logic doesn't directly support grouping
  118. options. If two options must be specified together or not at all, the
  119. parser must be told that these options are not required and the
  120. programmer must check that if one option is specified the other is as well.
  121. If this additional check fails after G_parser() has succeeded, the
  122. programmer can then call G_usage() to print the standard usage message
  123. and then would separately need to print the additional information on how
  124. the two options work together.
  125. \subsection Parser_Displaying_multiple_answers Multiple answer default values
  126. Providing multiple default values (answers) for an option that allows
  127. multiple values is possible using:
  128. \code
  129. char *def[] = {"One", "Two", "Last", NULL};
  130. opt->multiple = YES;
  131. opt->answers = def;
  132. \endcode
  133. The programmer must not forget the last NULL value.
  134. <em>New in GRASS 5.</em>
  135. \subsection Parser_Disabling_interactive Disabling interactive mode
  136. This is mainly historical feature which enables to disable interactive
  137. prompting in command line.
  138. When a user runs a command with no arguments on the command line, the
  139. parser will enter its own standardized interactive session and prompt the
  140. user for all flags and options interactively. A call to
  141. G_disable_interactive() disables this feature.
  142. \section Parser_Programming_Examples Parser Programming Examples
  143. The use of the parser in the programming process is demonstrated
  144. here. Both a basic step by step example and a full code example are
  145. presented.
  146. \subsection Step_by_Step_Use_of_the_Parser Step by Step Use of the Parser
  147. Below are four basic steps to use the GRASS parser in a GRASS command:
  148. \subsubsection gislib_cmdline_Allocate Allocate memory for Flags and Options
  149. Options and flags are pointers to structures (Option and Flag structures)
  150. allocated through the parser functions G_define_option() and G_define_flag()
  151. as described in \ref Parser_Interface.
  152. \code
  153. #include <grass/gis.h>; /* The standard GRASS include file */
  154. /* ... */
  155. struct Option *opt; /* Establish an Option pointer for each option */
  156. struct Flag *flag; /* Establish a Flag pointer for each option */
  157. opt = G_define_option(); /* Request a pointer to memory for each option */
  158. flag = G_define_flag(); /* Request a pointer to memory for each flag */
  159. \endcode
  160. \subsubsection gislib_cmdline_Define Define members of Flag and Option structures
  161. Define the characteristics of each required option and flag, for example:
  162. \code
  163. opt->key = "option"; /* The name of this option is "option". */
  164. opt->description = _("Option test"); /* The option description is "Option test" */
  165. opt->type = TYPE_STRING; /* The data type of the answer to the option */
  166. opt->required = YES; /* This option *is* required from the user */
  167. flag->key = "t"; /* Single letter name for flag */
  168. flag->description = _("Flag test"); /* The flag description is "Flag test" */
  169. \endcode
  170. \note There are more options defined in \ref Complete_Structure_Members_Table.
  171. You should for sure explore the <b>label</b>, the <b>options</b>
  172. and the <b>multiple</b> structure members.
  173. \subsubsection gislib_cmdline_Call Call the parser
  174. \code
  175. int main(int argc, char *argv[]); /* command line args passed into main() */
  176. /* ... options and flags definitions */
  177. if (G_parser(argc, argv)) /* Returns 0 if successful, non-zero otherwise */
  178. exit(EXIT_FAILURE);
  179. /* ... additional checks */
  180. /* ... module code */
  181. \endcode
  182. \subsubsection gislib_cmdline_Extracting Extracting information from the parser structures
  183. The following code extracts the information about an option and a flag
  184. and prints it to the standard output.
  185. \code
  186. fprintf(stdout, "For the option "%s" you chose: <%s>\n", opt->description, opt->answer);
  187. fprintf(stdout, "The flag "-%s" is %s.\n", flag->key, flag->answer ? "set" : "not set");
  188. \endcode
  189. \subsubsection gislib_cmdline_Running Running the example program
  190. Once such a module has been compiled
  191. (see \ref Compiling_and_Installing_GRASS_Modules), execution will result
  192. in the following user interface scenarios. Lines that begin with '$' (dollar sign)
  193. indicate a command line session with user commands.
  194. \verbatim
  195. $ r.mysample --help
  196. \endverbatim
  197. This is a standard user request for basic help information on the
  198. module. The command line options (in this case, <tt>--help</tt>) are sent to
  199. the parser via G_parser(). The parser recognizes the <tt>--help</tt>
  200. command line option and returns the list of options and/or flags that
  201. are applicable for the specific command. Note how the option and the flag
  202. information specified above appears in the output.
  203. \verbatim
  204. r.mysample [-t] option=name
  205. Flags:
  206. -t Flag test
  207. Parameters:
  208. option Option test
  209. \endverbatim
  210. Now consider the following command:
  211. \verbatim
  212. $ r.mysample -t
  213. \endverbatim
  214. This command does not contain the required option. Note that the
  215. output provides this information along with the standard usage message
  216. (as already shown above):
  217. \verbatim
  218. Required parameter <option> not set (Option test).
  219. Usage:
  220. r.mysample [-t] option=name
  221. Flags:
  222. -t Flag test
  223. Parameters:
  224. option Option test
  225. \endverbatim
  226. The following commands are correct and equivalent:
  227. \verbatim
  228. $ r.mysample option=Hello -t
  229. $ r.mysample -t option=Hello
  230. \endverbatim
  231. The parser provides no error messages and the module executes normally:
  232. \verbatim
  233. For the option "Option test" you chose: Hello
  234. The flag "-t" is set.
  235. \endverbatim
  236. \subsection gislibcmd_Full_Module_Example Full Module Example
  237. The following code demonstrates some of the basic capabilities of the
  238. parser. To compile this code, create this Makefile and run the
  239. <tt>make</tt> command (see \ref Compiling_and_Installing_GRASS_Modules).
  240. \verbatim
  241. MODULE_TOPDIR = ../..
  242. PGM = r.mysample
  243. LIBES = $(GISLIB)
  244. DEPENDENCIES = $(GISDEP)
  245. include $(MODULE_TOPDIR)/include/Make/Module.make
  246. default: cmd
  247. \endverbatim
  248. The sample C code follows (the usual name of the file with the main
  249. function is <tt>%main.c</tt>). You can experiment with this code to
  250. familiarize yourself with the parser.
  251. \note This example includes some of the advanced structure
  252. members described in \ref Complete_Structure_Members_Table.
  253. \code
  254. #include <stdlib.h>
  255. #include <string.h>
  256. #include <grass/gis.h>
  257. #include <grass/glocale.h>
  258. int main(int argc, char *argv[])
  259. {
  260. struct Option *opt, *coor;
  261. struct Flag *flag;
  262. double X, Y;
  263. int n;
  264. opt = G_define_option();
  265. opt->key = "debug";
  266. opt->type = TYPE_STRING;
  267. opt->required = NO;
  268. opt->answer = "0";
  269. opt->description = _("Debug level");
  270. coor = G_define_option();
  271. coor->key = "coordinate";
  272. coor->key_desc = "x,y";
  273. coor->type = TYPE_STRING;
  274. coor->required = YES;
  275. coor->multiple = YES;
  276. coor->description = _("One or more coordinate(s)");
  277. /* Note that coor->answer is not given a default value. */
  278. flag = G_define_flag();
  279. flag->key = 'v';
  280. flag->description = _("Verbose execution");
  281. /* Note that flag->answer is not given a default value. */
  282. if (G_parser(argc, argv))
  283. exit (EXIT_FAILURE);
  284. G_message("For the option <%s> you chose: <%s>",
  285. opt->description, opt->answer);
  286. G_message("The flag <%s> is: %s", flag->key,
  287. flag->answer ? "set" : "not set");
  288. G_message("You specified the following coordinates:");
  289. for (n=0; coor->answers[n] != NULL; n+=2) {
  290. G_scan_easting(coor->answers[n], &X , G_projection());
  291. G_scan_northing(coor->answers[n+1], &Y , G_projection());
  292. fprintf(stdout, "%.15g,%.15g", X, Y);
  293. }
  294. }
  295. \endcode
  296. \note This example defines the option for coordinates in its own way to
  297. demonstrate usage of particular parser features. However, it must be noted
  298. that there is a standardized G_OPT_M_COORDS option, which should be used for
  299. coordinates.
  300. \section Complete_Structure_Members_Table Complete Structure Members Table
  301. \subsection memtabFlag struct Flag
  302. <table>
  303. <tr>
  304. <th>structure member</th>
  305. <th>C type</th>
  306. <th>required</th>
  307. <th>default</th>
  308. <th> description and example</th>
  309. </tr><tr>
  310. <td>key</td>
  311. <td> char</td>
  312. <td> YES</td>
  313. <td> none</td>
  314. <td> Key char used on command line.<br>
  315. <tt>flag->key = 'f' ;</tt></td>
  316. </tr><tr>
  317. <td>Description</td>
  318. <td> char *</td>
  319. <td> YES</td>
  320. <td> none</td>
  321. <td> A string describing the flag meaning.<br>
  322. <tt>flag->description = _("run in fast mode") ;</tt></td>
  323. </tr><tr>
  324. <td>answer</td>
  325. <td> char</td>
  326. <td> NO</td>
  327. <td> NULL</td>
  328. <td> Default and parser-returned
  329. flag states.</td>
  330. </tr>
  331. </table>
  332. \subsection memtabOption struct Option
  333. <table>
  334. <tr>
  335. <th>structure member</th>
  336. <th>C type </th>
  337. <th>required </th>
  338. <th>default </th>
  339. <th>description and example</th>
  340. </tr>
  341. <tr>
  342. <td>key </td>
  343. <td>char * </td>
  344. <td>YES </td>
  345. <td>none </td>
  346. <td>Key word used on command line.<br>
  347. <tt>opt->key = "map" ;</tt></td>
  348. </tr>
  349. <tr>
  350. <td>type </td>
  351. <td>int </td>
  352. <td>YES </td>
  353. <td>none </td>
  354. <td>%Option type: <br>
  355. TYPE_STRING <br>
  356. TYPE_INTEGER <br>
  357. TYPE_DOUBLE <br>
  358. <tt>opt->type = TYPE_STRING ;</tt></td>
  359. </tr>
  360. <tr>
  361. <td>Description </td>
  362. <td>char * </td>
  363. <td>YES </td>
  364. <td>none </td>
  365. <td>A string describing the option along with the gettext macro for internationalization.<br>
  366. <tt>opt->description = _("Map name") ;</tt></td>
  367. </tr>
  368. <tr>
  369. <td>answer </td>
  370. <td>char * </td>
  371. <td>NO </td>
  372. <td>NULL </td>
  373. <td>The default and the parser-returned answer to the option.<br>
  374. <tt>opt->answer = "defaultmap" ;</tt></td>
  375. </tr>
  376. <tr>
  377. <td>key_desc </td>
  378. <td>char * </td>
  379. <td>NO </td>
  380. <td>NULL </td>
  381. <td>Single word describing the key. Commas in this string denote
  382. to the parser that several comma-separated arguments are expected
  383. from the user as one answer. For example, if a pair of coordinates
  384. is desired, this element should be defined as follows.<br>
  385. <tt>opt->key_desc = "x,y" ;</tt></td>
  386. </tr>
  387. <tr>
  388. <th>structure member</th>
  389. <th>C type </th>
  390. <th>required </th>
  391. <th>default </th>
  392. <th>description and example</th>
  393. </tr>
  394. <tr>
  395. <td>multiple </td>
  396. <td>int </td>
  397. <td>NO </td>
  398. <td>NO </td>
  399. <td>Indicates whether the user can provide multiple answers or not.
  400. YES and NO are defined in "gis.h" and should be used (NO is
  401. the default.) <b>multiple</b> is used in conjunction with the <b>answers</b>
  402. structure member below.<br>
  403. <tt>opt->multiple = NO ;</tt></td>
  404. </tr>
  405. <tr>
  406. <td>answers </td>
  407. <td> </td>
  408. <td>NO </td>
  409. <td>NULL </td>
  410. <td>Multiple parser-returned answers to an option.<br>
  411. N/A</td>
  412. </tr>
  413. <tr>
  414. <td>required </td>
  415. <td>int </td>
  416. <td>NO </td>
  417. <td>NO </td>
  418. <td>Indicates whether the user MUST provide the option on the command
  419. line. YES and NO are defined in "gis.h" and should be used (NO
  420. is the default).<br>
  421. <tt>opt->required = YES ;</tt></td>
  422. </tr>
  423. <tr>
  424. <td>options </td>
  425. <td>char * </td>
  426. <td>NO </td>
  427. <td>NULL </td>
  428. <td>Approved values or range of values. <br>
  429. <tt>opt->options = "red,blue,white" ;</tt><br>
  430. For integers and doubles, the following format is available: <br>
  431. <tt>opt->options = "0-1000" ;</tt></td>
  432. </tr>
  433. <tr>
  434. <td>gisprompt</td>
  435. <td>char *</td>
  436. <td>NO</td>
  437. <td>NULL</td>
  438. <td>Interactive prompt guidance. There are three comma-separated
  439. parts to this argument, which guide the standard GRASS
  440. file name prompting routines.<br>
  441. <tt>opt->gisprompt = "old,cell,raster" ;</tt></td>
  442. </tr>
  443. <tr>
  444. <td>checker</td>
  445. <td>char *()</td>
  446. <td>NO</td>
  447. <td>NULL</td>
  448. <td>A routine to check an answer to the option.<br>
  449. <tt>opt->checker = my_routine() ;</tt></td>
  450. </tr>
  451. </table>
  452. \section Description_of_Complex_Structure_Members Description of Complex Structure Members
  453. What follows are explanations of possibly confusing structure
  454. members. They are intended to clarify and supplement the structures table
  455. above.
  456. \subsection Answer_member_of_the_Flag_and_Option_structures Answer member of the Flag and Option structures
  457. The answer structure member serves two functions for GRASS commands
  458. that use the parser.
  459. <b>(1) To set the default answer to an option:</b>
  460. If a default state is desired for a programmer-defined option, the
  461. programmer may define the Option structure member <b>answer</b> before
  462. calling G_parser() in his module. After the G_parser() call, the
  463. <b>answer</b> member will hold this preset default value if the user did
  464. <i>not</i> enter an option that has the default answer member value.
  465. <b>(2) To obtain the command-line answer to an option or a flag:</b>
  466. After a call to G_parser(), the <b>answer</b> member will contain one of the two
  467. values:
  468. - (a) If the user provided an option and answered this option on the
  469. command line, the user's input will replace the default value of the
  470. <b>answer</b> member as described above.
  471. - (b) If the user provided an option, but did <i>not</i> answer this
  472. option on the command line, the default is not used. The user may use
  473. the default answer to an option by withholding mention of the option
  474. on the command line. But if the user enters an option without an
  475. answer, G_parser() will replace the default <b>answer</b> member value with
  476. NULL.
  477. As an example, please review the use of <b>answer</b> members in the structures
  478. implemented in \ref gislibcmd_Full_Module_Example.
  479. \subsection Multiple_and_Answers_Members Multiple and Answers Members
  480. The functionality of the <b>answers</b> structure member is reliant on the
  481. programmer's definition of the <b>multiple</b> structure member. If the <b>multiple</b>
  482. member is set to NO, the <b>answer</b> member is used to obtain the answer to an
  483. option as described above.
  484. If the <b>multiple</b> structure member is set to YES, it
  485. tells G_parser() to capture multiple answers. Multiple answers are
  486. separated by <em>commas</em> on the command line after an option.
  487. <b>Note:</b> G_parser() does not recognize any character other than a
  488. comma to delimit multiple answers.
  489. After the programmer has set up an option to receive multiple answers,
  490. these answers are stored in the <b>answers</b> member of the Option
  491. structure. The <b>answers</b> member is an array that contains each
  492. individual user-entered answer. The elements of this array are the
  493. type specified by the programmer using the <b>type</b> member. The <b>answers</b>
  494. array contains however many comma-delimited answers the user entered,
  495. followed (terminated) by a NULL array element.
  496. Below is an example definition of an Option using the <b>multiple</b>
  497. and the <b>answers</b> structure members:
  498. \code
  499. opt->key = "option";
  500. opt->description = _("option example");
  501. opt->type = TYPE_INTEGER;
  502. opt->required = NO;
  503. opt->multiple = YES;
  504. \endcode
  505. The above definition would ask the user for multiple integer answers
  506. to the option. If in response the user entered "option=1,3,8,15" on the
  507. command line, the <b>answers</b> array would contain the following values:
  508. \code
  509. answers[0] == "1"
  510. answers[1] == "3"
  511. answers[2] == "8"
  512. answers[3] == "15"
  513. answers[4] == NULL
  514. \endcode
  515. \subsection key_desc_Member key_desc Member
  516. The <b>key_desc</b> structure member is used to define the format of a single
  517. command line answer to an option. A programmer may wish to ask for one
  518. answer to an option, but this answer may not be a single argument of a type
  519. set by the type structure member. If the programmer wants the user to enter
  520. a coordinate, for example, the programmer might define an Option as follows:
  521. \code
  522. opt->key ="coordinate";
  523. opt->description = _("Specified Coordinate");
  524. opt->type = TYPE_INTEGER;
  525. opt->required = NO;
  526. opt->key_desc = "x,y"
  527. opt->multiple = NO;
  528. \endcode
  529. The answer to this option would <i>not</i> be stored in the answer
  530. member, but in the answers member. If the user entered
  531. "coordinate=112,225" on the command line in response to a routine that
  532. contains the above option definition, the <b>answers</b> array would have the
  533. following values after the call to G_parser():
  534. \code
  535. answers[0] == "112"
  536. answers[1] == "225"
  537. answers[2] == NULL
  538. \endcode
  539. Note that "coordinate=112" would not be valid, as it does not contain both
  540. components of an answer as defined by the <b>key_desc</b> structure member.
  541. If the <b>multiple</b> structure member was set to YES instead of NO in the
  542. example above, the answers would be stored sequentially in the <b>answers</b>
  543. member. For example, if the user wanted to enter the coordinates
  544. (112,225), (142,155), and (43,201), his response on the command line
  545. would be "coordinate=112,225,142,155,43,201". Note that G_parser()
  546. recognizes only a comma for both the <b>key_desc</b> member, and multiple
  547. answers.
  548. The <b>answers</b> array would have the following values after a call to
  549. G_parser():
  550. \code
  551. answers[0] == "112" answers[1] == "225"
  552. answers[2] == "142" answers[3] == "155"
  553. answers[4] == "43" answers[5] == "201"
  554. answers[6] == NULL
  555. \endcode
  556. <B>Note.</B> In this case as well, neither "coordinate=112" nor
  557. "coordinate=112,225,142" would be valid command line arguments, as
  558. they do not contain even pairs of coordinates. Each answer's format
  559. (as described by the <b>key_desc</b> member) must be fulfilled completely.
  560. The overall function of the <b>key_desc</b> and the <b>multiple</b> structure members is
  561. very similar. The <b>key_desc</b> member is used to specify the number of
  562. <i>required</i> components of a single option answer (e.g. a
  563. multi-valued coordinate.) The <b>multiple</b> member tells G_parser() to ask
  564. the user for multiple instances of the compound answer as defined by
  565. the format in the <b>key_desc</b> structure member.
  566. Another function of the <b>key_desc</b> structure member is to explain to the
  567. user the type of information expected as an answer. The coordinate
  568. example is explained above.
  569. The usage message that is displayed by G_parser() in case of an error,
  570. or by G_usage() on programmer demand, is shown below. The Option
  571. "option" for the command <tt>a.out</tt> does not have its <b>key_desc</b>
  572. structure member defined.
  573. \verbatim
  574. Usage:
  575. a.out option=name
  576. \endverbatim
  577. The use of "name" is a G_parser() standard. If the programmer defines
  578. the <b>key_desc</b> structure member before a call to G_parser(), the
  579. value of the <b>key_desc</b> member replaces "name". Thus, if the <b>key_desc</b>
  580. member is set to "x,y" as was used in an example above, the following
  581. usage message would be displayed:
  582. \verbatim
  583. Usage:
  584. a.out option=x,y
  585. \endverbatim
  586. The <b>key_desc</b> structure member can be used by the programmer to clarify
  587. the usage message as well as to specify single or multiple required
  588. components of a single option answer.
  589. \subsection gisprompt_Member gisprompt Member
  590. The <b>gisprompt</b> Option structure member requires a bit more
  591. description. The three comma-separated (no spaces allowed)
  592. sub-arguments are defined as follows:
  593. - The first argument: "old" results in a call to the GRASS library
  594. subroutine G_open_old(), "new" to G_open_new(), otherwise "any" or
  595. "mapset".
  596. - If any option has "new" as the first component, the <tt>--o</tt>
  597. (overwrite) flag will be listed in the module's interface
  598. (<tt>--help</tt> output, manual page, GUI dialog, etc).
  599. - If an option that has "new" as the first component is given, the
  600. parser checks whether the entity (map, etc.) already exists.
  601. - The second argument: this is identical to the "element" argument in the
  602. above subroutine calls. It specifies a directory inside the mapset
  603. that may contain the user's response. In other words, the second field
  604. is used to determine where to look for the file (i.e. if the option
  605. has "new,cell,...", it will look in the "cell" directory). The second
  606. field should be the name of one of the standard subdirectories of the
  607. mapset, as listed in $GISBASE/etc/element_list.
  608. - The third argument: identical to the "prompt" argument in the above
  609. subroutine calls. This is a string presented to the user that
  610. describes the type of data element being requested.
  611. Here are two examples:
  612. \verbatim
  613. "new,cell,raster" G_open_new("cell", "map")
  614. "old,vector,vector" G_open_old("vector", "map")
  615. \endverbatim
  616. The gisprompt values are passed to any GUI code, both self-contained
  617. dialogs generated by the parser for the <tt>--ui</tt> option, and
  618. stand-alone GUIs (wxGUI) which use the <tt>--xml-description</tt>
  619. flags to obtain a machine-readable description of the module's
  620. interface. How the GUI interprets this is up to the GUI.
  621. \section gislibcmd_Standard_options Standard options and flags
  622. There are standard options and standard flags, they ensure consistency
  623. in names and values.
  624. Standard options are defined by the G_define_standard_option() function
  625. and standard flags are defined by the G_define_standard_flag() function.
  626. Both the options and the flags are defined dynamically, so to see
  627. the values of all members you need to open the file parser_standard_options.c.
  628. The function G_define_standard_option() accepts one value of the STD_OPT
  629. enum defined in the gis.h file. The G_define_standard_option() function
  630. calls the G_define_option() function, so there is no need to call
  631. the latter separately. The same convention applies to standard flags too,
  632. which use the G_define_standard_flag() function and the STD_OPT enum.
  633. Besides a name and a value, standard options also define a label, a description,
  634. allowed values, their descriptions etc. Standard flags use a similar approach.
  635. After defining a standard option or a standard flag you can still assign custom
  636. values to individual structure members as required instead of the default values.
  637. \section gislibcmd_FAQ Command line parsing FAQ
  638. \par Can the user mix options and flags?
  639. Yes. Options and flags can be given in any order.
  640. \par In what order does the parser present options and flags?
  641. Flags and options appear in the usage message according to the order
  642. of the G_define_option() and the G_define_flag() function calls.
  643. \par Is the user required to use full option names?
  644. No. Users are required to type in only as many characters of an option name
  645. as is necessary to make the option choice unambiguous. If, for example,
  646. there are two options, "input=" and "output=", the following would be
  647. valid command line arguments:
  648. \verbatim
  649. $ command i=map1 o=map2
  650. $ command in=map1 out=map2
  651. \endverbatim
  652. \par Are options standardized at all?
  653. Yes. There are a few conventions. Options that identify a single input map
  654. are usually "map=", not "raster=" or "vector=". In the case of an
  655. input and an output map the convention is "input=xx output=yy". By passing
  656. the '--help' flag to existing GRASS commands, it is likely that you will
  657. find other conventions. The intent is to make it as easy as possible for the
  658. user to remember (or to guess correctly) what the command line syntax is for a
  659. given command.
  660. To ensure maximal consistency, the most common options such as the options
  661. named above are defined as standard options and are available through
  662. G_define_standard_option() function. For flags you can use the
  663. G_define_standard_flag() function.
  664. \par How does a programmer query for coordinates?
  665. There is the standardized G_OPT_M_COORDS option, which should be used for
  666. coordinates.
  667. See the source code for the GRASS commands <tt>r.drain</tt> or
  668. <tt>r.cost</tt> for examples.
  669. \par How does a programmer define that the option is a set of values?
  670. For any user input that requires a tuple of values (like a pair of
  671. map coordinates) the programmer specifies the number of arguments in
  672. the <b>key_desc</b> member of the Option structure. For example, if
  673. <b>key_desc</b> was set to "x,y", the parser would require that the
  674. user enters a pair of arguments separated only with a comma.
  675. \note There is the standardized G_OPT_M_COORDS option, which should be used for
  676. coordinates.
  677. \par How is automatic prompting turned off?
  678. GRASS 4.0 introduced a new method for driving GRASS interactive and
  679. non-interactive modules as described in \ref
  680. Compiling_and_Installing_GRASS_Modules. Here is a short overview.
  681. For most modules a user runs a front-end module out of the GRASS bin
  682. directory, which in turn looks for the existence of interactive and
  683. non-interactive versions of the module. If an interactive version
  684. exists and the user provided no command line arguments, then that
  685. version is executed.
  686. In such a situation, the parser's default interaction will never be
  687. seen by the user. A programmer using the parser is able to avoid the
  688. front-end's default search for a fully interactive version of the
  689. command by placing a call to G_disable_interactive() before
  690. calling G_parser() (see \ref Parser_Interface for details).
  691. */