Skip to content

DefaultParser emits an empty word if the cursor is at the end of the string #1489

@madisp

Description

@madisp

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions