c++ - 如何使用 boost::program_options 解析本身包含开关的命令行参数?

标签 c++ boost-program-options

我正在用 C++ 编写一个程序,它是一些基准测试的包装器,在开头包含一些设置代码,在结尾包含分析代码。

我想同时运行两个基准测试。这些的原始命令行是:

/path0/benchmark0 -switch0 -switch1 -switch2
/path1/benchmark1 -arg0 -arg1 -arg2 -arg4

我想把这些放在我的包装器的命令行上:

wrapper -setup_arg0 -setup_arg1 -analysis_arg0 --command0 /path0/benchmark0 -switch0 -switch1 -switch2 --command1 /path1/benchmark1 -arg0 -arg1 -arg2 -arg4

我想在哪里得到两个std::vector<std::string> s,每个 command0command1 ,包含原始命令行。这就是我的做法(使用 boost::program_options ):

("command0", po::value<std::vector< std::string> >(&command0)->multitoken(), "command line for thread 0")
("command1", po::value<std::vector< std::string> >(&command1)->multitoken(), "command line for thread 1")

这基本上有效。但是,如果基准测试的参数以 - 开头(就像我见过的大多数程序上的大多数开关一样),program_options尝试将它们解析为包装器开关的一部分,因为它不知道它们应该在 command0 下分组在一起或 command1 .

是否 program_options支持吗?如果是,怎么办?


示例:

在我工作的地方,有一个惯例是通过像这样“终止”多 token 来做到这一点:

wrapper <snip> --command0 /path0/benchmark0 -switch0 -switch1 -switch2 -command0- 

(在这个例子中,我用 --command0 终止了 -command0-。)

我怎样才能制作program_options就这么处理?

最佳答案

我认为最好将 command0command1 的值作为单个字符串。例如,

wrapper --command0 "/path0/benchmark0 ..." --command1 "/path1/benchmark1 ..."

是的,您需要做更多的工作,因为您必须 wordexp 您各自的命令字符串(除非您已经将这些字符串直接传递给 shell ;-)),但它更干净将包装器的内容和调用的命令的内容分开。

关于c++ - 如何使用 boost::program_options 解析本身包含开关的命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10227489/

相关文章:

c++ - c++ 多线程递归调用终止

c# - 如何检索可执行文件的相对路径

c++ - 未定义引用 boost::program_options::abstract_variables_map::operator[]

c++ - 将参数从 boost 程序选项导出到 doxygen

c++ - 使用辅助函数 (c++) 定义运行时已知的全局常量变量

C++ Box2d 内存管理/垃圾收集

c++ - 从电影文件 (QuickTime-API) 中将每一帧导出为图像

c++ - Qt 如何知道何时连接了新的 USB 存储设备?

c++ - 自定义验证器不允许 default_value

c++ - Boost 程序选项 : force "="