Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mutuallyExclusiveOptions run error when option is Boolean type #349

Closed
dongfengweixiao opened this issue May 9, 2022 · 0 comments · Fixed by #351
Closed

mutuallyExclusiveOptions run error when option is Boolean type #349

dongfengweixiao opened this issue May 9, 2022 · 0 comments · Fixed by #351

Comments

@dongfengweixiao
Copy link

dongfengweixiao commented May 9, 2022

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.groups.mutuallyExclusiveOptions
import com.github.ajalt.clikt.parameters.groups.single
import com.github.ajalt.clikt.parameters.options.convert
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option


sealed class RunMode01 {
    data class Server(val isS: Boolean) : RunMode01()
    data class Client(val address: String) : RunMode01()
}

class Config01 : CliktCommand() {
    private val runMode: RunMode01? by mutuallyExclusiveOptions(
        option("-s", "--server").flag().convert { RunMode01.Server(it) },
        option("-c", "--client").convert { RunMode01.Client(it) }
    ).single()

    override fun run() = echo(runMode)
}

sealed class RunMode02 {
    data class Server(val isS: String) : RunMode02()
    data class Client(val address: String) : RunMode02()
}

class Config02 : CliktCommand() {
    private val runMode: RunMode02? by mutuallyExclusiveOptions(
        option("-s", "--server").convert { RunMode02.Server(it) },
        option("-c", "--client").convert { RunMode02.Client(it) }
    ).single()

    override fun run() = echo(runMode)
}

fun main(args: Array<String>) {

    val config01 = Config01()
    print("case 01: =>")
    config01.main(arrayOf("-s"))
//    print("case 02: =>")
//    config01.main(arrayOf("-c", "aaa"))  // error
    val config02 = Config02()
    print("case 03: =>")
    config02.main(arrayOf("-s", "bbb"))
    print("case 04: =>")
    config02.main(arrayOf("-c", "ccc"))
}

case 01: =>Server(isS=true)
case 02: =>Usage: config01 [OPTIONS]

Error: option --server cannot be used with --client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant