parser_help.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*!
  2. * \file gis/parser_help.c
  3. *
  4. * \brief GIS Library - Argument parsing functions (help)
  5. *
  6. * (C) 2001-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Original author CERL
  12. * \author Soeren Gebbert added Dec. 2009 WPS process_description document
  13. */
  14. #include "parser_local_proto.h"
  15. static void show_options(int, const char *);
  16. static int show(const char *, int );
  17. /*!
  18. * \brief Command line help/usage message.
  19. *
  20. * Calls to G_usage() allow the programmer to print the usage
  21. * message at any time. This will explain the allowed and required
  22. * command line input to the user. This description is given according
  23. * to the programmer's definitions for options and flags. This function
  24. * becomes useful when the user enters options and/or flags on the
  25. * command line that are syntactically valid to the parser, but
  26. * functionally invalid for the command (e.g. an invalid file name.)
  27. *
  28. * For example, the parser logic doesn't directly support grouping
  29. * options. If two options be specified together or not at all, the
  30. * parser must be told that these options are not required and the
  31. * programmer must check that if one is specified the other must be as
  32. * well. If this additional check fails, then G_parser() will succeed,
  33. * but the programmer can then call G_usage() to print the standard
  34. * usage message and print additional information about how the two
  35. * options work together.
  36. */
  37. void G_usage(void)
  38. {
  39. struct Option *opt;
  40. struct Flag *flag;
  41. char item[256];
  42. const char *key_desc;
  43. int maxlen;
  44. int len, n;
  45. int new_prompt = 0;
  46. new_prompt = G__uses_new_gisprompt();
  47. if (!st->pgm_name) /* v.dave && r.michael */
  48. st->pgm_name = G_program_name();
  49. if (!st->pgm_name)
  50. st->pgm_name = "??";
  51. if (st->module_info.label || st->module_info.description) {
  52. fprintf(stderr, _("\nDescription:\n"));
  53. if (st->module_info.label)
  54. fprintf(stderr, " %s\n", st->module_info.label);
  55. if (st->module_info.description)
  56. fprintf(stderr, " %s\n", st->module_info.description);
  57. }
  58. if (st->module_info.keywords) {
  59. fprintf(stderr, _("\nKeywords:\n "));
  60. G__print_keywords(stderr, NULL);
  61. fprintf(stderr, "\n");
  62. }
  63. fprintf(stderr, _("\nUsage:\n "));
  64. len = show(st->pgm_name, 1);
  65. /* Print flags */
  66. if (st->n_flags) {
  67. item[0] = ' ';
  68. item[1] = '[';
  69. item[2] = '-';
  70. flag = &st->first_flag;
  71. for (n = 3; flag != NULL; n++, flag = flag->next_flag)
  72. item[n] = flag->key;
  73. item[n++] = ']';
  74. item[n] = 0;
  75. len = show(item, len);
  76. }
  77. maxlen = 0;
  78. if (st->n_opts) {
  79. opt = &st->first_option;
  80. while (opt != NULL) {
  81. if (opt->key_desc != NULL)
  82. key_desc = opt->key_desc;
  83. else if (opt->type == TYPE_STRING)
  84. key_desc = "string";
  85. else
  86. key_desc = "value";
  87. n = strlen(opt->key);
  88. if (n > maxlen)
  89. maxlen = n;
  90. strcpy(item, " ");
  91. if (!opt->required)
  92. strcat(item, "[");
  93. strcat(item, opt->key);
  94. strcat(item, "=");
  95. strcat(item, key_desc);
  96. if (opt->multiple) {
  97. strcat(item, "[,");
  98. strcat(item, key_desc);
  99. strcat(item, ",...]");
  100. }
  101. if (!opt->required)
  102. strcat(item, "]");
  103. len = show(item, len);
  104. opt = opt->next_opt;
  105. }
  106. }
  107. if (new_prompt) {
  108. strcpy(item, " [--overwrite]");
  109. len = show(item, len);
  110. }
  111. strcpy(item, " [--verbose]");
  112. len = show(item, len);
  113. strcpy(item, " [--quiet]");
  114. len = show(item, len);
  115. fprintf(stderr, "\n");
  116. /* Print help info for flags */
  117. fprintf(stderr, _("\nFlags:\n"));
  118. if (st->n_flags) {
  119. flag = &st->first_flag;
  120. while (flag != NULL) {
  121. fprintf(stderr, " -%c ", flag->key);
  122. if (flag->label) {
  123. fprintf(stderr, "%s\n", flag->label);
  124. if (flag->description)
  125. fprintf(stderr, " %s\n", flag->description);
  126. }
  127. else if (flag->description) {
  128. fprintf(stderr, "%s\n", flag->description);
  129. }
  130. flag = flag->next_flag;
  131. }
  132. }
  133. if (new_prompt)
  134. fprintf(stderr, " --o %s\n",
  135. _("Allow output files to overwrite existing files"));
  136. fprintf(stderr, " --v %s\n", _("Verbose module output"));
  137. fprintf(stderr, " --q %s\n", _("Quiet module output"));
  138. /* Print help info for options */
  139. if (st->n_opts) {
  140. fprintf(stderr, _("\nParameters:\n"));
  141. opt = &st->first_option;
  142. while (opt != NULL) {
  143. fprintf(stderr, " %*s ", maxlen, opt->key);
  144. if (opt->label) {
  145. fprintf(stderr, "%s\n", opt->label);
  146. if (opt->description) {
  147. fprintf(stderr, " %*s %s\n",
  148. maxlen, " ", opt->description);
  149. }
  150. }
  151. else if (opt->description) {
  152. fprintf(stderr, "%s\n", opt->description);
  153. }
  154. if (opt->options)
  155. show_options(maxlen, opt->options);
  156. /*
  157. fprintf (stderr, " %*s options: %s\n", maxlen, " ",
  158. _(opt->options)) ;
  159. */
  160. if (opt->def)
  161. fprintf(stderr, _(" %*s default: %s\n"), maxlen, " ",
  162. opt->def);
  163. if (opt->descs) {
  164. int i = 0;
  165. while (opt->opts[i]) {
  166. if (opt->descs[i])
  167. fprintf(stderr, " %*s %s: %s\n",
  168. maxlen, " ", opt->opts[i], opt->descs[i]);
  169. i++;
  170. }
  171. }
  172. opt = opt->next_opt;
  173. }
  174. }
  175. }
  176. static void show_options(int maxlen, const char *str)
  177. {
  178. char *buff = G_store(str);
  179. char *p1, *p2;
  180. int totlen, len;
  181. fprintf(stderr, _(" %*s options: "), maxlen, " ");
  182. totlen = maxlen + 13;
  183. p1 = buff;
  184. while ((p2 = G_index(p1, ','))) {
  185. *p2 = '\0';
  186. len = strlen(p1) + 1;
  187. if ((len + totlen) > 76) {
  188. totlen = maxlen + 13;
  189. fprintf(stderr, "\n %*s", maxlen + 13, " ");
  190. }
  191. fprintf(stderr, "%s,", p1);
  192. totlen += len;
  193. p1 = p2 + 1;
  194. }
  195. len = strlen(p1);
  196. if ((len + totlen) > 76)
  197. fprintf(stderr, "\n %*s", maxlen + 13, " ");
  198. fprintf(stderr, "%s\n", p1);
  199. G_free(buff);
  200. }
  201. static int show(const char *item, int len)
  202. {
  203. int n;
  204. n = strlen(item) + (len > 0);
  205. if (n + len > 76) {
  206. if (len)
  207. fprintf(stderr, "\n ");
  208. len = 0;
  209. }
  210. fprintf(stderr, "%s", item);
  211. return n + len;
  212. }