-
Notifications
You must be signed in to change notification settings - Fork 122
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
Adding validation for mutually exclusive option groups #265
Comments
There aren't built-in The main reason this doesn't exist yet is because I'm not sure if there's a general solution for an error message. For a single option, we print out something like |
I am only interested in validating the options in the group individually, here's my example:
Are you suggesting I can in fact validate these individually? My error messages whould be something like "name too long" if over a certain length, similar for id. |
Yes, although you'll need to specify types explicitly, since the private val strategy: Strategy? by mutuallyExclusiveOptions(
option("--id", help = "Name")
.convert<String, Strategy> { Strategy.IdBased(it) }
.check { (it as Strategy.IdBased).id.isNotEmpty() },
option("--name", help = "Id")
.convert<String, Strategy> { Strategy.NameBased(it) }
.validate { require((it as Strategy.NameBased).name.isNotEmpty()) { "name too short" } }
).single() |
Ok, I made the parameters covariant in #267, so with that change you'll be able to validate individual options without explicitly specifying the types. Let me know if that solves your problem. |
I'm using mutually exclusive option groups as described here
https://ajalt.github.io/clikt/options/#mutually-exclusive-option-groups
but would like to also do parameter validation with validate
https://ajalt.github.io/clikt/parameters/#validate
This doesn't seem to be supported as the returned types are not compatible as far as I can see.
If I'm wrong and this is in fact possible, please provide an example.
The text was updated successfully, but these errors were encountered: