Boost logo

Boost Users :

From: Fredrik Hedman (hedman_at_[hidden])
Date: 2005-08-24 05:36:09


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. 

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net