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

[QUESTION] Validate input arguments without running the command #489

Closed alegomes closed 4 months ago

alegomes commented 6 months ago

Is it possible to validate the command's input arguments without having to execute it?

What I want:

assertFailsWith<SomeCliktError> {
    MyCommand().parse(listOf("..."))
}

...but without necessarily executing the command, which is cost-intensive.

I followed the CliktCommand.parse() > Parser.parse() > command.run() execution stack, but I didn't see a way to run Parser.parse() with the canRun=false parameter.

thanks a lot Alexandre

ajalt commented 6 months ago

This has been discussed before in #449.

run is called in the middle of parsing. If we skipped run, it could result in different behavior, which is why I haven't added this feature.

But since multiple people have asked for it, it might be worth adding, maybe as a property on the context.

In the mean time, you could add your own dry run property that you check in run if you need it.