[GRASS-dev] making grass modules less verbose

Sören Gebbert soerengebbert at gmx.de
Wed Sep 20 03:37:36 EDT 2006


Hi,
i think this is a nice idea.
Some modules are very verbose and with this approach a general
way is provided to handle this verbosity.

Best regards
Soeren


Jachym Cepicky schrieb:
> Hallo,
> 
> following patch makes GRASS modules able to use --verbose flag or set
> GRASS_VERBOSE environment variable.
> 
> In the code:
> 
>         if (module->verbose)
>                     printf("Blah, blah, blah, blah\n");
> 
> 
> or
> 
>         if (module->verbose)
>                     G_percent(row, nrows, 2);
> 
> I thing, that according to Eric Steven Raymond, if the program does
> not have anything interesting to say, it should say nothing. This is
> good for embeding GRASS in some web server application or for some scripts.
> 
> But how to use it in old GRASS modules, so that the number of modules,
> which must be corrected by hand, is as low as possible? Do you thing,
> this is useful? Shall I put apply this to cvs?
> 
> Example session:
> 
> GRASS 6.3.cvs (spearfish60):~ > export GRASS_VERBOSE=0
> GRASS 6.3.cvs (spearfish60):~ > r.example --help
> 
> Description:
>  My first raster module
> 
>  Keywords:
>   keyword1, keyword2, keyword3
> 
>  Usage:
>    r.example [-q] input=name output=name [--overwrite] [--verbose]
> 
>  Flags:
>     -q   Quiet
>     --o   Force overwrite of output files
>     --v   Verbose module output
> 
> Parameters:
>     input   Name of input raster map
>     output   Name for output raster map
>     
> GRASS 6.3.cvs (spearfish60):~ > r.example in=aspect out=aspect
> GRASS 6.3.cvs (spearfish60):~ >
> GRASS 6.3.cvs (spearfish60):~ > r.example in=aspect out=aspect --v
> Blah, blah, blah, blah
> 93%
> GRASS 6.3.cvs (spearfish60):~ > 
> GRASS 6.3.cvs (spearfish60):~ > export GRASS_VERBOSE=1
> GRASS 6.3.cvs (spearfish60):~ > r.example in=aspect out=aspect 
> Blah, blah, blah, blah
> 93%
> GRASS 6.3.cvs (spearfish60):~ > 
> 
> Looking forward to your comments
> 
> Jachym
> 
> 
> ------------------------------------------------------------------------
> 
> Index: include/gis.h
> ===================================================================
> RCS file: /home/grass/grassrepository/grass6/include/gis.h,v
> retrieving revision 1.33
> diff -u -r1.33 gis.h
> --- include/gis.h	19 Aug 2006 12:52:21 -0000	1.33
> +++ include/gis.h	19 Sep 2006 14:43:42 -0000
> @@ -570,6 +570,7 @@
>      char *keywords;                 /* Keywords describing module */
>  	/* further items are possible: author(s), version */
>      int overwrite; /* overwrite old files */
> +    int verbose;   /* print all informations about progres and so on */
>  };
>  
>  struct TimeStamp
> Index: lib/gis/parser.c
> ===================================================================
> RCS file: /home/grass/grassrepository/grass6/lib/gis/parser.c,v
> retrieving revision 1.85
> diff -u -r1.85 parser.c
> --- lib/gis/parser.c	17 Sep 2006 21:50:00 -0000	1.85
> +++ lib/gis/parser.c	19 Sep 2006 14:43:43 -0000
> @@ -136,6 +136,7 @@
>  static int n_opts = 0 ;
>  static int n_flags = 0 ;
>  static int overwrite = 0 ;
> +static int verbose = 0 ;
>  
>  static struct Flag first_flag;    /* First flag in a linked list      */
>  static struct Flag *current_flag; /* Pointer for traversing list      */
> @@ -171,6 +172,7 @@
>  static int split_opts();
>  static int check_multiple_opts();
>  static int check_overwrite();
> +static int check_verbose();
>  static int interactive( char *);
>  static int interactive_flag( struct Flag *);
>  static int interactive_option( struct Option *);
> @@ -728,6 +730,12 @@
>  			    overwrite = 1;
>  			}
>  
> +			/* Verbose option */
> +                        else if ( strcmp(ptr,"--v") == 0 || strcmp(ptr,"--verbose") == 0 )
> +			{
> +			    module_info.verbose = 1;
> +			}
> +
>  			/* Force gui to come up */
>  			else if ( strcmp(ptr,"--ui") == 0 )
>  			{
> @@ -794,6 +802,9 @@
>  	if ( check_overwrite () )
>  	    return -1;
>  
> +	if (check_verbose ())
> +            module_info.verbose = 1;
> +
>  	return(0) ;
>  }
>  
> @@ -932,6 +943,10 @@
>  		len=show(item,len);
>  	}
>  
> +        strcpy (item, " [--verbose]");
> +        len=show(item,len);
> +
> +
>  	fprintf (stderr, "\n");
>  
>  	/* Print help info for flags */
> @@ -960,8 +975,9 @@
>  	}
>  
>  	if (new_prompt)
> -		fprintf(stderr," --o   %s\n", _("Force overwrite of output files")) ;
> -		
> +	        fprintf(stderr," --o   %s\n", _("Force overwrite of output files")) ;
> +
> +        fprintf(stderr," --v   %s\n", _("Verbose module output")) ;
>  
>  	/* Print help info for options */
>  
> @@ -1334,9 +1350,10 @@
>  		}
>  	}
>  	if (new_prompt)
> -	{
>  		fprintf(stdout, " [--<b>overwrite</b>] ");
> -	}
> +	
> +        fprintf(stdout, " [--<b>verbose</b>] ");
> +
>  	fprintf(stdout, "\n");
>  
>  
> @@ -1371,6 +1388,10 @@
>  			fprintf(stdout, "<DT><b>--overwrite</b></DT>\n");
>  			fprintf(stdout, "<DD>Force overwrite of output files</DD>");
>  		}
> +
> +                fprintf(stdout, "<DT><b>--verbose</b></DT>\n");
> +                fprintf(stdout, "<DD>Verbose module output</DD>");
> +
>  		fprintf(stdout, "</DL>\n");
>  	}
>  
> @@ -2144,6 +2165,34 @@
>  	}
>  
>  	return(error) ;
> +}
> +
> +/* Check verbose */
> +static int check_verbose (void)
> +{
> +	char *verstr;
> +	int verb;
> +
> +	verb = 0;
> +	/* Check the GRASS VERBOSE variable */
> +	if ( (verstr = G__getenv ( "VERBOSE" )) ) {
> +	    verb = atoi ( verstr );
> +	}
> +
> +	/* Check the GRASS_OVERWRITE environment variable */
> +	if ( (verstr = getenv ( "GRASS_VERBOSE" )) ) {
> +	    if (atoi ( verstr ))
> +		verb = 1;
> +	}
> +
> +	if ( verb ) {
> +	    module_info.verbose = 1;
> +	    /* Set the environment so that programs run in a script also obey --o */
> +	    putenv("GRASS_OVERWRITE=1");
> +	    /* No need to check options for existing files if overwrite is true */
> +	}
> +
> +	return(verb) ;
>  }
>  
>  static int interactive( char *command)
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> grass-dev mailing list
> grass-dev at grass.itc.it
> http://grass.itc.it/mailman/listinfo/grass-dev




More information about the grass-dev mailing list