GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
parser_help.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/parser_help.c
3
4 \brief GIS Library - Argument parsing functions (help)
5
6 SPDX-FileCopyrightText: 2001-2009, 2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL
10 \author Soeren Gebbert added Dec. 2009 WPS process_description document
11 */
12
13#include <stdio.h>
14#include <string.h>
15#include <stdlib.h>
16
17#include <grass/gis.h>
18#include <grass/glocale.h>
19
20#include "parser_local_proto.h"
21
22static void usage(FILE *fp, int markers);
23static void show_options(FILE *fp, int maxlen, const char *str);
24static int show(FILE *fp, const char *item, int len);
25
26/*!
27 \brief Command line help/usage message.
28
29 Calls to G_usage() allow the programmer to print the usage
30 message at any time. This will explain the allowed and required
31 command line input to the user. This description is given according
32 to the programmer's definitions for options and flags. This function
33 becomes useful when the user enters options and/or flags on the
34 command line that are syntactically valid to the parser, but
35 functionally invalid for the command (e.g. an invalid file name.)
36
37 For example, the parser logic doesn't directly support grouping
38 options. If two options be specified together or not at all, the
39 parser must be told that these options are not required and the
40 programmer must check that if one is specified the other must be as
41 well. If this additional check fails, then G_parser() will succeed,
42 but the programmer can then call G_usage() to print the standard
43 usage message and print additional information about how the two
44 options work together.
45 */
46void G_usage(void)
47{
48 usage(stderr, 0);
49}
50
51void G__usage_text(void)
52{
53 usage(stdout, 1);
54}
55
56static void usage(FILE *fp, int markers)
57{
58 struct Option *opt;
59 struct Flag *flag;
60 char item[256];
61 const char *key_desc;
62 int maxlen;
63 int len, n;
64 int new_prompt = 0;
65 int extensive = 0; /* include also less important parts */
66 int standard = 0; /* include also standard flags */
67 int detailed = 0; /* details for each flag and option */
68
70
71 if (!st->pgm_name) /* v.dave && r.michael */
72 st->pgm_name = G_program_name();
73 if (!st->pgm_name)
74 st->pgm_name = "??";
75
76 if (st->module_info.label || st->module_info.description) {
77 if (extensive)
78 fprintf(fp, "\n");
79 if (markers)
80 fprintf(fp, "{{{DESCRIPTION}}}\n");
81 if (extensive) {
82 fprintf(fp, "%s\n", _("Description:"));
83 if (st->module_info.label)
84 fprintf(fp, " %s\n", st->module_info.label);
85 if (st->module_info.description)
86 fprintf(fp, " %s\n", st->module_info.description);
87 }
88 else {
89 /* print label, if no label, try description */
90 /* no leading space without heading */
91 if (st->module_info.label)
92 fprintf(fp, "%s\n", st->module_info.label);
93 else if (st->module_info.description)
94 fprintf(fp, "%s\n", st->module_info.description);
95 }
96 }
97 if (extensive && st->module_info.keywords) {
98 fprintf(fp, "\n");
99 if (markers)
100 fprintf(fp, "{{{KEYWORDS}}}\n");
101 fprintf(fp, "%s\n ", _("Keywords:"));
103 fprintf(fp, "\n");
104 }
105
106 fprintf(fp, "\n");
107 if (markers)
108 fprintf(fp, "{{{USAGE}}}\n");
109 fprintf(fp, "%s\n ", _("Usage:"));
110
111 len = show(fp, st->pgm_name, 1);
112
113 /* Print flags */
114
115 if (st->n_flags) {
116 item[0] = ' ';
117 item[1] = '[';
118 item[2] = '-';
119 flag = &st->first_flag;
120 for (n = 3; flag != NULL; n++, flag = flag->next_flag)
121 item[n] = flag->key;
122 item[n++] = ']';
123 item[n] = 0;
124 len = show(fp, item, len);
125 }
126
127 maxlen = 0;
128 if (st->n_opts) {
129 opt = &st->first_option;
130 while (opt != NULL) {
131 if (opt->key_desc != NULL)
132 key_desc = opt->key_desc;
133 else if (opt->type == TYPE_STRING)
134 key_desc = "string";
135 else
136 key_desc = "value";
137
138 if (!opt->key) {
139 fprintf(stderr, "\n%s\n", _("ERROR: Option key not defined"));
141 }
142 n = strlen(opt->key);
143 if (n > maxlen)
144 maxlen = n;
145
146 G_strlcpy(item, " ", sizeof(item));
147 if (!opt->required)
148 G_strlcat(item, "[", sizeof(item));
149 G_strlcat(item, opt->key, sizeof(item));
150 G_strlcat(item, "=", sizeof(item));
151 G_strlcat(item, key_desc, sizeof(item));
152 if (opt->multiple) {
153 G_strlcat(item, "[,", sizeof(item));
154 G_strlcat(item, key_desc, sizeof(item));
155 G_strlcat(item, ",...]", sizeof(item));
156 }
157 if (!opt->required)
158 G_strlcat(item, "]", sizeof(item));
159
160 len = show(fp, item, len);
161
162 opt = opt->next_opt;
163 }
164 }
165 if (new_prompt) {
166 G_strlcpy(item, " [--overwrite]", sizeof(item));
167 len = show(fp, item, len);
168 }
169
170 G_strlcpy(item, " [--help]", sizeof(item));
171 len = show(fp, item, len);
172
173 G_strlcpy(item, " [--verbose]", sizeof(item));
174 len = show(fp, item, len);
175
176 G_strlcpy(item, " [--quiet]", sizeof(item));
177 len = show(fp, item, len);
178
179 G_strlcpy(item, " [--ui]", sizeof(item));
180 len = show(fp, item, len);
181
182 fprintf(fp, "\n");
183
184 /* Print help info for flags */
185
186 /* Show section only when there are flags.
187 * There are always the standard flags if we are printing those.
188 * There is no use case for the markers, so no way to decide if
189 * the marker for flags is mandatory and should be empty if it is
190 * okay for it to be missing like in the current implementation.
191 */
192 if (st->n_flags || standard) {
193 fprintf(fp, "\n");
194 if (markers)
195 fprintf(fp, "{{{FLAGS}}}\n");
196 fprintf(fp, "%s\n", _("Flags:"));
197 }
198
199 if (st->n_flags) {
200 flag = &st->first_flag;
201 while (flag != NULL) {
202 fprintf(fp, " -%c ", flag->key);
203
204 if (flag->label) {
205 fprintf(fp, "%s\n", flag->label);
206 if (detailed && flag->description)
207 fprintf(fp, " %s\n", flag->description);
208 }
209 else if (flag->description) {
210 fprintf(fp, "%s\n", flag->description);
211 }
212
213 flag = flag->next_flag;
214 }
215 }
216
217 if (standard) {
218 if (new_prompt)
219 fprintf(fp, " --o %s\n",
220 _("Allow output files to overwrite existing files"));
221
222 fprintf(fp, " --h %s\n", _("Print usage summary"));
223 fprintf(fp, " --v %s\n", _("Verbose module output"));
224 fprintf(fp, " --q %s\n", _("Quiet module output"));
225 fprintf(fp, " --qq %s\n", _("Super quiet module output"));
226 fprintf(fp, " --ui %s\n", _("Force launching GUI dialog"));
227 }
228
229 /* Print help info for options */
230
231 if (st->n_opts) {
232 fprintf(fp, "\n");
233 if (markers)
234 fprintf(fp, "{{{PARAMETERS}}}\n");
235 fprintf(fp, "%s\n", _("Parameters:"));
236 opt = &st->first_option;
237 while (opt != NULL) {
238 fprintf(fp, " %*s ", maxlen, opt->key);
239
240 if (opt->label) {
241 fprintf(fp, "%s\n", opt->label);
242 if (detailed && opt->description) {
243 fprintf(fp, " %*s %s\n", maxlen, " ", opt->description);
244 }
245 }
246 else if (opt->description) {
247 fprintf(fp, "%s\n", opt->description);
248 }
249
250 if (opt->options)
251 show_options(fp, maxlen, opt->options);
252 /*
253 fprintf (fp, " %*s options: %s\n", maxlen, " ",
254 _(opt->options)) ;
255 */
256 if (opt->def)
257 fprintf(fp, _(" %*s default: %s\n"), maxlen, " ", opt->def);
258
259 if (detailed && opt->descs) {
260 int i = 0;
261
262 while (opt->opts[i]) {
263 if (opt->descs[i])
264 fprintf(fp, " %*s %s: %s\n", maxlen, " ",
265 opt->opts[i], opt->descs[i]);
266
267 i++;
268 }
269 }
270
271 opt = opt->next_opt;
272 }
273 }
274}
275
276static void show_options(FILE *fp, int maxlen, const char *str)
277{
278 char *buff = G_store(str);
279 char *p1, *p2;
280 int totlen, len;
281
282 fprintf(fp, _(" %*s options: "), maxlen, " ");
283 totlen = maxlen + 13;
284 p1 = buff;
285 while ((p2 = strchr(p1, ','))) {
286 *p2 = '\0';
287 len = strlen(p1) + 1;
288 if ((len + totlen) > 76) {
289 totlen = maxlen + 13;
290 fprintf(fp, "\n %*s", maxlen + 13, " ");
291 }
292 fprintf(fp, "%s,", p1);
293 totlen += len;
294 p1 = p2 + 1;
295 }
296 len = strlen(p1);
297 if ((len + totlen) > 76)
298 fprintf(fp, "\n %*s", maxlen + 13, " ");
299 fprintf(fp, "%s\n", p1);
300
301 G_free(buff);
302}
303
304static int show(FILE *fp, const char *item, int len)
305{
306 int n;
307
308 n = strlen(item) + (len > 0);
309 if (n + len > 76) {
310 if (len)
311 fprintf(fp, "\n ");
312 len = 0;
313 }
314 fprintf(fp, "%s", item);
315 return n + len;
316}
#define NULL
Definition ccmath.h:32
const char * G_program_name(void)
Return module name.
Definition progrm_nme.c:26
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
size_t G_strlcat(char *, const char *, size_t)
Size-bounded string concatenation.
Definition strlcat.c:59
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
Definition strlcpy.c:54
#define TYPE_STRING
Definition gis.h:188
#define FALSE
Definition gis.h:79
#define _(str)
Definition glocale.h:10
void G__print_keywords(FILE *fd, void(*format)(FILE *, const char *), int newline)
Print list of keywords (internal use only)
Definition parser.c:927
int G__uses_new_gisprompt(void)
Definition parser.c:890
struct state * st
Definition parser.c:102
void G_usage(void)
Command line help/usage message.
Definition parser_help.c:46
void G__usage_text(void)
Definition parser_help.c:51
Structure that stores flag info.
Definition gis.h:591
Structure that stores option information.
Definition gis.h:560