Hello, I would like to be able to specify different sanity checks on the input data using the program_options framework. This should be a common enough requirement, but I can not find any example that exactly fits the bill. To be more specific I have a solution that kind of works, but I think it needs a bit to much typing. Here is what I have now: /*! Check for values greater than min. */ template <typename T> class value_gt { public: value_gt(std::string const & msg, T min) : msg_(msg), min_(min) {} void operator()(T const & v) const { if (v > min_) return; ostringstream os; os << msg_ << " to small. Must be larger than " << min_ << " . Found " << v; throw pgmopt::invalid_option_value(os.str()); } private: std::string msg_; T min_; }; void doSetProgram(Options & options) { options.add_options() ("help,h", "show this message") ("density", pgmopt::value<double>()->notifier( value_gt<double>("density", 1.0)), "initial density") ("particles", pgmopt::value<unsigned>()->notifier( value_gt<unsigned>("particles", 100)), "number of particles") ; } I don't like repeating the type (see code above) and would rather like to have access to the name of the option in the function that does the check. What would be a better solution? Adding to the validators idea or? -- F.
Hi Fredrik,
Here is what I have now:
/*! Check for values greater than min. */ template <typename T> class value_gt { public: value_gt(std::string const & msg, T min) : msg_(msg), min_(min) {} void operator()(T const & v) const { if (v > min_) return; ....... void doSetProgram(Options & options) { options.add_options() ("help,h", "show this message") ("density", pgmopt::value<double>()->notifier( value_gt<double>("density", 1.0)), .....
I don't like repeating the type (see code above) and would rather like to have access to the name of the option in the function that does the check.
What would be a better solution? Adding to the validators idea or?
I'd suggest creating a class template derived from typed_value<T>, overriding the 'xparse' method and adding the check there, after calling the inherited version. This will bring your example down to: ("density", pgmopt::value_gt<double>("density", "1.0") It would be nicer to write: ("density", pgmopt::value<double>()->validator(_1 < 1.0) (Using boost.lambda to create validator expression), but it's not supported yet. It's something I'd like to add, though. - Volodya
On Wednesday 24 August 2005 15.15, Vladimir Prus wrote:
Hi Fredrik,
Here is what I have now:
/*! Check for values greater than min. */ template <typename T> class value_gt { public: value_gt(std::string const & msg, T min) : msg_(msg), min_(min) {} void operator()(T const & v) const { if (v > min_) return;
.......
void doSetProgram(Options & options) { options.add_options() ("help,h", "show this message") ("density", pgmopt::value<double>()->notifier( value_gt<double>("density", 1.0)),
.....
I don't like repeating the type (see code above) and would rather like to have access to the name of the option in the function that does the check.
What would be a better solution? Adding to the validators idea or?
I'd suggest creating a class template derived from typed_value<T>, overriding the 'xparse' method and adding the check there, after calling the inherited version. This will bring your example down to:
("density", pgmopt::value_gt<double>("density", "1.0")
Hi Vladimir, This is almost working now, need to clean it up and add a couple of ctors to the class plus an overloaded helper function for value that returns typed_value pointer. Thanks! What is the recommendation regarding namespaces? Should I put my own stuff in boost::program_options, or should it be kept outside?
It would be nicer to write:
("density", pgmopt::value<double>()->validator(_1 < 1.0)
(Using boost.lambda to create validator expression), but it's not supported yet. It's something I'd like to add, though.
That would be very cool! Two followup questions. 1) is there a way to make a multitoken option stop? As it is now it just seems to munch everything... --opt1multi 1 2 3 --opt2multi a b c would be nice, but how to do it? 2) related to this is the question of how to have options like vector1 = 1 33 44 vector2 = 1.0 2.0 3.0 4.0 in a file that is parsed by parse_config_file? What about the " = "? Does it really have to be there? Could it not be relegated to a style or more general rule? -- Cheers! F.
participants (2)
-
Fredrik Hedman
-
Vladimir Prus