1

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.

vandekerkoff
  • 415
  • 8
  • 24

1 Answers1

1

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)

Jayan
  • 18,003
  • 15
  • 89
  • 143