Onk*_*nde 7 c++ windows boost command-line-arguments boost-program-options
我想使用boost :: program_options解析多个命令行参数.但是,一些参数是用双引号括起来的字符串.这就是我所拥有的 -
void processCommands(int argc, char *argv[]) {
std::vector<std::string> createOptions;
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("create", boost::program_options::value<std::vector<std::string> >(&createOptions)->multitoken(), "create command")
;
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
boost::program_options::notify(vm);
if(vm.count("create") >= 1) {
std::string val1 = createOptions[0];
std::string val2 = createOptions[1];
...
// call some function passing val1, val2.
}
}
Run Code Online (Sandbox Code Playgroud)
我这样做很好
cmdparsing.exe --create arg1 arg2
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时不起作用
cmdparsing.exe --create "this is arg1" "this is arg2"
Run Code Online (Sandbox Code Playgroud)
来自windows命令行.对于第二个选项,它将转换为["this" "is" "arg1" "this" "is" "arg2"]createOptions向量.因此,val1获得"this"和val2得到
"is",而不是"this is arg1"和"this is arg2"分别.
如何使用boost :: program_option使其工作?
我使用本机 Windows 函数修复了它,该函数以不同的方式处理命令行参数。有关详细信息,请参阅CommandLineToArgvW。在将其传递给 processCommands() 之前,我使用上述方法修改 argv[] 和 argc。感谢 Bart van Ingen Schenau 的评论。
#ifdef _WIN32
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (NULL == argv)
{
std::wcout << L"CommandLineToArgvw failed" << std::endl;
return -1;
}
#endif
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5472 次 |
| 最近记录: |