Is there a slick kotlin way of handling this? I h...
# announcements
c
Is there a slick kotlin way of handling this? I have a series of cmdline args like "--root /home/foo --root /other/dir --algo MD5" and I want to turn them into a Map<String,List<String>>
t
chb0kotlin: Have you considered using some lib to handle CLI params?
d
If so, I'd definitely recommend JCommander simple smile http://jcommander.org/#_kotlin
👍 1
c
I am intentionally not using any libraries - normally I would but this is for demonstrating kotlins capabilities as a language. I need to take the commandline args and turn it into necessary types to pass to a contructor:
Copy code
public DeDup(Set<File> roots, String hashAlgo, Set<String> fileTypes) {
        this.roots = roots;
        this.hashAlgo = hashAlgo;
        this.fileTypes = fileTypes;
    }
x
There are lots of corner cases to parsing command line args. eg, what happens if number of arguments is odd?
(shameless plug) kotlin-argparser would make parsing those options pretty easy:
Copy code
class Args(parser: ArgParser) {
    val root by parser.adding("directories to search")
    val algo by parser.storing("hash algorithm")
    val fileTypes by parser.adding("file types to process",
            initialValue = mutableSetOf<String>()) { this }
}
c
@chb0kotlin I respect your decision not to use any library but if you have any JCommander question, ask away. I'm using it with Kotlin without any efforts, although it's obviously not an idiomatically Kotlin library
t
@chb0kotlin here a snippet to start with (just the happy path) :
val (keys, values) = args.partition { it.startsWith("--") } val result = keys.zip(values) .groupBy { it.first } .map { it.key to it.value.map { it.second } } .toMap()
Assuming that args is a Array<String>
c
I never thought to use partition. I am gonna have to give this a try.
t
@chb0kotlin I thought in a much more elegant solution:
args.partition { it.startsWith("--") } .let { it.first.zip(it.second) } .groupBy( {it.first}, {it.second} )
c
WOW!
c
Note that this will obviously fail if the command you're parsing is not using an exact even number of args
t
Exactly... As I said, this cover only the happy path.
c
That's all ok. It's for demonstrating how Kotlin can save lines of code and provide clarity. The application it's replicating doesn't handle commandline args any better
Def appreciate it. If I can get team buy in for kotlin on the next project I will revisit this 🙂