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

Is it possible to emit an error when parsing an option for groups other than the selected? #390

Open
Difocd opened this issue Feb 15, 2023 · 0 comments

Comments

@Difocd
Copy link

Difocd commented Feb 15, 2023

The groupChoice feature allow users to define multiple groups and use an option to select one from these. The doc said that:

Options for groups other than the selected one are ignored

I am wondering if it's possible to emit an error other than just ignoring the option from other group. And then it will be less error prone.

For example:

sealed class LoadConfig(name: String): OptionGroup(name)
class FromDisk : LoadConfig("Options for loading from disk") {
    val path by option().file().required()
    val followSymlinks by option().flag()
}

class FromNetwork: LoadConfig("Options for loading from network") {
    val url by option().required()
    val username by option().prompt()
    val password by option().prompt(hideInput = true)
}

class Tool : CliktCommand(help = "An example of a custom help formatter that uses ansi colors") {
    val load by option().groupChoice(
            "disk" to FromDisk(),
            "network" to FromNetwork()
    )

    override fun run() {
        when(val it = load) {
            is FromDisk -> echo("Loading from disk: ${it.path}")
            is FromNetwork -> echo("Loading from network: ${it.url}")
            null -> echo("Not loading")
        }
    }
}

Usage:

$ ./tool --load=disk --path=./config --follow-symlinks --username=admin
Error: No option "--username" when "--load=disk"
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

No branches or pull requests

1 participant