Having trouble using jcommander with Kotlin

112 views
Skip to first unread message

Bakkiaraj Murugesan

unread,
Jul 11, 2017, 6:50:53 AM7/11/17
to jcommander
Hi,

I am new to kotlin. I Like the jcommander by looking its documents and I like the Idea of annotations. I mainly work in "perl" and use "http://perldoc.perl.org/Getopt/Long.html". When I try to use jcommander  with kotlin, I get unexpected results. Since its used in kobalt, it should work with kotlin, I am doing a silly mistake which I could not find.


main.kt
===========
@file:JvmName("CheckForNewSVNTags")
........
import com.beust.jcommander.JCommander
import com.beust.jcommander.Parameter;

.........

// Following code is copied from Kobalt for learning 
class Args {
@Parameter
var targets: List<String> = arrayListOf()

@Parameter(names = arrayOf("--download"), description = "Force a download from the downloadUrl in the wrapper")
var download: Boolean = false

@Parameter(names = arrayOf("--pluginJarFiles"), description = "Comma-separated list of plug-in jar files")
var pluginJarFiles: String? = null

@Parameter(names = arrayOf("--port"), description = "Port, if --server was specified")
var port: Int? = null

}

fun main(args: Array<String>){
val cmdArgsObj = Args()
val result = JCommander().apply {
setProgramName("Test")
addObject(cmdArgsObj)
setVerbose(1)
}

result.parse("--download","--port=9090","--pluginJarFiles=a.txt")

println (cmdArgsObj.port)
println (cmdArgsObj.pluginJarFiles)
println(cmdArgsObj.download)
println(cmdArgsObj.targets)
}

Results
===========

[JCommander] Parsing "--download --port=9090 --pluginJarFiles=a.txt"

[JCommander] Found main parameter:com.beust.jcommander.Parameterized@b4304576
[JCommander] Adding description for --download
[JCommander] Adding description for --pluginJarFiles
[JCommander] Adding description for --port
[JCommander] Parsing arg: --download
[JCommander] Parsing arg: --port=9090
[JCommander] Parsing arg: --pluginJarFiles=a.txt
null
null
true
[--port=9090, --pluginJarFiles=a.txt]



Question: Why port, pluginJarFiles parameters are not set? Why only download is set? What am I missing?

Thanks,
Bakkiaraj M

Dieter Bogdoll

unread,
Sep 25, 2017, 4:18:13 PM9/25/17
to jcommander
Hi,

try this line

result.parse("--download","--port","9090","--pluginJarFiles","a.txt")
Best regards,
Dieter

Dieter Bogdoll

unread,
Sep 25, 2017, 4:18:17 PM9/25/17
to jcommander
Hi again,

another possibility is to prepend the following line before class Args { }

@Parameters(separators = "=")
class {
// the code as you have written
}

Best regards,
Dieter
Reply all
Reply to author
Forward
0 new messages