20 #include "parser_local_proto.h"
22 static void print_python_short_flag(FILE *
file,
const char *key,
23 const char *label,
const char *description,
25 static void print_python_long_flag(FILE *
file,
const char *key,
26 const char *label,
const char *description,
28 static void print_python_option(FILE *
file,
const struct Option *opt,
29 const char *indent,
bool tools_api);
30 static void print_python_example(FILE *
file,
const char *python_function,
31 const char *output_format_default,
32 const char *indent,
bool tools_api);
33 static void print_python_tuple(FILE *
file,
const char *type,
int num_items);
35 void print_python_short_flag(FILE *
file,
const char *key,
const char *label,
36 const char *description,
const char *indent)
38 fprintf(
file,
"%s", indent);
40 fprintf(
file,
"**%s**", key);
41 fprintf(
file, MD_NEWLINE);
44 fprintf(
file,
"%s", indent);
47 fprintf(
file, MD_NEWLINE);
50 if (description !=
NULL) {
51 fprintf(
file,
"%s", indent);
57 void print_python_long_flag(FILE *
file,
const char *key,
const char *label,
58 const char *description,
const char *indent)
60 fprintf(
file,
"%s**%s**: bool, *optional*", indent, key);
61 fprintf(
file, MD_NEWLINE);
64 fprintf(
file,
"%s", indent);
67 fprintf(
file, MD_NEWLINE);
70 if (description !=
NULL) {
71 fprintf(
file,
"%s", indent);
74 fprintf(
file, MD_NEWLINE);
77 fprintf(
file,
"%s", indent);
79 const char *flag_default =
"*None*";
80 fprintf(
file,
"Default: %s", flag_default);
83 void print_python_tuple(FILE *
file,
const char *type,
int num_items)
85 fprintf(
file,
"tuple[%s", type);
86 for (
int i = 1; i < num_items; i++) {
87 fprintf(
file,
", %s", type);
92 void print_python_option(FILE *
file,
const struct Option *opt,
93 const char *indent,
bool tools_api)
114 char prompt_description[KEYLENGTH];
120 type =
"str | io.StringIO";
124 fprintf(
file,
"%s**%s** : ", indent, opt->
key);
128 fprintf(
file,
"list[");
129 print_python_tuple(
file, type, tuple_items);
130 fprintf(
file,
"] | ");
131 print_python_tuple(
file, type, tuple_items);
132 fprintf(
file,
" | list[%s] | str", type);
135 if (strcmp(type,
"str")) {
140 fprintf(
file,
"%s | list[%s] | str", type, type);
143 fprintf(
file,
"%s | list[%s]", type, type);
147 else if (tuple_items) {
148 print_python_tuple(
file, type, tuple_items);
149 fprintf(
file,
" | list[%s] | str", type);
152 fprintf(
file,
"%s", type);
155 fprintf(
file,
", *required*");
158 fprintf(
file,
", *optional*");
161 fprintf(
file, MD_NEWLINE);
164 fprintf(
file,
"%s", indent);
170 fprintf(
file, MD_NEWLINE);
173 fprintf(
file,
"%s", indent);
178 fprintf(
file, MD_NEWLINE);
180 fprintf(
file,
"%s", indent);
182 fprintf(
file,
"%s: ",
_(
"Used as"));
185 if (strcmp(age,
"new") == 0)
186 fprintf(
file,
"output, ");
187 else if (strcmp(age,
"old") == 0)
188 fprintf(
file,
"input, ");
193 fprintf(
file,
"%s", prompt_description);
203 fprintf(
file, MD_NEWLINE);
205 fprintf(
file,
"%s", indent);
207 fprintf(
file,
"%s: *",
_(
"Allowed values"));
215 while (opt->
opts[i]) {
217 fprintf(
file, MD_NEWLINE);
219 fprintf(
file,
"%s", indent);
220 char *thumbnails =
NULL;
222 if (strcmp(opt->
gisprompt,
"old,colortable,colortable") ==
224 thumbnails =
"colortables";
225 else if (strcmp(opt->
gisprompt,
"old,barscale,barscale") ==
227 thumbnails =
"barscales";
229 "old,northarrow,northarrow") == 0)
230 thumbnails =
"northarrows";
234 fprintf(
file,
" ", opt->
opts[i],
235 thumbnails, opt->
opts[i]);
244 fprintf(
file,
"**: ");
252 fprintf(
file, MD_NEWLINE);
254 fprintf(
file,
"%s", indent);
256 fprintf(
file,
"%s:",
_(
"Default"));
263 void print_python_example(FILE *
file,
const char *python_function,
264 const char *output_format_default,
const char *indent,
267 fprintf(
file,
"\n%sExample:\n", indent);
269 fprintf(
file,
"\n%s```python\n", indent);
270 bool first_parameter_printed =
false;
274 fprintf(
file,
"%stools = Tools()\n", indent);
275 fprintf(
file,
"%stools.%s(", indent, tool_name);
279 fprintf(
file,
"%sgs.%s(\"%s\"", indent, python_function,
st->pgm_name);
280 first_parameter_printed =
true;
283 const struct Option *first_required_rule_option =
289 opt = &
st->first_option;
291 while (opt !=
NULL) {
309 if (opt->
required || first_required_rule_option == opt ||
310 (strcmp(opt->
key,
"format") == 0 && output_format_default)) {
311 if (first_parameter_printed) {
314 fprintf(
file,
"%s=", opt->
key);
338 if (output_format_default && strcmp(opt->
key,
"format") == 0) {
339 fprintf(
file,
"\"%s\"", output_format_default);
343 fprintf(
file,
"%s", value);
346 fprintf(
file,
"\"%s\"", value);
354 fprintf(
file,
"0.0");
360 first_parameter_printed =
true;
366 fprintf(
file,
")\n%s```\n", indent);
375 bool output_format_option =
false;
376 const char *output_format_default =
NULL;
377 bool shell_eval_flag =
false;
378 const char *python_function =
NULL;
383 opt = &
st->first_option;
384 while (opt !=
NULL) {
385 if (strcmp(opt->
key,
"format") == 0) {
388 while (opt->
opts[i]) {
389 if (strcmp(opt->
opts[i],
"csv") == 0)
390 output_format_default =
"csv";
391 if (strcmp(opt->
opts[i],
"json") == 0) {
392 output_format_default =
"json";
398 if (output_format_default) {
399 output_format_option =
true;
407 flag = &
st->first_flag;
408 while (
st->n_flags && flag !=
NULL) {
409 if (flag->
key ==
'g') {
410 shell_eval_flag =
true;
416 bool first_parameter_printed =
false;
420 fprintf(
file,
"%s*grass.tools.Tools.%s*(", indent, tool_name);
424 if (output_format_option || (!new_prompt && shell_eval_flag)) {
425 python_function =
"parse_command";
431 python_function =
"run_command";
433 fprintf(
file,
"%s*grass.script.%s*(\"***%s***\",", indent,
434 python_function,
st->pgm_name);
436 first_parameter_printed =
true;
440 opt = &
st->first_option;
442 while (opt !=
NULL) {
443 if (first_parameter_printed) {
444 fprintf(
file,
"%s ", indent);
447 fprintf(
file,
"**%s**=*None*", opt->
key);
450 fprintf(
file,
"**%s**", opt->
key);
462 fprintf(
file,
"*\"");
464 fprintf(
file,
"\"*");
467 fprintf(
file,
",\n");
468 first_parameter_printed =
true;
474 flag = &
st->first_flag;
475 fprintf(
file,
"%s **flags**=*None*,\n", indent);
478 const char *flag_default =
"*None*";
480 fprintf(
file,
"%s **overwrite**=%s,\n", indent, flag_default);
482 fprintf(
file,
"%s **verbose**=%s,\n", indent, flag_default);
483 fprintf(
file,
"%s **quiet**=%s,\n", indent, flag_default);
484 fprintf(
file,
"%s **superquiet**=%s)\n", indent, flag_default);
486 print_python_example(
file, python_function, output_format_default, indent,
490 "\n%sThis grass.tools API is experimental in version 8.5 "
491 "and expected to be stable in version 8.6.\n",
507 opt = &
st->first_option;
508 while (opt !=
NULL) {
509 print_python_option(
file, opt, indent, tools_api);
511 fprintf(
file, MD_NEWLINE);
518 fprintf(
file,
"%s**flags** : str, *optional*", indent);
519 fprintf(
file, MD_NEWLINE);
521 fprintf(
file,
"%s", indent);
523 fprintf(
file,
"Allowed values: ");
524 flag = &
st->first_flag;
525 while (
st->n_flags && flag !=
NULL) {
526 fprintf(
file,
"*%s*", &flag->
key);
531 fprintf(
file, MD_NEWLINE);
533 flag = &
st->first_flag;
534 while (
st->n_flags && flag !=
NULL) {
537 fprintf(
file, MD_NEWLINE);
543 print_python_long_flag(
545 _(
"Allow output files to overwrite existing files"), indent);
546 fprintf(
file, MD_NEWLINE);
550 print_python_long_flag(
file,
"verbose",
NULL,
_(
"Verbose module output"),
552 fprintf(
file, MD_NEWLINE);
554 print_python_long_flag(
file,
"quiet",
NULL,
_(
"Quiet module output"),
556 fprintf(
file, MD_NEWLINE);
558 print_python_long_flag(
file,
"superquiet",
NULL,
559 _(
"Very quiet module output"), indent);
560 fprintf(
file, MD_NEWLINE);
void G_free(void *)
Free allocated memory.
void G_free_tokens(char **)
Free memory allocated to tokens.
char * G_strchg(char *, char, char)
Replace all occurrences of character in string bug with new.
int G_strncasecmp(const char *, const char *, int)
String compare ignoring case (upper or lower) - limited number of characters.
char * G_chop(char *)
Chop leading and trailing white spaces.
char * G_store(const char *)
Copy string to allocated memory.
char ** G_tokenize(const char *, const char *)
Tokenize string.
int G__uses_new_gisprompt(void)
void G__split_gisprompt(const char *gisprompt, char *age, char *element, char *desc)
const struct Option * G__first_required_option_from_rules(void)
void G__md_print_escaped(FILE *f, const char *str)
int G__option_num_tuple_items(const struct Option *opt)
Get number of tuple items if option is a tuple.
void G__md_print_escaped_for_options(FILE *f, const char *str)
void G__md_print_python_long_version(FILE *file, const char *indent, bool tools_api)
void G__md_print_python_short_version(FILE *file, const char *indent, bool tools_api)
Structure that stores flag info.
Structure that stores option information.