增强程序选项设置最小值和最大值的选项

时间:2022-11-29 17:43:11

Is it possible to set minimum and maximum limit of a value (suppose it is unsigned short and I need a value between 0 and 10) as I can set default value by

是否可以设置一个值的最小值和最大值(假设它是无符号的,并且我需要在0到10之间的值),因为我可以设置默认值?

opt::value<unsigned short>()->default_value(5)

I want to use arguments given from variables map of program options immediately without checking each of them.

我想使用来自变量映射的参数,而不需要检查它们。

3 个解决方案

#1


7  

No, you cannot. All options are described here. You can check them manually, or write function, that will check them manually.

不,你不能。这里描述所有选项。您可以手动检查它们,或者编写函数,这将手动检查它们。

opt::value<unsigned short>()->default_value(5)->notifier(&check_function);

where check function is something like

检查函数是什么样子的?

void check(unsigned short value)
{
   if (value < 0 || value > 10)
   {
      // throw exception
   }
}

or more general

或更一般的

template<typename T>
void check_range(const T& value, const T& min, const T& max)
{
   if (value < min || value > max)
   {
      // throw exception
   }
}

opt::value<unsigned short>()->default_value(5)->notifier
(boost::bind(&check_range<unsigned short>, _1, 0, 10));

#2


8  

I recommend a lambda (like kaveish's answer). But you can have it return a function that checks the appropriate bounds to make everything more readable.

我推荐一个lambda(如kaveish的答案)。但是,您可以让它返回一个函数,该函数检查适当的界限,以使所有内容更易于阅读。

auto in = [](int min, int max, char const * const opt_name){
  return [opt_name, min, max](unsigned short v){ 
    if(v < min || v > max){ 
      throw opt::validation_error
        (opt::validation_error::invalid_option_value,
         opt_name, std::to_string(v));
    }
  };
};

opt::value<unsigned short>()->default_value(5)
  ->notifier(in(0, 10, "my_opt"));

#3


7  

In C++11 this can also be achieved using lambda expressions.

在c++ 11中,也可以使用lambda表达式来实现这一点。

opt::value<unsigned short>()
  ->default_value(5)
  ->notifier(
      [](std::size_t value)
      {
        if (value < 0 || value > 10) {
          // throw exception
        }
      })

This handily keeps the validation code itself close to the call point and allows you to customize the exception easier, something like

这样可以方便地将验证代码本身保持在调用点附近,并允许您更容易地自定义异常,比如

throw opt::validation_error(
  opt::validation_error::invalid_option_value,
  "option_name",
  std::to_string(value));

#1


7  

No, you cannot. All options are described here. You can check them manually, or write function, that will check them manually.

不,你不能。这里描述所有选项。您可以手动检查它们,或者编写函数,这将手动检查它们。

opt::value<unsigned short>()->default_value(5)->notifier(&check_function);

where check function is something like

检查函数是什么样子的?

void check(unsigned short value)
{
   if (value < 0 || value > 10)
   {
      // throw exception
   }
}

or more general

或更一般的

template<typename T>
void check_range(const T& value, const T& min, const T& max)
{
   if (value < min || value > max)
   {
      // throw exception
   }
}

opt::value<unsigned short>()->default_value(5)->notifier
(boost::bind(&check_range<unsigned short>, _1, 0, 10));

#2


8  

I recommend a lambda (like kaveish's answer). But you can have it return a function that checks the appropriate bounds to make everything more readable.

我推荐一个lambda(如kaveish的答案)。但是,您可以让它返回一个函数,该函数检查适当的界限,以使所有内容更易于阅读。

auto in = [](int min, int max, char const * const opt_name){
  return [opt_name, min, max](unsigned short v){ 
    if(v < min || v > max){ 
      throw opt::validation_error
        (opt::validation_error::invalid_option_value,
         opt_name, std::to_string(v));
    }
  };
};

opt::value<unsigned short>()->default_value(5)
  ->notifier(in(0, 10, "my_opt"));

#3


7  

In C++11 this can also be achieved using lambda expressions.

在c++ 11中,也可以使用lambda表达式来实现这一点。

opt::value<unsigned short>()
  ->default_value(5)
  ->notifier(
      [](std::size_t value)
      {
        if (value < 0 || value > 10) {
          // throw exception
        }
      })

This handily keeps the validation code itself close to the call point and allows you to customize the exception easier, something like

这样可以方便地将验证代码本身保持在调用点附近,并允许您更容易地自定义异常,比如

throw opt::validation_error(
  opt::validation_error::invalid_option_value,
  "option_name",
  std::to_string(value));