parser_help.c 5.6 KB

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