CLI11 icon indicating copy to clipboard operation
CLI11 copied to clipboard

Update transform validators documentation

Open JonathanGzzBen opened this issue 3 years ago • 3 comments

Close #688 Adds an example on how to use transform validator objects and clarifies the usage of the return type of the base function.

JonathanGzzBen avatar Dec 12 '21 21:12 JonathanGzzBen

I was looking at this a little more. I think part of the confusion is that the transform method that takes a lambda uses a different signature from the std::function used in the validator object. So you can't just take that same lambda function and stick it in a Validator object even though they are compatible. You get very different results.

Also if the string return from the validator is not empty, then an exception gets generated from that. So it is permissible but not necessary to generate an exception for failed validation inside the function. You can just return an error message as the result.

Not sure if there is a better way to do that or not? The best I can think of right now is to add an error flag to the validator, so if validator failed then that flag must be set and then the input is always const and the output is either a message or the transformed value depending on whether the flag was set

phlptp avatar Dec 13 '21 16:12 phlptp

Returning the error message is preferred, since CLI11 might be able to run without exceptions enabled one day. That's why that signature is used.

I think this is a clear upgrade, but we could follow up with information about the validator std::function?

henryiii avatar Jan 28 '22 05:01 henryiii