GRASS 8 Programmer's Manual 8.6.0dev(2026)-e8fd76f1e2
Loading...
Searching...
No Matches
parser_rest.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/parser_rest.c
3
4 \brief GIS Library - Argument parsing functions (reStructuredText output)
5
6 SPDX-FileCopyrightText: 2012 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Luca Delucchi
10 */
11#include <stdio.h>
12#include <string.h>
13
14#include <grass/gis.h>
15#include <grass/glocale.h>
16
17#include "parser_local_proto.h"
18
19static void print_escaped_for_rest(FILE *f, const char *str);
20static void print_escaped_for_rest_options(FILE *f, const char *str);
21
22/*!
23 \brief Print module usage description in reStructuredText format.
24 */
25void G__usage_rest(void)
26{
27 struct Option *opt;
28 struct Flag *flag;
29 const char *type;
30 int new_prompt = 0;
31 unsigned int s;
32
34
35 if (!st->pgm_name)
36 st->pgm_name = G_program_name();
37 if (!st->pgm_name)
38 st->pgm_name = "??";
39
40 fprintf(stdout, "=================");
41 for (s = 0; s <= strlen(st->pgm_name); s++) {
42 fprintf(stdout, "=");
43 }
44 fprintf(stdout, "\n");
45 fprintf(stdout, "%s - GRASS manual\n", st->pgm_name);
46 fprintf(stdout, "=================");
47 for (s = 0; s <= strlen(st->pgm_name); s++) {
48 fprintf(stdout, "=");
49 }
50 fprintf(stdout, "\n\n");
51
52 fprintf(stdout, ".. figure:: grass_logo.png\n");
53 fprintf(stdout, " :align: center\n");
54 fprintf(stdout, " :alt: GRASS logo\n\n");
55
56 fprintf(stdout, "%s\n----\n", _("NAME"));
57 fprintf(stdout, "**%s**", st->pgm_name);
58
59 if (st->module_info.label || st->module_info.description)
60 fprintf(stdout, " - ");
61
62 if (st->module_info.label)
63 fprintf(stdout, "%s\n\n", st->module_info.label);
64
65 if (st->module_info.description)
66 fprintf(stdout, "%s\n", st->module_info.description);
67
68 fprintf(stdout, "\n%s\n----------------------\n", _("KEYWORDS"));
69 if (st->module_info.keywords) {
71 fprintf(stdout, "\n");
72 }
73 fprintf(stdout, "\n%s\n----------------------\n", _("SYNOPSIS"));
74 fprintf(stdout, "**%s**\n\n", st->pgm_name);
75 fprintf(stdout, "**%s** --help\n\n", st->pgm_name);
76
77 fprintf(stdout, "**%s**", st->pgm_name);
78
79 /* print short version first */
80 if (st->n_flags) {
81 flag = &st->first_flag;
82 fprintf(stdout, " [**-");
83 while (flag != NULL) {
84 fprintf(stdout, "%c", flag->key);
85 flag = flag->next_flag;
86 }
87 fprintf(stdout, "**] ");
88 }
89 else
90 fprintf(stdout, " ");
91
92 if (st->n_opts) {
93 opt = &st->first_option;
94
95 while (opt != NULL) {
96 if (opt->key_desc != NULL)
97 type = opt->key_desc;
98 else
99 switch (opt->type) {
100 case TYPE_INTEGER:
101 type = "integer";
102 break;
103 case TYPE_DOUBLE:
104 type = "float";
105 break;
106 case TYPE_STRING:
107 type = "string";
108 break;
109 default:
110 type = "string";
111 break;
112 }
113 if (!opt->required)
114 fprintf(stdout, " [");
115 fprintf(stdout, "**%s** = *%s*", opt->key, type);
116 if (opt->multiple) {
117 fprintf(stdout, " [, *%s* ,...]", type);
118 }
119 if (!opt->required)
120 fprintf(stdout, "] ");
121
122 opt = opt->next_opt;
123 fprintf(stdout, " ");
124 }
125 }
126 if (new_prompt)
127 fprintf(stdout, " [-- **overwrite**] ");
128
129 fprintf(stdout, " [-- **verbose**] ");
130 fprintf(stdout, " [-- **quiet**] ");
131
132 fprintf(stdout, "\n");
133
134 /* now long version */
135 fprintf(stdout, "\n");
136 if (st->n_flags || new_prompt) {
137 flag = &st->first_flag;
138 fprintf(stdout, "%s:\n~~~~~~\n", _("Flags"));
139 while (st->n_flags && flag != NULL) {
140 fprintf(stdout, "**-%c**\n", flag->key);
141
142 if (flag->label) {
143 fprintf(stdout, " %s", flag->label);
144 }
145
146 if (flag->description) {
147 fprintf(stdout, " %s", flag->description);
148 }
149
150 flag = flag->next_flag;
151 fprintf(stdout, "\n");
152 }
153 if (new_prompt) {
154 fprintf(stdout, "-- **overwrite**\n");
155 fprintf(stdout, " %s\n",
156 _("Allow output files to overwrite existing files"));
157 }
158
159 fprintf(stdout, "-- **verbose**\n");
160 fprintf(stdout, " %s\n", _("Verbose module output"));
161
162 fprintf(stdout, "-- **quiet**\n");
163 fprintf(stdout, " %s\n", _("Quiet module output"));
164
165 fprintf(stdout, "\n");
166 }
167
168 fprintf(stdout, "\n");
169 if (st->n_opts) {
170 opt = &st->first_option;
171 fprintf(stdout, "%s:\n~~~~~~~~~~~\n", _("Parameters"));
172
173 while (opt != NULL) {
174 /* TODO: make this a enumeration type? */
175 if (opt->key_desc != NULL)
176 type = opt->key_desc;
177 else
178 switch (opt->type) {
179 case TYPE_INTEGER:
180 type = "integer";
181 break;
182 case TYPE_DOUBLE:
183 type = "float";
184 break;
185 case TYPE_STRING:
186 type = "string";
187 break;
188 default:
189 type = "string";
190 break;
191 }
192 fprintf(stdout, "**%s** = *%s*", opt->key, type);
193 if (opt->multiple) {
194 fprintf(stdout, " [, *%s* ,...]", type);
195 }
196 /* fprintf(stdout, "*"); */
197 if (opt->required) {
198 fprintf(stdout, " **[required]**");
199 }
200 fprintf(stdout, "\n\n");
201 if (opt->label) {
202 fprintf(stdout, "\t");
203 print_escaped_for_rest(stdout, opt->label);
204 /* fprintf(stdout, " %s\n", opt->label); */
205 fprintf(stdout, "\n\n");
206 }
207 if (opt->description) {
208 fprintf(stdout, "\t");
209 print_escaped_for_rest(stdout, opt->description);
210 /* fprintf(stdout, " %s\n", opt->description); */
211 fprintf(stdout, "\n\n");
212 }
213
214 if (opt->options) {
215 fprintf(stdout, "\t%s: *", _("Options"));
216 print_escaped_for_rest_options(stdout, opt->options);
217 /* fprintf(stdout, "%s", opt->options); */
218 fprintf(stdout, "*\n\n");
219 }
220
221 if (opt->def) {
222 fprintf(stdout, "\t%s:", _("Default"));
223 /* TODO check if value is empty
224 if (!opt->def.empty()){ */
225 fprintf(stdout, " *");
226 print_escaped_for_rest(stdout, opt->def);
227 /* fprintf(stdout,"%s", opt->def); */
228 fprintf(stdout, "*\n\n");
229 /* } */
230 fprintf(stdout, "\n\n");
231 }
232
233 if (opt->descs) {
234 int i = 0;
235
236 while (opt->opts[i]) {
237 if (opt->descs[i]) {
238 fprintf(stdout, "\t\t**");
239 print_escaped_for_rest(stdout, opt->opts[i]);
240 /*fprintf(stdout,"%s", opt->opts[i]); */
241 fprintf(stdout, "** : ");
242 print_escaped_for_rest(stdout, opt->descs[i]);
243 /* fprintf(stdout, "%s\n", opt->descs[i]); */
244 fprintf(stdout, "\n\n");
245 }
246 i++;
247 }
248 }
249
250 opt = opt->next_opt;
251 fprintf(stdout, "\n");
252 }
253 fprintf(stdout, "\n");
254 }
255}
256
257/*!
258 * \brief Format text for reStructuredText output
259 */
260#define do_escape(c, escaped) \
261 case c: \
262 fputs(escaped, f); \
263 break
264void print_escaped_for_rest(FILE *f, const char *str)
265{
266 const char *s;
267
268 for (s = str; *s; s++) {
269 switch (*s) {
270 do_escape('\n', "\n\n");
271 default:
272 fputc(*s, f);
273 }
274 }
275}
276
277void print_escaped_for_rest_options(FILE *f, const char *str)
278{
279 const char *s;
280
281 for (s = str; *s; s++) {
282 switch (*s) {
283 do_escape('\n', "\n\n");
284 default:
285 fputc(*s, f);
286 }
287 }
288}
289
290#undef do_escape
#define NULL
Definition ccmath.h:32
const char * G_program_name(void)
Return module name.
Definition progrm_nme.c:26
#define TYPE_STRING
Definition gis.h:188
#define TYPE_INTEGER
Definition gis.h:186
#define FALSE
Definition gis.h:79
#define TYPE_DOUBLE
Definition gis.h:187
#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
#define do_escape(c, escaped)
Format text for reStructuredText output.
void G__usage_rest(void)
Print module usage description in reStructuredText format.
Definition parser_rest.c:25
Structure that stores flag info.
Definition gis.h:591
Structure that stores option information.
Definition gis.h:560