We're using jline to build a CLI application with roughly the following code:
val terminal = TerminalBuilder.terminal()
val reader = LineReaderBuilder.builder()
.terminal(terminal)
.parser(BoCliLineParser())
.build()
while (true) {
reader.readLine("$ ")
val words = reader.parsedLine.words()
val cmd = words.first()
val args = words.drop(1)
// lookup cmd, invoke with args
}
There's a surprising property that a space at the end of the input line (e.g. user happens to write echo foo bar will include an empty-string word at the end, i.e.
val line = "echo foo bar "
val out = DefaultParser().parse(line, line.length)
println(out.words())
will print [echo, foo, bar, ].
We could work around by filtering out empty words at the end, but this has the downside of also filtering out explicit empty strings, e.g. echo foo bar "" would also filter out the empty word even though the user definitely intended for it to be there.