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

Cooccuring option group with required options is passed as nullable #495

Closed realkarmakun closed 4 months ago

realkarmakun commented 6 months ago

I have following optional group idea is to make it required at all times and use coocurring group as those options are needed for almost all commands:

class ExplicitCredentials: OptionGroup() {
    val panelUrl: String by option("--url", "-u")
        .required()
        .help("Pterodactyl Instance")
    val apiKey: String by option("--api-key", "-a")
        .required()
        .help("API key for an Instance")
}

The usage of said group is as follows

class PowerActionCommand : CliktCommand(name = "power") {
    val credentials by ExplicitCredentials().cooccurring()
    val servers by ServerIds().cooccurring()
    val action: PowerAction by argument().enum()

    override fun run() {
        credentials?.let { creds ->
            val api = PteroBuilder.createClient(creds.panelUrl, creds.apiKey)
            servers?.let {
                for (server in it.serverIds) {
                    api.retrieveServerByIdentifier(server)
                        .flatMap { clientServer -> clientServer.setPower(action) }
                        .execute()
                }
            }
        } ?: echo("No credentials specified")
    }
}

Is it possible to mark group as not nullable in this case?

ajalt commented 6 months ago

I might be misunderstanding the request, but if you want all the options in the group to be required all the time, you don't need coocurring. Just do val group by MyGroup().

realkarmakun commented 4 months ago

Hello @ajalt . Sorry for long reply. I want those to be optionally overridden, but by default I'd like to resolve them from filesystem. At the time I've been under impression that coocurring is needed to allow for Options to be applicable for all commands (because that's what I need essentially)

ajalt commented 4 months ago

coocurring is not necessary for that.