[GRASS-dev] new v.support metadata module

Glynn Clements glynn at gclements.plus.com
Thu Mar 15 21:45:23 EDT 2007


Markus Neteler wrote:

> I have added another parameter "cmdhist" to add strings (hist formatted) to the
> command history in metadata (for 'v.info -h map').
> This is useful for all vector *scripts* which so far do not extend
> the vector history.
> 
> Example:
> 
> v.random rand n=20
> v.db.addcol rand col="mycol integer"
> v.info -h rand
> COMMAND: v.random output="rand" n=20 zmin=0.0 zmax=0.0
> GISDBASE: /home/neteler/grassdata
> LOCATION: spearfish60 MAPSET: neteler USER: neteler DATE: Thu Mar 15 16:36:22 2007
> # ... so far 'v.db.addcol' doesn't appear in the history file
> 
> 
> # now:
> v.support rand cmdhist="v.db.addcol rand col='mycol integer'"
> v.info -h rand
> COMMAND: v.random output="rand" n=20 zmin=0.0 zmax=0.0
> GISDBASE: /home/neteler/grassdata
> LOCATION: spearfish60 MAPSET: neteler USER: neteler DATE: Thu Mar 15 16:36:22 2007
> ---------------------------------------------------------------------------------
> COMMAND: v.db.addcol rand col='mycol integer'
> GISDBASE: /home/neteler/grassdata
> LOCATION: spearfish60 MAPSET: neteler USER: neteler DATE: Thu Mar 15 16:44:01 2007

> Question:
> What's the best trick to construct the cmd line automatically in
> g.parser supported scripts? Stuff like
>  CMDLINE="$0 $@"
> fails since g.parser modifies the shell parameters. The solution should then
> go into all scripts/v*/v* where appropriate.

A simple aproach such as CMDLINE="$0 $@" will fail if any of the
arguments contain spaces.

The following should work provided that none of the arguments contain
single quotes (quotes which get eaten by the shell don't count):

	CMDLINE="'$0'"
	for arg in "$@" ; do
	    CMDLINE="$CMDLINE '$arg'"
	done

But you need to do this on the first pass (before g.parser is called)
and export it, e.g.:

	if [ "$1" != "@ARGS_PARSED@" ] ; then
	    CMDLINE="'$0'"
	    for arg in "$@" ; do
	        CMDLINE="$CMDLINE '$arg'"
	    done
	    export CMDLINE
	    exec g.parser "$0" "$@"
	fi

If you need to handle the case where an argument contains single
quotes, it gets ugly. It should work to replace:

	        CMDLINE="$CMDLINE '$arg'"
with:
	        CMDLINE="$CMDLINE '`echo "$arg" | sed "s/'/'\\\\\\\\''/g"`'"

This assumes that you want the COMMAND: entry to use shell syntax.

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list