Jim White | 16 Jul 23:20

Re: [groovy-user] referencing a binding variables that's not passed

Kallin Nagelberg wrote:

> I have a groovy script that may or may not be called from another script.
> 
> In the the callee I'm trying to do something like:
> 
> if (bindingvar){
> // do something
> }
> 
> It works great when bindingvar is in the binding (ie the other script 
> called it), but when I run the script alone it complains of nosuchproperty.
> How can I check to see if the property is passed without catching that 
> exception?

if (binding.variables.containsKey("bindingVar")) {
// do something
}

Or if you'd like to get a null value for an optional binding:

def optVar = binding.variables.get("bindingVar")
if (optVar) {
// do something
}

This works because your script gets compiled as a subclass of 
groovy.lang.Script and it has a getBinding() method (along with other 
useful ones like println, run, and evaluate).

Jim

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane