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

Init inside `Object` in Kotlin Script #482

Closed fiddlededee closed 9 months ago

fiddlededee commented 9 months ago

I use clikt in kts files and it is perfect. Could you please clarify one issue.

This code works as intended:

class Options1 : NoOpCliktCommand() {
    val input by option(help = "Input contents")
    init { main(args) }
}
println(Options1().input)

So does this one (the one I use):

object Options2 : NoOpCliktCommand() {
    val input by option(help = "Input contents")
}
Options2.main(args)
println(Options2.input)

But this one fails (would be cleanest in simple cases, IMHO):

object Options3 : NoOpCliktCommand() {
    val input by option(help = "Input contents")
    init { this.main(args) }
}
println(Options3.input)

The message is error: object Options3 captures the script class instance

ajalt commented 9 months ago

That error isn't from Clikt, it's from Kotlin itself, so there's nothing I can do about it on my end.

The first result from googling that error message was this Kotlin issue: https://youtrack.jetbrains.com/issue/KT-32410

Seems like it will be fixed in Kotlin 2.0.