[groovy-user] CliBuilder problem

54 views
Skip to first unread message

trx

unread,
Apr 26, 2011, 2:09:25 AM4/26/11
to us...@groovy.codehaus.org
Hello,
I have the following CliBuilder test code:

def cli = new CliBuilder(
usage: 'testFile -n yourName',
header: '\nAvailable options (use -h for help):\n',
footer: '\nInformation provided via above options is used to generate
printed string.\n')

cli.with{
h(longOpt: 'help', 'Help - Usage Information')
n(longOpt: 'name', 'Name to say hello to', type: String, required: true)
}

def opt = cli.parse(args)
if (opt.h) cli.usage()
if (opt.n) {
println "Hello ${opt.n}"
}

Why I have got this error massage without to specify any parameters:

Exception in thread "main" java.lang.NullPointerException: Cannot get
property 'h' on null object
at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:56)
at
org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:156)
at
org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:44)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:235)
at testFile.run(testFile.groovy:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1058)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at
org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:793)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:776)
at
org.codehaus.groovy.runtime.InvokerHelper.runScript(InvokerHelper.java:394)
at org.codehaus.groovy.runtime.InvokerHelper$runScript.call(Unknown Source)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at testFile.main(testFile.groovy)

How is possible to get the help without the above error?

Thank you in advance.

--
View this message in context: http://groovy.329449.n5.nabble.com/CliBuilder-problem-tp4340202p4340202.html
Sent from the groovy - user mailing list archive at Nabble.com.

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

http://xircles.codehaus.org/manage_email


Dinko Srkoč

unread,
Apr 26, 2011, 3:49:27 AM4/26/11
to us...@groovy.codehaus.org
You marked option 'n' as required. While parsing, if a parse error is
met, or required option is not given, method parse() will invoke usage()
and return null. Hence your NPE - opt is null because option 'n' is
probably absent.

Cheers,
Dinko

trx

unread,
Apr 26, 2011, 8:39:34 AM4/26/11
to us...@groovy.codehaus.org
Thank you. With "if (!opt) return" I do not get the error.

--
View this message in context: http://groovy.329449.n5.nabble.com/CliBuilder-problem-tp4340202p4340850.html

Reply all
Reply to author
Forward
0 new messages