我正在用 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,每个 command0
和 command1
,包含原始命令行。这就是我的做法(使用 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
就这么处理?
最佳答案
我认为最好将 command0
和 command1
的值作为单个字符串。例如,
wrapper --command0 "/path0/benchmark0 ..." --command1 "/path1/benchmark1 ..."
是的,您需要做更多的工作,因为您必须 wordexp
您各自的命令字符串(除非您已经将这些字符串直接传递给 shell ;-)),但它更干净将包装器的内容和调用的命令的内容分开。
关于c++ - 如何使用 boost::program_options 解析本身包含开关的命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10227489/