c++ - boost::program_options 是否支持要求一系列替代方案中的一个?

标签 c++ boost boost-program-options

我正在使用 boost::program_options 来指定我的 C++ 应用程序的参数。有没有办法指定一组备选方案中需要一个参数?

<application> [--one int-value1 | --two string-value2 | --three]

在上面,用户必须恰好传递以下选项之一:--one--two--three .

我可以手动执行此操作,但希望有一个内置机制来代替这个:

#include <boost/program_options.hpp>

namespace po = boost::program_options;

int main(int argc, char *argv[]) {
  po::options_description options;
  int band;
  std::string titles_path;

  options.add_options()
    ("one", po::value<int>(&band)->default_value(1))
    ("two", po::value<std::string>(&titles_path))
    ("three");

  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, options), vm);

  if (1 != (vm.count("one") + vm.count("two") + vm.count("three"))) {
    std::cerr << options << std::endl;

    return -11;
  }
  return 0;
}

有没有更好的方法使用 boost 选项来做到这一点?

最佳答案

program_options 验证器不支持参数相互依赖(包括负依赖)。

也许您现在所做的实际上是最好的选择。

关于c++ - boost::program_options 是否支持要求一系列替代方案中的一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15501714/

相关文章:

c++ - Boost.Program_options - 免费值(没有选项的值)

c++ - 使用gcc编译c/c++程序

c++ - 通过索引运算符插入到 boost::program_options::variables_map

c++ - 在滚动条上跟踪鼠标事件

c++ - boost multi_index 获得依赖类型

c++ - Boost Spirit X3 eol 意外行为

c++ - Boost 事件日志未正确显示事件

c++ - 来自 boost 程序选项的 bool 选项

C++11 升级技术

c++ - C++ 函数中静态变量的生命周期是多少?