0

I have created a very simple script and would like to pass arguments to the script.

like:

grails> helloworld -n Howdy
grails> helloworld -name Howdy

with the script:

target(main: 'Hello World') {
    def cli = new CliBuilder()
    cli.with
            {
                h(longOpt: 'help', 'Help - Usage Information')
                n(longOpt: 'name', 'Name to say hello to', args: 1, required: true)
            }
    def opt = cli.parse(args)
    if (!opt) return
    if (opt.h) cli.usage()
    println "Hello ${opt.n}"
}

I seem to fail in every attempt that i do. The script keeps complain about the -n option being not present.

When i debug the script the value op the args parameters looks like the values are rearranged.

When calling the script with :

grails> helloworld -n Howdy 

the value of args inside the script is: Howdy -n

What am i missing here of doing wrong? Any suggestions?

2 Answers 2

1

Your problem is that you're running your code through grails shell. I've converted your code to CLI.groovy like this:

class CLI{
 public static void main(String [] args){
     def cli = new CliBuilder()
     cli.with
            {
                h(longOpt: 'help', 'Help - Usage Information')
                n(longOpt: 'name', 'Name to say hello to', args: 1, required: true)
             }
      def opt = cli.parse(args)
      if (!opt) return
      if (opt.h) cli.usage()
      println "Hello ${opt.n}"
      }
 }

After that I'm using groovy command to run it from linux shell like that:

 archer@capitan $ groovy CLI -n Daddy

It outputs:

 archer@capitan $ groovy CLI -n Daddy
 Hello Daddy

So it works like a charm.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your explanation! But i need this inside the grails shell, i am trying to use the parameters for some grails script i want to adjust.
I'm not sure you can pass parameters from within grails shell
0

I did a Google search for site:github.com grailsScript CliBuilder and came across:

https://github.com/Grails-Plugin-Consortium/grails-cxf/blob/master/scripts/WsdlToJava.groovy

That gave me the hint that the args variable needs to be formatted. Unfortunately it mutates -n Howdy into Howdy\n-n (not sure why the order is rearranged or the newline character is added).

The github page above has a doSplit() method to handle some of this, but it keeps the rearranged order. The best thing I've found is to remove the space between -n and Howdy, which will work with CliBuilder.

The following is what I have working:

target(main: 'Hello World') {
    def cli = new CliBuilder()
    cli.with
            {
                h(longOpt: 'help', 'Help - Usage Information')
                n(longOpt: 'name', 'Name to say hello to', args: 1, required: true)
            }
    def ops = doSplit(args)
    def opt = cli.parse(ops)
    if (!opt) return
    if (opt.h) cli.usage()
    println "Hello ${opt.n}"
}

private doSplit(String string){
    string.split(/(\n|[ ]|=)/).collect{ it.trim() }.findResults  { it && it != '' ? it : null }
}

Run this with: helloworld -nHowdy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.