ajalt / clikt

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

Disable prompt() globally #494

Closed jacobk closed 6 months ago

jacobk commented 6 months ago

Hello!

In line with https://clig.dev/#interactivity

If --no-input is passed, don’t prompt or do anything interactive. This allows users an explicit way to disable all prompts in commands. If the command requires input, fail and tell the user how to pass the information as a flag.

It would be great to have the ability to disable all prompt() and have them fall back to being normal options. Including handling of required() as needed.

Maybe this is already possible?

ajalt commented 6 months ago

This isn't built in, but you could implement it yourself:

val noInput by option().flag()
val foo by option().transformAll {
    when {
        noInput -> "foo default" // or `throw MissingOption(this)` if it's required
        else -> terminal.prompt("Enter foo")
    }
}
JakeWharton commented 6 months ago

Cool resource, btw! I sent a PR to add Clikt: https://github.com/cli-guidelines/cli-guidelines/pull/128

jacobk commented 6 months ago

This isn't built in, but you could implement it yourself:

val noInput by option().flag()
val foo by option().transformAll {
    when {
        noInput -> "foo default" // or `throw MissingOption(this)` if it's required
        else -> terminal.prompt("Enter foo")
    }
}

Thanks! Didn't know about transformAll.