vandekerkoff
vandekerkoff

Reputation: 445

Using parameters in a groovy script

Just starting with groovy, be grateful if someone could help me out.

I'm writing a function in a script that will accept a parameter.

def print_arg(def arg){
    println "$arg"
}

I save that file as test.groovy.

How do I test throwing arguments at the function from the command line?

groovy test.groovy print_arg input
groovy test.groovy print_arg 'input'
groovy test.groovy print_arg(input)

None of the above work

I suspect I'm doing something fundamentally daft :-)

Any tips, greatly appreciated.

Upvotes: 1

Views: 5064

Answers (1)

Jayan
Jayan

Reputation: 18458

Following is the way to call a method from script

def print_arg(def arg){
    println "$arg"
}

print_arg ("hello") 

Scripts receive args array so following will pass argument to your function

def print_arg(def arg){
        println "$arg"
    }

print_arg (args[0]) 

(There are more elegant mechanisms that you can pick later How to capture arguments passed to a Groovy script? - my answer is from above link, could not duplicate as this question is more basic)

Upvotes: 1

Related Questions