kohsuke/args4j

usage="", handler = StringArrayOptionHandler.class then parser.printUsage() will crash

yuzhidi opened this issue · 0 comments

// groovy code
`import org.kohsuke.args4j.CmdLineException
import org.kohsuke.args4j.CmdLineParser
import org.kohsuke.args4j.Option
import org.kohsuke.args4j.spi.StringArrayOptionHandler

class Args {
@option(name="-k", usage="found by this keyword")
String keyWord

@Option(name="-host",usage="host config")
String host

@Option(name="-u",usage="udid")
String udid

@Option(name="-s",usage="parse source file")
String sourceFile

@Option(name="-ignore", handler = StringArrayOptionHandler.class)
String[] ignoreList

boolean parse(String[] args) {
    CmdLineParser parser = new CmdLineParser(this)
    if (args.size() == 0) {
        System.err.println("please input arguments:")
        usage parser, null
        return false
    }
    try {
        // parse the arguments.
        parser.parseArgument(args)
    } catch( CmdLineException e ) {
        usage parser, e
        return false
    }
    true
}

def usage(parser, exception) {
    if (exception != null) {
        System.err.println(exception.getMessage())
    }
    parser.printUsage(System.err)
    System.err.println()
}

}`

/*
unit test
*/

class ArgsTest extends GroovyTestCase { void testUsage() { def argsParser = new Args() def args = [] as String[] argsParser.parse(args) } }

output:
please input arguments:
-host VAL : host config
-k VAL : found by this keyword
-s VAL : parse source file
-u VAL : udid

/*
if add usage at the line:
*/

@Option(name="-ignore", usage= "ignore", handler = StringArrayOptionHandler.class) String[] ignoreList

java.lang.NullPointerException
at java.lang.reflect.Array.getLength(Native Method)
at org.kohsuke.args4j.spi.ArrayFieldSetter.getValueList(ArrayFieldSetter.java:113)
at org.kohsuke.args4j.spi.OptionHandler.printDefaultValue(OptionHandler.java:139)
at org.kohsuke.args4j.CmdLineParser.createDefaultValuePart(CmdLineParser.java:358)
at org.kohsuke.args4j.CmdLineParser.printOption(CmdLineParser.java:335)
at org.kohsuke.args4j.CmdLineParser.printUsage(CmdLineParser.java:303)
at org.kohsuke.args4j.CmdLineParser.printUsage(CmdLineParser.java:273)
at org.kohsuke.args4j.CmdLineParser.printUsage(CmdLineParser.java:263)
at org.kohsuke.args4j.CmdLineParser$printUsage.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at Args.usage(Args.groovy:46)
at Args$usage$0.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:145)
at Args.parse(Args.groovy:29)
at Args$parse.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at ArgsTest.testUsage(ArgsTest.groovy:5)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)