GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
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  (C) 2001-2009, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Original author CERL
12  \author Soeren Gebbert added Dec. 2009 WPS process_description document
13  */
14 
15 #include <stdio.h>
16 
17 #include <grass/gis.h>
18 
19 #include "parser_local_proto.h"
20 
21 /*!
22  \brief Generate Python script-like output
23  */
24 void G__script(void)
25 {
26  FILE *fp = stdout;
27  char *type;
28 
29  fprintf(fp, "#!/usr/bin/env python3\n");
30  fprintf(fp, "##############################################################"
31  "##############\n");
32  fprintf(fp, "#\n");
33  fprintf(fp, "# MODULE: %s_wrapper\n", G_program_name());
34  fprintf(fp, "# AUTHOR(S): %s\n", G_whoami());
35  fprintf(fp, "# PURPOSE: Wrapper for %s\n", G_program_name());
36  fprintf(fp,
37  "# COPYRIGHT: (C) %s by %s, and the GRASS Development Team\n",
38  GRASS_VERSION_DATE, G_whoami());
39  fprintf(fp, "#\n");
40  fprintf(fp, "# This program is free software; you can redistribute it "
41  "and/or modify\n");
42  fprintf(fp, "# it under the terms of the GNU General Public License as "
43  "published by\n");
44  fprintf(fp, "# the Free Software Foundation; either version 2 of the "
45  "License, or\n");
46  fprintf(fp, "# (at your option) any later version.\n");
47  fprintf(fp, "#\n");
48  fprintf(
49  fp,
50  "# This program is distributed in the hope that it will be useful,\n");
51  fprintf(
52  fp,
53  "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
54  fprintf(
55  fp,
56  "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
57  fprintf(fp, "# GNU General Public License for more details.\n");
58  fprintf(fp, "#\n");
59  fprintf(fp, "##############################################################"
60  "##############\n\n");
61 
62  fprintf(fp, "\"\"\"Wraps %s to make it even better\"\"\"\n\n",
63  G_program_name());
64 
65  fprintf(fp, "# %%module\n");
66  if (st->module_info.label)
67  fprintf(fp, "# %% label: %s\n", st->module_info.label);
68  if (st->module_info.description)
69  fprintf(fp, "# %% description: %s\n", st->module_info.description);
70  if (st->module_info.keywords) {
71  int i;
72 
73  for (i = 0; i < st->n_keys; i++) {
74  fprintf(fp, "# %% keyword: %s\n", st->module_info.keywords[i]);
75  }
76  }
77  fprintf(fp, "# %%end\n");
78 
79  if (st->n_flags) {
80  struct Flag *flag;
81 
82  for (flag = &st->first_flag; flag; flag = flag->next_flag) {
83  fprintf(fp, "# %%flag\n");
84  fprintf(fp, "# %% key: %c\n", flag->key);
85  if (flag->suppress_required)
86  fprintf(fp, "# %% suppress_required: yes\n");
87  if (flag->label)
88  fprintf(fp, "# %% label: %s\n", flag->label);
89  if (flag->description)
90  fprintf(fp, "# %% description: %s\n", flag->description);
91  if (flag->guisection)
92  fprintf(fp, "# %% guisection: %s\n", flag->guisection);
93  fprintf(fp, "# %%end\n");
94  }
95  }
96 
97  if (st->n_opts) {
98  struct Option *opt;
99 
100  for (opt = &st->first_option; opt; opt = opt->next_opt) {
101  switch (opt->type) {
102  case TYPE_INTEGER:
103  type = "integer";
104  break;
105  case TYPE_DOUBLE:
106  type = "double";
107  break;
108  case TYPE_STRING:
109  type = "string";
110  break;
111  default:
112  type = "string";
113  break;
114  }
115 
116  fprintf(fp, "# %%option\n");
117  fprintf(fp, "# %% key: %s\n", opt->key);
118  fprintf(fp, "# %% type: %s\n", type);
119  fprintf(fp, "# %% required: %s\n", opt->required ? "yes" : "no");
120  fprintf(fp, "# %% multiple: %s\n", opt->multiple ? "yes" : "no");
121  if (opt->options)
122  fprintf(fp, "# %% options: %s\n", opt->options);
123  if (opt->key_desc)
124  fprintf(fp, "# %% key_desc: %s\n", opt->key_desc);
125  if (opt->label)
126  fprintf(fp, "# %% label: %s\n", opt->label);
127  if (opt->description)
128  fprintf(fp, "# %% description: %s\n", opt->description);
129  if (opt->descriptions)
130  fprintf(fp, "# %% descriptions: %s\n", opt->descriptions);
131  if (opt->answer)
132  fprintf(fp, "# %% answer: %s\n", opt->answer);
133  if (opt->gisprompt)
134  fprintf(fp, "# %% gisprompt: %s\n", opt->gisprompt);
135  if (opt->guisection)
136  fprintf(fp, "# %% guisection: %s\n", opt->guisection);
137  if (opt->guidependency)
138  fprintf(fp, "# %% guidependency: %s\n", opt->guidependency);
139  fprintf(fp, "# %%end\n");
140  }
141  }
142 
143  fprintf(fp, "\nimport grass.script as gs\n\n");
144  fprintf(fp, "\ndef main():");
145  fprintf(
146  fp,
147  "\n \"\"\"Process command line parameters and run analysis\"\"\"");
148  fprintf(fp, "\n options, flags = gs.parser()");
149  fprintf(fp, "\n # Put your code here.");
150  fprintf(fp, "\n\n");
151  fprintf(fp, "\nif __name__ == \"__main__\":");
152  fprintf(fp, "\n main()\n");
153 }
const char * G_whoami(void)
Gets user's name.
Definition: gis/whoami.c:35
const char * G_program_name(void)
Return module name.
Definition: progrm_nme.c:28
#define TYPE_STRING
Definition: gis.h:186
#define TYPE_INTEGER
Definition: gis.h:184
#define TYPE_DOUBLE
Definition: gis.h:185
struct state * st
Definition: parser.c:104
void G__script(void)
Generate Python script-like output.
Definition: parser_script.c:24
Structure that stores flag info.
Definition: gis.h:588
const char * guisection
Definition: gis.h:595
struct Flag * next_flag
Definition: gis.h:597
char suppress_required
Definition: gis.h:591
const char * description
Definition: gis.h:594
char key
Definition: gis.h:589
const char * label
Definition: gis.h:593
Structure that stores option information.
Definition: gis.h:557
const char * key
Definition: gis.h:558
struct Option * next_opt
Definition: gis.h:574
const char * key_desc
Definition: gis.h:564
const char * gisprompt
Definition: gis.h:575
const char * label
Definition: gis.h:565
int type
Definition: gis.h:559
const char * descriptions
Definition: gis.h:567
const char * description
Definition: gis.h:566
char * answer
Definition: gis.h:571
int required
Definition: gis.h:560
const char * options
Definition: gis.h:562
int multiple
Definition: gis.h:561
const char * guisection
Definition: gis.h:576
const char * guidependency
Definition: gis.h:578