Boost logo

Boost :

From: Pavol Droba (droba_at_[hidden])
Date: 2003-05-23 10:21:14

> > I like the idea proposed by Tanton. po::parameter should have a validation
> > predicate as an argument.
> > A reasonable set of arguments can be provided by library, and the user can
> > always provide a specific ones.
> The story is a bit more complex. There's a 'validator' which can be set to
> do anything.
> desc.add_options()
> ("foo", "bar").validator(your_nifty_functor)
> ;
> However, say you extensively use value ranges. You'd have to write this
> ".validator()" part everywhere, and "your_nifty_functor" will actually be
> creating of some functor, which is passed two values. This all is verbose.
> You can, however, write a single function, say int_range, which will take
> parameter name, and two ints, and set up the validation function. That
> would be more convenient.
> The only problem with current 'validator' is that it should validate
> *string* and return the value. So, the example given by Tanton:
> _1 >= 1 && _1 <= 9)
> would not really work --- the validator function is given a string. It's
> probably possible to use 'notify' callback to do such kind of validation,
> though. Or add another validator --- which is given converted value.

I see. However I think this is a little bit clumsy to use. I think it may be better
to strip validation from the conversion.

What I had in mind is an extension to po::parameter funtion to something like

        template< typename T >
        *unspecified* po::parameter<T>(
                        string parameter_name,
                        T& variable,
                        const boost::function1<bool, T)& validator );

so the framework would convert string parameter to a specified type and then run the validator
functor on the result to validate it.

> >> > * Format specification and checking for string. Regex
> >> > specification of something like scanf would be nice.
> >> > This can be useful for example to check if the parameter is a
> >> > filename.
> >>
> >> Could you clarify a bit how scanf-like specification can work? You mean
> >> it can be used to specify syntax?
> >>
> >
> > I mean something like "%02.4f" or something like that... it is just an
> > idea. If there would be a validation predicate as an argument, regex
> > library can be used to define regex predicate which would cover most of
> > the cases.
> Ah... ok. Indeed --- the validator can do that already, and in case of
> string validation, the current code will work.
That's nice. Can you post a piece of code as an example?


Boost list run by bdawes at, gregod at, cpdaniel at, john at