GRASS 8 Programmer's Manual 8.6.0dev(2026)-ec90b1cd77
Loading...
Searching...
No Matches
parser_script.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/parser_script.c
3
4 \brief GIS Library - Argument parsing functions (script)
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
15#include <grass/gis.h>
16
17#include "parser_local_proto.h"
18
19/*!
20 \brief Generate Python script-like output
21 */
22void G__script(void)
23{
24 FILE *fp = stdout;
25 char *type;
26
27 fprintf(fp, "#!/usr/bin/env python3\n");
28 fprintf(fp, "##############################################################"
29 "##############\n");
30 fprintf(fp, "#\n");
31 fprintf(fp, "# MODULE: %s_wrapper\n", G_program_name());
32 fprintf(fp, "# AUTHOR(S): %s\n", G_whoami());
33 fprintf(fp, "# PURPOSE: Wrapper for %s\n", G_program_name());
34 fprintf(fp, "# SPDX-FileCopyrightText: %s %s\n", GRASS_VERSION_DATE,
35 G_whoami());
36 fprintf(fp, "# SPDX-FileCopyrightText: GRASS Development Team\n");
37 fprintf(fp, "# SPDX-License-Identifier: GPL-2.0-or-later\n");
38 fprintf(fp, "#\n");
39 fprintf(fp, "##############################################################"
40 "##############\n\n");
41
42 fprintf(fp, "\"\"\"Wraps %s to make it even better\"\"\"\n\n",
44
45 fprintf(fp, "# %%module\n");
46 if (st->module_info.label)
47 fprintf(fp, "# %% label: %s\n", st->module_info.label);
48 if (st->module_info.description)
49 fprintf(fp, "# %% description: %s\n", st->module_info.description);
50 if (st->module_info.keywords) {
51 int i;
52
53 for (i = 0; i < st->n_keys; i++) {
54 fprintf(fp, "# %% keyword: %s\n", st->module_info.keywords[i]);
55 }
56 }
57 fprintf(fp, "# %%end\n");
58
59 if (st->n_flags) {
60 struct Flag *flag;
61
62 for (flag = &st->first_flag; flag; flag = flag->next_flag) {
63 fprintf(fp, "# %%flag\n");
64 fprintf(fp, "# %% key: %c\n", flag->key);
65 if (flag->suppress_required)
66 fprintf(fp, "# %% suppress_required: yes\n");
67 if (flag->label)
68 fprintf(fp, "# %% label: %s\n", flag->label);
69 if (flag->description)
70 fprintf(fp, "# %% description: %s\n", flag->description);
71 if (flag->guisection)
72 fprintf(fp, "# %% guisection: %s\n", flag->guisection);
73 fprintf(fp, "# %%end\n");
74 }
75 }
76
77 if (st->n_opts) {
78 struct Option *opt;
79
80 for (opt = &st->first_option; opt; opt = opt->next_opt) {
81 switch (opt->type) {
82 case TYPE_INTEGER:
83 type = "integer";
84 break;
85 case TYPE_DOUBLE:
86 type = "double";
87 break;
88 case TYPE_STRING:
89 type = "string";
90 break;
91 default:
92 type = "string";
93 break;
94 }
95
96 fprintf(fp, "# %%option\n");
97 fprintf(fp, "# %% key: %s\n", opt->key);
98 fprintf(fp, "# %% type: %s\n", type);
99 fprintf(fp, "# %% required: %s\n", opt->required ? "yes" : "no");
100 fprintf(fp, "# %% multiple: %s\n", opt->multiple ? "yes" : "no");
101 if (opt->options)
102 fprintf(fp, "# %% options: %s\n", opt->options);
103 if (opt->key_desc)
104 fprintf(fp, "# %% key_desc: %s\n", opt->key_desc);
105 if (opt->label)
106 fprintf(fp, "# %% label: %s\n", opt->label);
107 if (opt->description)
108 fprintf(fp, "# %% description: %s\n", opt->description);
109 if (opt->descriptions)
110 fprintf(fp, "# %% descriptions: %s\n", opt->descriptions);
111 if (opt->answer)
112 fprintf(fp, "# %% answer: %s\n", opt->answer);
113 if (opt->gisprompt)
114 fprintf(fp, "# %% gisprompt: %s\n", opt->gisprompt);
115 if (opt->guisection)
116 fprintf(fp, "# %% guisection: %s\n", opt->guisection);
117 if (opt->guidependency)
118 fprintf(fp, "# %% guidependency: %s\n", opt->guidependency);
119 fprintf(fp, "# %%end\n");
120 }
121 }
122
123 fprintf(fp, "\nimport grass.script as gs\n\n");
124 fprintf(fp, "\ndef main():");
125 fprintf(
126 fp,
127 "\n \"\"\"Process command line parameters and run analysis\"\"\"");
128 fprintf(fp, "\n options, flags = gs.parser()");
129 fprintf(fp, "\n # Put your code here.");
130 fprintf(fp, "\n\n");
131 fprintf(fp, "\nif __name__ == \"__main__\":");
132 fprintf(fp, "\n main()\n");
133}
const char * G_program_name(void)
Return module name.
Definition progrm_nme.c:26
const char * G_whoami(void)
Gets user's name.
Definition gis/whoami.c:32
#define TYPE_STRING
Definition gis.h:188
#define TYPE_INTEGER
Definition gis.h:186
#define TYPE_DOUBLE
Definition gis.h:187
struct state * st
Definition parser.c:102
void G__script(void)
Generate Python script-like output.
Structure that stores flag info.
Definition gis.h:591
Structure that stores option information.
Definition gis.h:560
int type
Definition gis.h:562