ajalt / clikt

Multiplatform command line interface parsing for Kotlin
https://ajalt.github.io/clikt/
Apache License 2.0
2.56k stars 123 forks source link

No way to timeout on missing input #521

Closed pdb-stripe closed 5 months ago

pdb-stripe commented 5 months ago

I am looking for a solution that would allow terminal.readLineOrNull to fail after some period of time, however this does not seem to work. This seems to occur in some other process? So even if I fail the running process, the JVM doesn't shut down. For example, the given code should fail and return null - and it does, except the process never completes until input is received

   private fun Terminal.readLineOrTimeout(): String? = runBlocking {
        val executor = Executors.newSingleThreadExecutor()
        var response: String? = null
        executor.submit {
            response = readLineOrNull(hideInput = true)
        }
        delay(1000)
        executor.shutdownNow()
        response

    }
ajalt commented 5 months ago

That's normal JVM thread behavior. If you want to shutdown without waiting for a thread, you need make it a daemon thread.

On JVM, Terminal.readLineOrNull is a wrapper for kotlin.io.readLineOrNull, so I don't think there's anything to do here on my end.

pdb-stripe commented 5 months ago

ack, got it. Thanks for the quick response!