GRASS GIS 7 Programmer's Manual  7.7.svn(2018)-r73373
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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,
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  int i;
66 
67  for(i = 0; i < st->n_keys; i++) {
68  fprintf(fp, "#%% keyword: %s\n", st->module_info.keywords[i]);
69  }
70  }
71  fprintf(fp, "#%%end\n");
72 
73  if (st->n_flags) {
74  struct Flag *flag;
75 
76  for (flag = &st->first_flag; flag; flag = flag->next_flag) {
77  fprintf(fp, "#%%flag\n");
78  fprintf(fp, "#%% key: %c\n", flag->key);
79  if (flag->suppress_required)
80  fprintf(fp, "#%% suppress_required: yes\n");
81  if (flag->label)
82  fprintf(fp, "#%% label: %s\n", flag->label);
83  if (flag->description)
84  fprintf(fp, "#%% description: %s\n", flag->description);
85  if (flag->guisection)
86  fprintf(fp, "#%% guisection: %s\n", flag->guisection);
87  fprintf(fp, "#%%end\n");
88  }
89  }
90 
91  if (st->n_opts) {
92  struct Option *opt;
93 
94  for (opt = &st->first_option; opt; opt = opt->next_opt) {
95  switch (opt->type) {
96  case TYPE_INTEGER:
97  type = "integer";
98  break;
99  case TYPE_DOUBLE:
100  type = "double";
101  break;
102  case TYPE_STRING:
103  type = "string";
104  break;
105  default:
106  type = "string";
107  break;
108  }
109 
110  fprintf(fp, "#%%option\n");
111  fprintf(fp, "#%% key: %s\n", opt->key);
112  fprintf(fp, "#%% type: %s\n", type);
113  fprintf(fp, "#%% required: %s\n", opt->required ? "yes" : "no");
114  fprintf(fp, "#%% multiple: %s\n", opt->multiple ? "yes" : "no");
115  if (opt->options)
116  fprintf(fp, "#%% options: %s\n", opt->options);
117  if (opt->key_desc)
118  fprintf(fp, "#%% key_desc: %s\n", opt->key_desc);
119  if (opt->label)
120  fprintf(fp, "#%% label: %s\n", opt->label);
121  if (opt->description)
122  fprintf(fp, "#%% description: %s\n", opt->description);
123  if (opt->descriptions)
124  fprintf(fp, "#%% descriptions: %s\n", opt->descriptions);
125  if (opt->answer)
126  fprintf(fp, "#%% answer: %s\n", opt->answer);
127  if (opt->gisprompt)
128  fprintf(fp, "#%% gisprompt: %s\n", opt->gisprompt);
129  if (opt->guisection)
130  fprintf(fp, "#%% guisection: %s\n", opt->guisection);
131  if (opt->guidependency)
132  fprintf(fp, "#%% guidependency: %s\n", opt->guidependency);
133  fprintf(fp, "#%%end\n");
134  }
135  }
136 
137  fprintf(fp, "\nimport sys\n");
138  fprintf(fp, "\nimport grass.script as grass\n");
139  fprintf(fp, "\ndef main():");
140  fprintf(fp, "\n # put code here\n");
141  fprintf(fp, "\n return 0\n");
142  fprintf(fp, "\nif __name__ == \"__main__\":");
143  fprintf(fp, "\n options, flags = grass.parser()");
144  fprintf(fp, "\n sys.exit(main())\n");
145 }
const char * guidependency
Definition: gis.h:528
#define TYPE_DOUBLE
Definition: gis.h:142
const char * descriptions
Definition: gis.h:520
#define TYPE_STRING
Definition: gis.h:143
const char * G_whoami(void)
Gets user&#39;s name.
Definition: gis/whoami.c:35
const char * guisection
Definition: gis.h:546
const char * description
Definition: gis.h:519
#define TYPE_INTEGER
Definition: gis.h:141
int type
Definition: gis.h:512
struct state * st
Definition: parser.c:103
char * answer
Definition: gis.h:522
const char * guisection
Definition: gis.h:527
int multiple
Definition: gis.h:514
Structure that stores flag info.
Definition: gis.h:538
void G__script(void)
Generate Python script-like output.
Definition: parser_script.c:24
char key
Definition: gis.h:540
int required
Definition: gis.h:513
const char * G_program_name(void)
Return module name.
Definition: progrm_nme.c:28
struct Flag * next_flag
Definition: gis.h:547
const char * label
Definition: gis.h:544
const char * label
Definition: gis.h:518
char suppress_required
Definition: gis.h:542
Structure that stores option information.
Definition: gis.h:509
const char * description
Definition: gis.h:545
const char * key
Definition: gis.h:511
const char * gisprompt
Definition: gis.h:526
const char * options
Definition: gis.h:515
const char * key_desc
Definition: gis.h:517
struct Option * next_opt
Definition: gis.h:525