gislib_cmdline_parsing.dox 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*!
  2. \page gislib_cmdline_parsing Command Line Parsing
  3. <!--
  4. extracted form gislib.dox and improved by Vaclav Petras, 2013
  5. Copyright 2004-2017 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 ensures even
  30. better standardization of names, values and descriptions.
  31. \section gislibcmd_Description Description
  32. The GRASS parser is a collection of functions and
  33. structures that are 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 this
  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 cancels
  48. execution of 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_Description.
  62. \subsection Option_structure Option structure
  63. The basic usage of the Option structure is as follows.
  64. You create 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> - Option name that user will use
  75. - <tt>description</tt> - Option description that is shown to the user
  76. - <tt>type</tt> - Variable type of the user's answer to the option
  77. - <tt>required</tt> - If this option is required on the command line?
  78. For full list of members see Option structure documentation.
  79. \subsection Flag_structure Flag structure
  80. The basic usage of the Flag structure is as follows.
  81. You create 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> - Single letter used for flag name
  92. - <tt>description</tt> - Flag description that is shown to the user
  93. For full list of members see Flag structure documentation.
  94. \subsection Running_Parser Running Parser
  95. To process and check the command line parameters of you module,
  96. you need to call G_parser() function.
  97. The command line parameters <i>argv</i> and the number of parameters
  98. <i>argc</i> from the main() routine are should be passed directly to
  99. G_parser() function. G_parser() 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, a usage
  103. statement is displayed that describes the expected and/or required
  104. options and flags and a non-zero value is returned.
  105. \subsection Parser_Additional_checks Additional checks of command line parameters
  106. When a G_parser() function is not sufficient to check all the details
  107. about the options and flags and their combinations, programmer has
  108. to add additional checks which are needed. If these checks are not
  109. successful programmer can call G_usage() function.
  110. Calls to G_usage() allow the programmer to print the usage message at
  111. any time. This will explain the allowed and 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 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 is specified the other must be as
  121. well. If this additional check fails, then G_parser() will
  122. succeed, but the programmer can then call G_usage() to print
  123. the standard usage message and print additional information about how
  124. the two options work together.
  125. \subsection Parser_Displaying_multiple_answers Displaying multiple answers default values
  126. Providing multiple default values (answers) for option with 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 may not forget 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 calls a command with no arguments on the command line, the
  139. parser will enter its own standardized interactive session in which
  140. all flags and options are presented to the user for input. A call to
  141. G_disable_interactive() disables the parser's interactive prompting.
  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 full code example are
  145. presented.
  146. \subsection Step_by_Step_Use_of_the_Parser Step by Step Use of the Parser
  147. These are the four basic steps to follow to implement the use of the
  148. GRASS parser in a GRASS command:
  149. \subsubsection gislib_cmdline_Allocate Allocate memory for Flags and Options
  150. Options and flags are pointers to structures (Option and Flag structures)
  151. allocated through the parser functions G_define_option() and G_define_flag()
  152. as described in \ref Parser_Interface.
  153. \code
  154. #include <grass/gis.h>; /* The standard GRASS include file */
  155. /* ... */
  156. struct Option *opt; /* Establish an Option pointer for each option */
  157. struct Flag *flag; /* Establish a Flag pointer for each option */
  158. opt = G_define_option(); /* Request a pointer to memory for each option */
  159. flag = G_define_flag(); /* Request a pointer to memory for each flag */
  160. \endcode
  161. \subsubsection gislib_cmdline_Define Define members of Flag and Option structures
  162. The programmer should define the characteristics of each option and
  163. flag desired as outlined by the following example:
  164. \code
  165. opt->key = "option"; /* The name of this option is "option". */
  166. opt->description = _("Option test"); /* The option description is "Option test" */
  167. opt->type = TYPE_STRING; /* The data type of the answer to the option */
  168. opt->required = YES; /* This option *is* required from the user */
  169. flag->key = "t"; /* Single letter name for flag */
  170. flag->description = _("Flag test"); /* The flag description is "Flag test" */
  171. \endcode
  172. \note There are more options defined in \ref Complete_Structure_Members_Table.
  173. You should for sure explore <tt>label</tt> member, <tt>options</tt> member
  174. and <tt>multiple</tt> member.
  175. \subsubsection gislib_cmdline_Call Call the parser
  176. \code
  177. int main(int argc, char *argv[]); /* command line args passed into main() */
  178. /* ... options and flags definitions */
  179. if (G_parser(argc, argv)) /* Returns 0 if successful, non-zero otherwise */
  180. exit(EXIT_FAILURE);
  181. /* ... additional checks */
  182. /* ... module code */
  183. \endcode
  184. \subsubsection gislib_cmdline_Extracting Extracting information from the parser structures
  185. The following lines will extract the information form option and flag
  186. and print it to the standard output.
  187. \code
  188. fprintf(stdout, "For the option "%s" you chose: <%s>\n", opt->description, opt->answer);
  189. fprintf(stdout, "The flag "-%s" is %s set.\n", flag->key, flag->answer ? "" : "not");
  190. \endcode
  191. \subsubsection gislib_cmdline_Running Running the example program
  192. Once such a module has been compiled
  193. (see \ref Compiling_and_Installing_GRASS_Modules), execution will result
  194. in the following user interface scenarios. Lines that begin with '$' (dollar sign)
  195. imply user entered commands on the command line.
  196. \verbatim
  197. $ r.mysample --help
  198. \endverbatim
  199. This is a standard user call for basic help information on the
  200. module. The command line options (in this case, <tt>--help</tt>) are sent to
  201. the parser via G_parser(). The parser recognizes the <tt>--help</tt>
  202. command line option and returns a list of options and/or flags that
  203. are applicable for the specific command. Note how the programmer
  204. provided option and flag information is captured in the output.
  205. \verbatim
  206. r.mysample [-t] option=name
  207. Flags:
  208. -t Flag test
  209. Parameters:
  210. option Option test
  211. \endverbatim
  212. Now the following command is executed:
  213. \verbatim
  214. # r.mysample -t
  215. \endverbatim
  216. This command line does not contain the required option. Note that the
  217. output provides this information along with the standard usage message
  218. (as already shown above):
  219. \verbatim
  220. Required parameter <option> not set (Option test).
  221. Usage:
  222. r.mysample [-t] option=name
  223. Flags:
  224. -t Flag test
  225. Parameters:
  226. option Option test
  227. \endverbatim
  228. The following commands are correct and equivalent:
  229. \verbatim
  230. $ r.mysample option=Hello -t
  231. $ r.mysample -t option=Hello
  232. \endverbatim
  233. The parser provides no error messages and the module executes normally:
  234. \verbatim
  235. For the option "Option test" you chose: Hello
  236. The flag "-t" is set.
  237. \endverbatim
  238. \subsection gislibcmd_Full_Module_Example Full Module Example
  239. The following code demonstrates some of the basic capabilities of the
  240. parser. To compile this code, create this Makefile and run the
  241. <tt>make</tt> command (see \ref Compiling_and_Installing_GRASS_Modules).
  242. \verbatim
  243. MODULE_TOPDIR = ../..
  244. PGM = r.mysample
  245. LIBES = $(GISLIB)
  246. DEPENDENCIES = $(GISDEP)
  247. include $(MODULE_TOPDIR)/include/Make/Module.make
  248. default: cmd
  249. \endverbatim
  250. The sample C code follows (the usual name of the file with the main
  251. function is <tt>%main.c</tt>. You might experiment with this code to
  252. familiarize yourself with the parser.
  253. \note This example includes some of the advanced structure
  254. members described in \ref Complete_Structure_Members_Table.
  255. \code
  256. #include <stdlib.h>
  257. #include <string.h>
  258. #include <grass/gis.h>
  259. #include <grass/glocale.h>
  260. int main(int argc, char *argv[])
  261. {
  262. struct Option *opt, *coor;
  263. struct Flag *flag;
  264. double X, Y;
  265. int n;
  266. opt = G_define_option();
  267. opt->key = "debug";
  268. opt->type = TYPE_STRING;
  269. opt->required = NO;
  270. opt->answer = "0";
  271. opt->description = _("Debug level");
  272. coor = G_define_option();
  273. coor->key = "coordinate";
  274. coor->key_desc = "x,y";
  275. coor->type = TYPE_STRING;
  276. coor->required = YES;
  277. coor->multiple = YES;
  278. coor->description = _("One or more coordinate(s)");
  279. /* Note that coor->answer is not given a default value. */
  280. flag = G_define_flag();
  281. flag->key = 'v';
  282. flag->description = _("Verbose execution");
  283. /* Note that flag->answer is not given a default value. */
  284. if (G_parser(argc, argv))
  285. exit (EXIT_FAILURE);
  286. G_message("For the option <%s> you chose: <%s>",
  287. opt->description, opt->answer);
  288. G_message("The flag <%s> is: %s set", flag->key,
  289. flag->answer ? "" : "not");
  290. G_message("You specified the following coordinates:");
  291. for (n=0; coor->answers[n] != NULL; n+=2) {
  292. G_scan_easting(coor->answers[n], &X , G_projection());
  293. G_scan_northing(coor->answers[n+1], &Y , G_projection());
  294. fprintf(stdout, "%.15g,%.15g", X, Y);
  295. }
  296. }
  297. \endcode
  298. \note This example defines option for coordinates in its own way to
  299. demonstrate usage of particular parser features. However, it must be noted
  300. that there is standardized option G_OPT_M_COORDS which should be used for
  301. coordinates.
  302. \section Complete_Structure_Members_Table Complete Structure Members Table
  303. \subsection memtabFlag struct Flag
  304. <table>
  305. <tr>
  306. <td>structure member</td>
  307. <td>C type</td>
  308. <td>required</td>
  309. <td>default</td>
  310. <td> description and example</td>
  311. </tr><tr>
  312. <td>key</td>
  313. <td> char</td>
  314. <td> YES</td>
  315. <td> none</td>
  316. <td> Key char used on command line<br>
  317. flag->key = 'f' ;</td>
  318. </tr><tr>
  319. <td>Description</td>
  320. <td> char *</td>
  321. <td> YES</td>
  322. <td> none</td>
  323. <td> String describing flag meaning<br>
  324. flag->description = _("run in fast mode") ;</td>
  325. </tr><tr>
  326. <td>answer</td>
  327. <td> char</td>
  328. <td> NO</td>
  329. <td> NULL</td>
  330. <td> Default and parser-returned
  331. flag states.</td>
  332. </tr>
  333. </table>
  334. \subsection memtabOption struct Option
  335. <table>
  336. <tr>
  337. <td>structure member</td>
  338. <td>C type </td>
  339. <td>required </td>
  340. <td>default </td>
  341. <td>description and example</td>
  342. </tr>
  343. <tr>
  344. <td>key </td>
  345. <td>char * </td>
  346. <td>YES </td>
  347. <td>none </td>
  348. <td>Key word used on command line.<br>
  349. opt->key = "map" ;</td>
  350. </tr>
  351. <tr>
  352. <td>type </td>
  353. <td>int </td>
  354. <td>YES </td>
  355. <td>none </td>
  356. <td>%Option type: <br>
  357. TYPE_STRING <br>
  358. TYPE_INTEGER <br>
  359. TYPE_DOUBLE <br>
  360. opt->type = TYPE_STRING ;</td>
  361. </tr>
  362. <tr>
  363. <td>Description </td>
  364. <td>char * </td>
  365. <td>YES </td>
  366. <td>none </td>
  367. <td>String describing option along with gettext macro for internationalization
  368. opt->description = _("Map name") ;</td>
  369. </tr>
  370. <tr>
  371. <td>answer </td>
  372. <td>char * </td>
  373. <td>NO </td>
  374. <td>NULL </td>
  375. <td>Default and parser-returned answer to an option.<br>
  376. opt->answer = "defaultmap" ;</td>
  377. </tr>
  378. <tr>
  379. <td>key_desc </td>
  380. <td>char * </td>
  381. <td>NO </td>
  382. <td>NULL </td>
  383. <td>Single word describing the key. Commas in this string denote
  384. to the parser that several comma-separated arguments are expected
  385. from the user as one answer. For example, if a pair of coordinates
  386. is desired, this element might be defined as follows.<br>
  387. opt->key_desc = "x,y" ; </td>
  388. </tr>
  389. <tr>
  390. <td>structure member</td>
  391. <td>C type </td>
  392. <td>required </td>
  393. <td>default </td>
  394. <td>description and example</td>
  395. </tr>
  396. <tr>
  397. <td>multiple </td>
  398. <td>int </td>
  399. <td>NO </td>
  400. <td>NO </td>
  401. <td>Indicates whether the user can provide multiple answers or not.
  402. YES and NO are defined in "gis.h" and should be used (NO is
  403. the default.) Multiple is used in conjunction with the answers
  404. structure member below. opt->multiple = NO ;</td>
  405. </tr>
  406. <tr>
  407. <td>answers </td>
  408. <td> </td>
  409. <td>NO </td>
  410. <td>NULL </td>
  411. <td>Multiple parser-returned answers to an option. 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 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.) opt->required = YES ;</td>
  421. </tr>
  422. <tr>
  423. <td>options </td>
  424. <td>char * </td>
  425. <td>NO </td>
  426. <td>NULL </td>
  427. <td>Approved values or range of values. <br>
  428. opt->options = "red,blue,white" ;<br>
  429. For integers and doubles, the following format is available: <br>
  430. opt->options = "0-1000" ;</td>
  431. </tr>
  432. <tr>
  433. <td>gisprompt</td>
  434. <td>char *</td>
  435. <td>NO</td>
  436. <td>NULL</td>
  437. <td>Interactive prompt guidance. There are three comma separated
  438. parts to this argument which guide the use of the standard GRASS
  439. file name prompting routines.<br>
  440. opt->gisprompt = "old,cell,raster" ;</td>
  441. </tr>
  442. <tr>
  443. <td>checker</td>
  444. <td>char *()</td>
  445. <td>NO</td>
  446. <td>NULL</td>
  447. <td>Routine to check the answer to an option<br>
  448. m opt->checker = my_routine() ;</td>
  449. </tr>
  450. </table>
  451. \section Description_of_Complex_Structure_Members Description of Complex Structure Members
  452. What follows are explanations of possibly confusing structure
  453. members. It is intended to clarify and supplement the structures table
  454. above.
  455. \subsection Answer_member_of_the_Flag_and_Option_structures Answer member of the Flag and Option structures
  456. The answer structure member serves two functions for GRASS commands
  457. that use the parser.
  458. <b>(1) To set the default answer to an option:</b>
  459. If a default state is desired for a programmer-defined option, the
  460. programmer may define the Option structure member "answer" before
  461. calling G_parser() in his module. After the G_parser() call, the
  462. answer member will hold this preset default value if the user did
  463. <i>not</i> enter an option that has the default answer member value.
  464. <b>(2) To obtain the command-line answer to an option or flag:</b>
  465. After a call to G_parser(), the answer member will contain one of two
  466. values:
  467. - (a) If the user provided an option, and answered this option on the
  468. command line, the default value of the answer member (as described
  469. above) is replaced by the user's input.
  470. - (b) If the user provided an option, but did <i>not</i> answer this
  471. option on the command line, the default is not used. The user may use
  472. the default answer to an option by withholding mention of the option
  473. on the command line. But if the user enters an option without an
  474. answer, the default answer member value will be replaced and set to a
  475. NULL value by G_parser().
  476. As an example, please review the use of answer members in the structures
  477. implemented in \ref Full_Module_Example.
  478. \subsection Multiple_and_Answers_Members Multiple and Answers Members
  479. The functionality of the answers structure member is reliant on the
  480. programmer's definition of the multiple structure member. If the multiple
  481. member is set to NO, the answer member is used to obtain the answer to an
  482. option as described above.
  483. If the multiple structure member is set to YES, the programmer has
  484. told G_parser() to capture multiple answers. Multiple answers are
  485. separated by <em>commas</em> on the command line after an option.
  486. <b>Note:</b> G_parser() does not recognize any character other than a
  487. comma to delimit multiple answers.
  488. After the programmer has set up an option to receive multiple answers,
  489. these the answers are stored in the answers member of the Option
  490. structure. The answers member is an array that contains each
  491. individual user-entered answer. The elements of this array are the
  492. type specified by the programmer using the type member. The answers
  493. array contains however many comma-delimited answers the user entered,
  494. followed (terminated) by a NULL array element.
  495. For example, here is a sample definition of an Option using multiple
  496. and answers structure members:
  497. \code
  498. opt->key ="option";
  499. opt->description = _("option example");
  500. opt->type = TYPE_INTEGER;
  501. opt->required = NO;
  502. opt->multiple = YES;
  503. \endcode
  504. The above definition would ask the user for multiple integer answers
  505. to the option. If in response to a routine that contained the above
  506. code, the user entered "option=1,3,8,15" on the command line, the
  507. answers 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 answers 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 key_desc structure member.
  541. If the multiple structure member were set to YES instead of NO in the
  542. example above, the answers are stored sequentially in the answers
  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 key_desc member, and for multiple
  547. answers.
  548. The answers 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 key_desc member) must be fulfilled completely.
  560. The overall function of the key_desc and multiple structure members is
  561. very similar. The key_desc 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 multiple member tells G_parser() to ask
  564. the user for multiple instances of the compound answer as defined by
  565. the format in the key_desc structure member.
  566. Another function of the key_desc 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 key_desc
  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 key_desc structure member before a call to G_parser(), the
  579. value of the key_desc member replaces "name". Thus, if the key_desc
  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 key_desc structure member can be used by the programmer to clarify
  587. the usage message as well as specify single or multiple required
  588. components of a single option answer.
  589. \subsection gisprompt_Member gisprompt Member
  590. The <em>gisprompt</em> Option structure item requires a bit more
  591. description. The three comma-separated (no spaces allowed)
  592. sub-arguments are defined as follows:
  593. - 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 which has "new" as the first component is given, the
  600. parser checks whether the entity (map, etc.) already exists.
  601. - 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. - 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
  622. There are standard options which ensures consistency in names
  623. and values of options. There are also standard flags which does the same.
  624. Options are defined by the G_define_standard_option() function
  625. and flags are defined by the function G_define_standard_flag().
  626. Both the options and flags are defined dynamically, so to get 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 an STD_OPT
  629. enum defined in the file gis.h. When the G_define_standard_option() function
  630. calls the G_define_option() function, so there is no need to call
  631. it separately. The same applies also for standard flags which
  632. uses the G_define_standard_flag() function and STD_OPT enum.
  633. Besides name and value standard options also defines label, description
  634. allowed values, their descriptions etc. The similar applies for the
  635. flags, too. After defining a standard option or flag you can still change
  636. individual parameters to exactly fit your needs.
  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 are presented by the usage message in the order that
  642. the programmer defines them using calls to G_define_option()
  643. and G_define_flag().
  644. \par Is a user required to use full option names?
  645. No. Users are required to type in only as many characters of an option name
  646. as is necessary to make the option choice unambiguous. If, for example,
  647. there are two options, "input=" and "output=", the following would be
  648. valid command line arguments:
  649. \verbatim
  650. # command i=map1 o=map2
  651. # command in=map1 out=map2
  652. \endverbatim
  653. \par Are options standardized at all?
  654. Yes. There are a few conventions. Options which identify a single input map
  655. are usually "map=", not "raster=" or "vector=". In the case of an
  656. input and output map the convention is: "input=xx output=yy". By passing
  657. the '--help' flag to existing GRASS commands, it is likely that you will
  658. find other conventions. The desire is to make it as easy as possible for the
  659. user to remember (or guess correctly) what the command line syntax is for a
  660. given command.
  661. To ensure maximal consistency, the most common options such as the options
  662. named above are defined as standard options and are available through
  663. G_define_standard_option() function. For flags you can use G_define_standard_flag()
  664. function.
  665. \par How does a programmer query for coordinates?
  666. There is standardized option G_OPT_M_COORDS which should be used for
  667. coordinates.
  668. See the source code for the GRASS commands <tt>r.drain</tt> or
  669. <tt>r.cost</tt> for examples.
  670. \par How does a programmer define that the option is a set of values?
  671. For any user input that requires a set of arguments (like a pair of
  672. map coordinates) the programmer specifies the number of arguments in
  673. the key_desc member of the Option structure. For example, if
  674. opt-&gt;key_desc was set to "x,y", the parser will require that the
  675. user enter a pair of arguments separated only by a comma.
  676. \note There is standardized option G_OPT_M_COORDS which should be used for
  677. coordinates.
  678. \par How is automatic prompting turned off?
  679. GRASS 4.0 introduced a new method for driving GRASS interactive and
  680. non-interactive modules as described in \ref
  681. Compiling_and_Installing_GRASS_Programs. Here is a short overview.
  682. For most modules a user runs a front-end module out of the GRASS bin
  683. directory which in turn looks for the existence of interactive and
  684. non-interactive versions of the module. If an interactive version
  685. exists and the user provided no command line arguments, then that
  686. version is executed.
  687. In such a situation, the parser's default interaction will never be
  688. seen by the user. A programmer using the parser is able to avoid the
  689. front-end's default search for a fully interactive version of the
  690. command by placing a call to G_disable_interactive() before
  691. calling G_parser() (see \ref Parser_Interface for details).
  692. */