VallingSki
VallingSki

Reputation: 133

groovy use var when other var is not defined

I have two variables in my script.

var1 = foo
var2 = (defined outside of the script)

What I need is something that defines var2 as var1 when var2 is not defined.

So lets say var2 is not defined, because the user didn't define it, then it should be this

var2 = var1

How can I do this?

Best regards

Upvotes: 1

Views: 581

Answers (1)

daggett
daggett

Reputation: 28564

inside the groovy script you can do this:

if(!this.getBinding().hasVariable('var2'))this.getBinding().setVariable('var2',var1)
println "var1 = ${var1}"
println "var2 = ${var2}"

or simplified option:

if(!binding.hasVariable('var2')) binding.var2 = var1

Upvotes: 3

Related Questions