GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71933
 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  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 }
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
#define NULL
Definition: ccmath.h:32
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
void G__print_keywords(FILE *fd, void(*format)(FILE *, const char *))
Print list of keywords (internal use only)
Definition: parser.c:890
const char * options
Definition: gis.h:515
const char * key_desc
Definition: gis.h:517
struct Option * next_opt
Definition: gis.h:525