2012-04-19 43 views
0

我正在用C++编写一个程序,它是一些基准的包装,包含开始时的一些设置代码和最后的分析代码。如何解析使用boost :: program_options自身包含开关的命令行参数?

我想最多并行运行两个基准测试。这些原COMMANDLINES是:

/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支持吗?如果是这样,怎么样?


实施例:

我在哪里工作存在通过 “终止” 像这样的多令牌这样一个约定:

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

(在这个例子中我与-command0-终止--command0

我该如何让program_options像这样处理?

回答

1

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

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

是的,有你在,你必须wordexp你们各自的命令字符串(除非你已经只是路过这些字符串直奔外壳;-))更多的工作,但它更干净分离了解包装的内容以及调用的命令的内容。

+0

我需要将命令传递给'exec'。有没有一种很好的方法将这样的字符串传递给'exec',或者我需要将它分解为一个字符串数组?那是什么'wordexp'呢? – 2012-04-19 12:13:15

+0

如果您的命令字符串是可信的(即您不必担心攻击者等),只需使用'execl(“/ bin/sh”,“/ bin/sh”,“-c”,cmd, static_cast (0))'。如果它不被信任,那么是的,使用'wordexp'来扩展。 – 2012-04-19 15:10:35