GRASS GIS 7 Programmer's Manual  7.0.5(2016)-r00000
parser_script.c
Go to the documentation of this file.
1 
15 #include <stdio.h>
16 
17 #include <grass/gis.h>
18 
19 #include "parser_local_proto.h"
20 
24 void G__script(void)
25 {
26  FILE *fp = stdout;
27  char *type;
28 
29  fprintf(fp,
30  "#!/usr/bin/env python\n");
31  fprintf(fp,
32  "############################################################################\n");
33  fprintf(fp, "#\n");
34  fprintf(fp, "# MODULE: %s_wrapper\n", G_program_name());
35  fprintf(fp, "# AUTHOR(S): %s\n", G_whoami());
36  fprintf(fp, "# PURPOSE: Wrapper for %s\n", G_program_name());
37  fprintf(fp, "# COPYRIGHT: (C) %s by %s, and the GRASS Development Team\n",
38  GRASS_VERSION_DATE, G_whoami());
39  fprintf(fp, "#\n");
40  fprintf(fp,
41  "# This program is free software; you can redistribute it and/or modify\n");
42  fprintf(fp,
43  "# it under the terms of the GNU General Public License as published by\n");
44  fprintf(fp,
45  "# the Free Software Foundation; either version 2 of the License, or\n");
46  fprintf(fp, "# (at your option) any later version.\n");
47  fprintf(fp, "#\n");
48  fprintf(fp,
49  "# This program is distributed in the hope that it will be useful,\n");
50  fprintf(fp,
51  "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
52  fprintf(fp,
53  "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
54  fprintf(fp, "# GNU General Public License for more details.\n");
55  fprintf(fp, "#\n");
56  fprintf(fp,
57  "############################################################################\n\n");
58 
59  fprintf(fp, "#%%module\n");
60  if (st->module_info.label)
61  fprintf(fp, "#%% label: %s\n", st->module_info.label);
62  if (st->module_info.description)
63  fprintf(fp, "#%% description: %s\n", st->module_info.description);
64  if (st->module_info.keywords) {
65  fprintf(fp, "#%% keyword: ");
67  fprintf(fp, "\n");
68  }
69  fprintf(fp, "#%%end\n");
70 
71  if (st->n_flags) {
72  struct Flag *flag;
73 
74  for (flag = &st->first_flag; flag; flag = flag->next_flag) {
75  fprintf(fp, "#%%flag\n");
76  fprintf(fp, "#%% key: %c\n", flag->key);
77  if (flag->suppress_required)
78  fprintf(fp, "#%% suppress_required: yes\n");
79  if (flag->label)
80  fprintf(fp, "#%% label: %s\n", flag->label);
81  if (flag->description)
82  fprintf(fp, "#%% description: %s\n", flag->description);
83  if (flag->guisection)
84  fprintf(fp, "#%% guisection: %s\n", flag->guisection);
85  fprintf(fp, "#%%end\n");
86  }
87  }
88 
89  if (st->n_opts) {
90  struct Option *opt;
91 
92  for (opt = &st->first_option; opt; opt = opt->next_opt) {
93  switch (opt->type) {
94  case TYPE_INTEGER:
95  type = "integer";
96  break;
97  case TYPE_DOUBLE:
98  type = "double";
99  break;
100  case TYPE_STRING:
101  type = "string";
102  break;
103  default:
104  type = "string";
105  break;
106  }
107 
108  fprintf(fp, "#%%option\n");
109  fprintf(fp, "#%% key: %s\n", opt->key);
110  fprintf(fp, "#%% type: %s\n", type);
111  fprintf(fp, "#%% required: %s\n", opt->required ? "yes" : "no");
112  fprintf(fp, "#%% multiple: %s\n", opt->multiple ? "yes" : "no");
113  if (opt->options)
114  fprintf(fp, "#%% options: %s\n", opt->options);
115  if (opt->key_desc)
116  fprintf(fp, "#%% key_desc: %s\n", opt->key_desc);
117  if (opt->label)
118  fprintf(fp, "#%% label: %s\n", opt->label);
119  if (opt->description)
120  fprintf(fp, "#%% description: %s\n", opt->description);
121  if (opt->descriptions)
122  fprintf(fp, "#%% descriptions: %s\n", opt->descriptions);
123  if (opt->answer)
124  fprintf(fp, "#%% answer: %s\n", opt->answer);
125  if (opt->gisprompt)
126  fprintf(fp, "#%% gisprompt: %s\n", opt->gisprompt);
127  if (opt->guisection)
128  fprintf(fp, "#%% guisection: %s\n", opt->guisection);
129  if (opt->guidependency)
130  fprintf(fp, "#%% guidependency: %s\n", opt->guidependency);
131  fprintf(fp, "#%%end\n");
132  }
133  }
134 
135  fprintf(fp, "\nimport sys\n");
136  fprintf(fp, "\nimport grass.script as grass\n");
137  fprintf(fp, "\ndef main():");
138  fprintf(fp, "\n # put code here\n");
139  fprintf(fp, "\n return 0\n");
140  fprintf(fp, "\nif __name__ == \"__main__\":");
141  fprintf(fp, "\n options, flags = grass.parser()");
142  fprintf(fp, "\n sys.exit(main())\n");
143 }
#define NULL
Definition: ccmath.h:32
struct state * st
Definition: parser.c:101
void G__script(void)
Generate Python script-like output.
Definition: parser_script.c:24
const char * G_program_name(void)
Return module name.
Definition: progrm_nme.c:27
const char * G_whoami(void)
Gets user&#39;s name.
Definition: whoami.c:35
void G__print_keywords(FILE *fd, void(*format)(FILE *, const char *))
Print list of keywords (internal use only)
Definition: parser.c:815