|
Boost : |
Subject: [boost] program_options custom validators
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2009-07-10 19:15:36
Hello, recently I was faced with a situation of writing a custom
validator for a certain command line option I wanted to handle. The
validator was supposed to check whether the value on the command line
was in a certain range. After some head scratching, I realized
there's no elegant way to do this with the current validation overload
function. In the end I just ended up doing the validation after the
options had been parsed.
I was thinking about a good way to support this, and I thought that
maybe the best way is to add an additional value semantic so that the
validate function could instead be:
template<class T, class validation_data>
void validate(boost::any& v, const std::vector<std::string>& values,
const validation_data& data, T* target_type, int);
and we could write
add_options() ("test", value<int>()->validate(boost::interval<int>(1, 10)) );
this would require the user to provide the following overload:
void validate(boost::any& v, std::vector<std::string>& values, const
boost::interval<int>& data, int* target_type, int);
the default implementation of validate would all use some dummy type
for validation_data, this way compilation would fail if user calls
validate but does not provide the appropriate
overload. And if the user does not want to provide additional
validation data he can simply use the current syntax, but instead
would provide the following validate() overload
void validate(boost::any& v, const std::vector<std::string>& values,
const no_validation_data& data, T* target_type, int);
Thoughts?
Zach
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk