jcommander icon indicating copy to clipboard operation
jcommander copied to clipboard

Bug: Strange behaviour for single or multiple main paramters

Open dibog opened this issue 7 years ago • 0 comments

In the following code fragment the first and the third block works as expected, where as the 3rd throws an exception.

The output is attached behind the source code. `class NormalArgs { @Parameter(names=["-f"]) var argPath : Path? = null }

class MainArgs { @Parameter var mainPath : Path? = null }

class MainArgs2 { @Parameter var mainPaths = mutableListOf<Path>() }

fun main(args: Array<String>) {

val normalArgs = NormalArgs()
JCommander.newBuilder()
        .addObject(normalArgs)
        .build()
        .parse("-f", "/path/to/my/file1")

println("f = ${normalArgs.argPath}")

try {
    val mainArgs = MainArgs()
    JCommander.newBuilder()
            .addObject(mainArgs)
            .build()
            .parse("/path/to/my/file2")

    println("main = ${mainArgs.mainPath}")
}
catch(e: Exception) {
    e.printStackTrace()
}

val mainArgs2 = MainArgs2()
JCommander.newBuilder()
        .addObject(mainArgs2)
        .build()
        .parse("/path/to/my/file3", "/path/to/my/file4")

println("main2 = ${mainArgs2.mainPaths}")

}`

And here is the output:

f = \path\to\my\file1 com.beust.jcommander.ParameterException: Could not invoke null Reason: Can not set java.nio.file.Path field net.bogdoll.utils.MainArgs.mainPath to java.lang.String at com.beust.jcommander.Parameterized.set(Parameterized.java:263) at com.beust.jcommander.JCommander$MainParameter.addValue(JCommander.java:101) at com.beust.jcommander.JCommander.parseValues(JCommander.java:773) ... main2 = [\path\to\my\file3, \path\to\my\file4]

So depending if I require one or more main parameters, the converting works or fails.

Best regards, Dieter

dibog avatar Feb 17 '18 15:02 dibog