[GRASS-SVN] r41386 - grass/branches/releasebranch_6_4/general/g.parser

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Mar 11 17:34:16 EST 2010


Author: neteler
Date: 2010-03-11 17:34:16 -0500 (Thu, 11 Mar 2010)
New Revision: 41386

Modified:
   grass/branches/releasebranch_6_4/general/g.parser/description.html
   grass/branches/releasebranch_6_4/general/g.parser/main.c
Log:
backport of -s flag

Modified: grass/branches/releasebranch_6_4/general/g.parser/description.html
===================================================================
--- grass/branches/releasebranch_6_4/general/g.parser/description.html	2010-03-11 22:11:59 UTC (rev 41385)
+++ grass/branches/releasebranch_6_4/general/g.parser/description.html	2010-03-11 22:34:16 UTC (rev 41386)
@@ -9,9 +9,22 @@
 
 <img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
 
+
 <h2>NAME</h2>
 <em><b>g.parser</b></em>
 
+<h2>SYNOPSIS</h2>
+<b>g.parser help</b><br>
+<b>g.parser</b> [-<b>s</b>] [-<b>t</b>] <em>filename</em> [<em>argument</em>,...]
+
+<h3>Flags:</h3>
+<DL>
+<DT><b>-t</b></DT>
+<DD>Print strings for translation</DD>
+<DT><b>-s</b></DT>
+<DD>Write option values to stdout instead of reinvoking script</DD>
+</DL>
+
 <h2>DESCRIPTION</h2>
 
 The <em>g.parser</em> module provides full parser support for GRASS
@@ -19,20 +32,32 @@
 template, and command line option checking. In this way a simple
 script can very quickly be made into a full-fledged GRASS module.
 
+
 <h2>OPTIONS</h2>
 
-After parsing the arguments are stored in environment variables for
-use in your scripts. These variables are named "GIS_FLAG_&lt;NAME&gt;"
-for flags and "GIS_OPT_&lt;NAME&gt;" for options. The names of
-variables are converted to upper case. For example if an option with
-key <b>input</b> was defined in the script header, the value will be
-available in variable <b>GIS_OPT_INPUT</b> and the value of flag with
-key <b>f</b> will be available in variable <b>GIS_FLAG_F</b>.
+Unless the <b>-s</b> switch is used, the arguments are stored in
+environment variables for use in your scripts. These variables are
+named "GIS_FLAG_&lt;NAME&gt;" for flags and "GIS_OPT_&lt;NAME&gt;" for
+options. The names of variables are converted to upper case. For
+example if an option with key <b>input</b> was defined in the script
+header, the value will be available in variable <b>GIS_OPT_INPUT</b>
+and the value of flag with key <b>f</b> will be available in variable
+<b>GIS_FLAG_F</b>.
 
 <p>
 For flags, the value will be "1" if the flag was given, and "0" otherwise.
 
 <p>
+If the <b>-s</b> switch is used, the options and flags are written to
+stdout in the form <em>opt_&lt;name&gt;=&lt;value&gt;</em> and
+<em>flag_&lt;name&gt;=&lt;value&gt;</em>, preceded by the string
+<b>@ARGS_PARSED@</b>. If this string doesn't appear as the first line
+of stdout, it indicates that the script was invoked with a switch such
+as <b>--html-description</b>. In this case, the data written by
+<em>g.parser</em> to stdout should be copied to the script's stdout
+verbatim.
+
+<p>
 Typical header definitions are as follows:
 
 <div class="code"><pre>
@@ -96,6 +121,7 @@
 script should behave the same way regardless of whether they were set
 by --o, --q or --v being passed to the script or set by other means.
 
+
 <h2>AUTOMATED SCRIPT CREATION</h2>
 
 The flag <b>--script</b> added to a GRASS command, generates shell
@@ -259,19 +285,23 @@
 import grass.script as grass
 
 def main():
+    flag_f = flags['f']
+    option1 = options['option1']
+    raster = options['raster']
+    vector = options['vector']
     #### add your code here ####
 
-    if flags['f']:
+    if flag_f:
         print "Flag -f set"
     else:
         print "Flag -f not set"
 
     # test if parameter present:
-    if options['option1']:
-        print "Value of GIS_OPT_OPTION1: '%s'" % options['option1']
+    if option1:
+        print "Value of option1= option: '%s'" % option1
 
-    print "Value of GIS_OPT_RASTER: '%s'" % options['raster']
-    print "Value of GIS_OPT_VECTOR: '%s'" % options['vector']
+    print "Value of raster= option: '%s'" % raster
+    print "Value of vector= option: '%s'" % vector
 
     #### end of your code ####
 
@@ -279,7 +309,7 @@
 
 if __name__ == "__main__":
     options, flags = grass.parser()
-    sys.exit(main())
+    main()
 </pre></div>
 
 The <tt>test.py</tt> script will provide following help text:

Modified: grass/branches/releasebranch_6_4/general/g.parser/main.c
===================================================================
--- grass/branches/releasebranch_6_4/general/g.parser/main.c	2010-03-11 22:11:59 UTC (rev 41385)
+++ grass/branches/releasebranch_6_4/general/g.parser/main.c	2010-03-11 22:34:16 UTC (rev 41386)
@@ -255,12 +255,102 @@
 	    cmd, ctx->line);
 }
 
+static int print_options(const struct context *ctx)
+{
+    struct Option *option;
+    struct Flag *flag;
+    const char *overwrite = getenv("GRASS_OVERWRITE");
+    const char *verbose = getenv("GRASS_VERBOSE");
+
+    printf("@ARGS_PARSED@\n");
+
+    if (overwrite)
+	printf("GRASS_OVERWRITE=%s\n", overwrite);
+
+    if (verbose)
+	printf("GRASS_VERBOSE=%s\n", verbose);
+
+    for (flag = ctx->first_flag; flag; flag = flag->next_flag)
+	printf("flag_%c=%d\n", flag->key, flag->answer ? 1 : 0);
+
+    for (option = ctx->first_option; option; option = option->next_opt)
+	printf("opt_%s=%s\n", option->key,
+	       option->answer ? option->answer : "");
+
+    return 0;
+}
+
+static int reinvoke_script(const struct context *ctx, const char *filename)
+{
+    struct Option *option;
+    struct Flag *flag;
+
+    /* Because shell from MINGW and CygWin converts all variables
+     * to uppercase it was necessary to use uppercase variables.
+     * Set both until all scripts are updated */
+    for (flag = ctx->first_flag; flag; flag = flag->next_flag) {
+	char buff[16];
+
+	sprintf(buff, "GIS_FLAG_%c=%d", flag->key, flag->answer ? 1 : 0);
+	putenv(G_store(buff));
+
+	sprintf(buff, "GIS_FLAG_%c=%d", toupper(flag->key),
+		flag->answer ? 1 : 0);
+
+	G_debug(2, "set %s", buff);
+	putenv(G_store(buff));
+    }
+
+    for (option = ctx->first_option; option; option = option->next_opt) {
+	char upper[4096];
+	char *str;
+
+	G_asprintf(&str, "GIS_OPT_%s=%s", option->key,
+		   option->answer ? option->answer : "");
+	putenv(str);
+
+	strcpy(upper, option->key);
+	G_str_to_upper(upper);
+	G_asprintf(&str, "GIS_OPT_%s=%s", upper,
+		   option->answer ? option->answer : "");
+
+	G_debug(2, "set %s", str);
+	putenv(str);
+    }
+
+#ifdef __MINGW32__
+    {
+	/* execlp() and _spawnlp ( _P_OVERLAY,..) do not work, they return 
+	 * immediately and that breaks scripts running GRASS scripts
+	 * because they dont wait until GRASS script finished */
+	/* execlp( "sh", "sh", filename, "@ARGS_PARSED@", NULL); */
+	/* _spawnlp ( _P_OVERLAY, filename, filename, "@ARGS_PARSED@", NULL ); */
+	int ret;
+	char *shell = getenv("GRASS_SH");
+
+	if (shell == NULL)
+	    shell = "sh";
+	ret = G_spawn(shell, shell, filename, "@ARGS_PARSED@", NULL);
+	G_debug(1, "ret = %d", ret);
+	if (ret == -1) {
+	    perror("G_spawn() failed");
+	    return 1;
+	}
+	return ret;
+    }
+#else
+    execl(filename, filename, "@ARGS_PARSED@", NULL);
+
+    perror("execl() failed");
+    return 1;
+#endif
+}
+
 int main(int argc, char *argv[])
 {
     struct context ctx;
-    struct Option *option;
-    struct Flag *flag;
     const char *filename;
+    int standard_output = 0;
 
     ctx.module = NULL;
     ctx.option = NULL;
@@ -274,18 +364,25 @@
     if (argc >= 2 && (strcmp(argv[1], "-t") == 0)) {
 	/* Turn on translation output */
 	translate_output = 1;
+	argv++, argc--;
     }
 
-    if ((argc < 2 + translate_output) || (argc >= 2 &&
-					  ((strcmp(argv[1], "help") == 0) ||
-					   (strcmp(argv[1], "-help") == 0) ||
-					   (strcmp(argv[1], "--help") == 0)))) {
-	fprintf(stderr, "Usage: %s [-t] <filename> [<argument> ...]\n",
+    if (argc >= 2 && (strcmp(argv[1], "-s") == 0)) {
+	/* write to stdout rather than re-invoking */
+	standard_output = 1;
+	argv++, argc--;
+    }
+
+    if ((argc < 2) || ((strcmp(argv[1], "help") == 0) ||
+		       (strcmp(argv[1], "-help") == 0) ||
+		       (strcmp(argv[1], "--help") == 0))) {
+	fprintf(stderr, "Usage: %s [-t] [-s] <filename> [<argument> ...]\n",
 		argv[0]);
 	return 1;
     }
 
-    filename = argv[1 + translate_output];
+    filename = argv[1];
+    argv++, argc--;
     G_debug(2, "filename = %s", filename);
 
     ctx.fp = fopen(filename, "r");
@@ -351,65 +448,10 @@
     if (translate_output)
 	return EXIT_SUCCESS;
 
-    if (G_parser(argc - 1, argv + 1))
+    if (G_parser(argc, argv))
 	return 1;
 
-    /* Because shell from MINGW and CygWin converts all variables
-     * to uppercase it was necessary to use uppercase variables.
-     * Set both until all scripts are updated */
-    for (flag = ctx.first_flag; flag; flag = flag->next_flag) {
-	char buff[16];
-
-	sprintf(buff, "GIS_FLAG_%c=%d", flag->key, flag->answer ? 1 : 0);
-	putenv(G_store(buff));
-
-	sprintf(buff, "GIS_FLAG_%c=%d", toupper(flag->key),
-		flag->answer ? 1 : 0);
-
-	G_debug(2, "set %s", buff);
-	putenv(G_store(buff));
-    }
-
-    for (option = ctx.first_option; option; option = option->next_opt) {
-	char buff[4096], upper[4096];
-
-	sprintf(buff, "GIS_OPT_%s=%s", option->key,
-		option->answer ? option->answer : "");
-	putenv(G_store(buff));
-
-	G_strcpy(upper, option->key);
-	G_str_to_upper(upper);
-	sprintf(buff, "GIS_OPT_%s=%s", upper,
-		option->answer ? option->answer : "");
-
-	G_debug(2, "set %s", buff);
-	putenv(G_store(buff));
-    }
-
-#ifdef __MINGW32__
-    {
-	/* execlp() and _spawnlp ( _P_OVERLAY,..) do not work, they return 
-	 * immediately and that breaks scripts running GRASS scripts
-	 * because they dont wait until GRASS script finished */
-	/* execlp( "sh", "sh", filename, "@ARGS_PARSED@", NULL); */
-	/* _spawnlp ( _P_OVERLAY, "sh", "sh", filename, "@ARGS_PARSED@", NULL ); */
-	int ret;
-	char *shell = getenv("GRASS_SH");
-
-	if (shell == NULL)
-	    shell = "sh";
-	ret = G_spawn(shell, shell, filename, "@ARGS_PARSED@", NULL);
-	G_debug(1, "ret = %d", ret);
-	if (ret == -1) {
-	    perror("G_spawn() failed");
-	    return 1;
-	}
-	return ret;
-    }
-#else
-    execl(filename, filename, "@ARGS_PARSED@", NULL);
-
-    perror("execl() failed");
-    return 1;
-#endif
+    return standard_output
+	? print_options(&ctx)
+	: reinvoke_script(&ctx, filename);
 }



More information about the grass-commit mailing list