GRASS GIS 7 Programmer's Manual  7.0.3(2016)-r00000
parser_help.c
Go to the documentation of this file.
1 
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 
19 #include <grass/gis.h>
20 #include <grass/glocale.h>
21 
22 #include "parser_local_proto.h"
23 
24 static void usage(FILE *fp, int markers);
25 static void show_options(FILE *fp, int maxlen, const char *str);
26 static int show(FILE *fp, const char *item, int len);
27 
48 void G_usage(void)
49 {
50  usage(stderr, 0);
51 }
52 
53 void G__usage_text(void)
54 {
55  usage(stdout, 1);
56 }
57 
58 static void usage(FILE *fp, int markers)
59 {
60  struct Option *opt;
61  struct Flag *flag;
62  char item[256];
63  const char *key_desc;
64  int maxlen;
65  int len, n;
66  int new_prompt = 0;
67 
68  new_prompt = G__uses_new_gisprompt();
69 
70  if (!st->pgm_name) /* v.dave && r.michael */
71  st->pgm_name = G_program_name();
72  if (!st->pgm_name)
73  st->pgm_name = "??";
74 
75  if (st->module_info.label || st->module_info.description) {
76  fprintf(fp, "\n");
77  if (markers)
78  fprintf(fp, "{{{DESCRIPTION}}}\n");
79  fprintf(fp, "%s\n", _("Description:"));
80  if (st->module_info.label)
81  fprintf(fp, " %s\n", st->module_info.label);
82  if (st->module_info.description)
83  fprintf(fp, " %s\n", st->module_info.description);
84  }
85  if (st->module_info.keywords) {
86  fprintf(fp, "\n");
87  if (markers)
88  fprintf(fp, "{{{KEYWORDS}}}\n");
89  fprintf(fp, "%s\n ", _("Keywords:"));
91  fprintf(fp, "\n");
92  }
93 
94  fprintf(fp, "\n");
95  if (markers)
96  fprintf(fp, "{{{USAGE}}}\n");
97  fprintf(fp, "%s\n ", _("Usage:"));
98 
99  len = show(fp, st->pgm_name, 1);
100 
101  /* Print flags */
102 
103  if (st->n_flags) {
104  item[0] = ' ';
105  item[1] = '[';
106  item[2] = '-';
107  flag = &st->first_flag;
108  for (n = 3; flag != NULL; n++, flag = flag->next_flag)
109  item[n] = flag->key;
110  item[n++] = ']';
111  item[n] = 0;
112  len = show(fp, item, len);
113  }
114 
115  maxlen = 0;
116  if (st->n_opts) {
117  opt = &st->first_option;
118  while (opt != NULL) {
119  if (opt->key_desc != NULL)
120  key_desc = opt->key_desc;
121  else if (opt->type == TYPE_STRING)
122  key_desc = "string";
123  else
124  key_desc = "value";
125 
126  if (!opt->key) {
127  fprintf(stderr, "\n%s\n", _("ERROR: Option key not defined"));
128  exit(EXIT_FAILURE);
129  }
130  n = strlen(opt->key);
131  if (n > maxlen)
132  maxlen = n;
133 
134  strcpy(item, " ");
135  if (!opt->required)
136  strcat(item, "[");
137  strcat(item, opt->key);
138  strcat(item, "=");
139  strcat(item, key_desc);
140  if (opt->multiple) {
141  strcat(item, "[,");
142  strcat(item, key_desc);
143  strcat(item, ",...]");
144  }
145  if (!opt->required)
146  strcat(item, "]");
147 
148  len = show(fp, item, len);
149 
150  opt = opt->next_opt;
151  }
152  }
153  if (new_prompt) {
154  strcpy(item, " [--overwrite]");
155  len = show(fp, item, len);
156  }
157 
158  strcpy(item, " [--help]");
159  len = show(fp, item, len);
160 
161  strcpy(item, " [--verbose]");
162  len = show(fp, item, len);
163 
164  strcpy(item, " [--quiet]");
165  len = show(fp, item, len);
166 
167  strcpy(item, " [--ui]");
168  len = show(fp, item, len);
169 
170  fprintf(fp, "\n");
171 
172  /* Print help info for flags */
173 
174  fprintf(fp, "\n");
175  if (markers)
176  fprintf(fp, "{{{FLAGS}}}\n");
177  fprintf(fp, "%s\n", _("Flags:"));
178 
179  if (st->n_flags) {
180  flag = &st->first_flag;
181  while (flag != NULL) {
182  fprintf(fp, " -%c ", flag->key);
183 
184  if (flag->label) {
185  fprintf(fp, "%s\n", flag->label);
186  if (flag->description)
187  fprintf(fp, " %s\n", flag->description);
188 
189  }
190  else if (flag->description) {
191  fprintf(fp, "%s\n", flag->description);
192  }
193 
194  flag = flag->next_flag;
195  }
196  }
197 
198  if (new_prompt)
199  fprintf(fp, " --o %s\n",
200  _("Allow output files to overwrite existing files"));
201 
202  fprintf(fp, " --h %s\n", _("Print usage summary"));
203  fprintf(fp, " --v %s\n", _("Verbose module output"));
204  fprintf(fp, " --q %s\n", _("Quiet module output"));
205  fprintf(fp, " --ui %s\n", _("Force launching GUI dialog"));
206 
207  /* Print help info for options */
208 
209  if (st->n_opts) {
210  fprintf(fp, "\n");
211  if (markers)
212  fprintf(fp, "{{{PARAMETERS}}}\n");
213  fprintf(fp, "%s\n", _("Parameters:"));
214  opt = &st->first_option;
215  while (opt != NULL) {
216  fprintf(fp, " %*s ", maxlen, opt->key);
217 
218  if (opt->label) {
219  fprintf(fp, "%s\n", opt->label);
220  if (opt->description) {
221  fprintf(fp, " %*s %s\n",
222  maxlen, " ", opt->description);
223  }
224  }
225  else if (opt->description) {
226  fprintf(fp, "%s\n", opt->description);
227  }
228 
229  if (opt->options)
230  show_options(fp, maxlen, opt->options);
231  /*
232  fprintf (fp, " %*s options: %s\n", maxlen, " ",
233  _(opt->options)) ;
234  */
235  if (opt->def)
236  fprintf(fp, _(" %*s default: %s\n"), maxlen, " ",
237  opt->def);
238 
239  if (opt->descs) {
240  int i = 0;
241 
242  while (opt->opts[i]) {
243  if (opt->descs[i])
244  fprintf(fp, " %*s %s: %s\n",
245  maxlen, " ", opt->opts[i], opt->descs[i]);
246 
247  i++;
248  }
249  }
250 
251  opt = opt->next_opt;
252  }
253  }
254 }
255 
256 static void show_options(FILE *fp, int maxlen, const char *str)
257 {
258  char *buff = G_store(str);
259  char *p1, *p2;
260  int totlen, len;
261 
262  fprintf(fp, _(" %*s options: "), maxlen, " ");
263  totlen = maxlen + 13;
264  p1 = buff;
265  while ((p2 = strchr(p1, ','))) {
266  *p2 = '\0';
267  len = strlen(p1) + 1;
268  if ((len + totlen) > 76) {
269  totlen = maxlen + 13;
270  fprintf(fp, "\n %*s", maxlen + 13, " ");
271  }
272  fprintf(fp, "%s,", p1);
273  totlen += len;
274  p1 = p2 + 1;
275  }
276  len = strlen(p1);
277  if ((len + totlen) > 76)
278  fprintf(fp, "\n %*s", maxlen + 13, " ");
279  fprintf(fp, "%s\n", p1);
280 
281  G_free(buff);
282 }
283 
284 static int show(FILE *fp, const char *item, int len)
285 {
286  int n;
287 
288  n = strlen(item) + (len > 0);
289  if (n + len > 76) {
290  if (len)
291  fprintf(fp, "\n ");
292  len = 0;
293  }
294  fprintf(fp, "%s", item);
295  return n + len;
296 }
int G__uses_new_gisprompt(void)
Definition: parser.c:779
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
#define NULL
Definition: ccmath.h:32
void G__usage_text(void)
Definition: parser_help.c:53
struct state * st
Definition: parser.c:101
const char * G_program_name(void)
Return module name.
Definition: progrm_nme.c:27
void G_usage(void)
Command line help/usage message.
Definition: parser_help.c:48
void G__print_keywords(FILE *fd, void(*format)(FILE *, const char *))
Print list of keywords (internal use only)
Definition: parser.c:815
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149