gislib_cmdline_parsing.dox 26 KB

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