我在boost::program options中有这段代码:
("output_path,o", po::value< std::string >(&outputPath)->implicit_value(""), "path to where the output should be created.")
和在命令行我有:
-o "C:My DataImagesWithDifferentResolution"
当boost选项用数据填充outputPath时,我在变量中获得此值:
C:My DataImagesWithDifferentResolution"
注意路径末尾的额外引号。
我该如何修复它?
<标题>编辑1要清楚,这是一个bug在增强。我知道在编译代码时应该转义字符串,但这是boost程序选项工作和从命令行提取输入数据的方式。
再解释一下:
我的程序名为testPrg.exe,我正试图以以下方式调用它:
testprg.exe -o "C:My DataImagesWithDifferentResolution"
这是正确的,我的用户应该能够做到这一点。没有必要转义 on命令行。
但是boost程序选项,错误地将最后的"转换成转义值。
测试显示错误的应用程序:
main()
{
po::options_description desc("Allowed options");
std::string outputPath;
desc.add_options()
("output_path,o", po::value< std::string >(&outputPath)->implicit_value(""), "path to where the output should be created.")
;
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
po::notify(vm);
std::cout<<outputPath <<std::endl;
}
使用boost 1.58编译cod,并按照上面的说明运行它,并检查输出
标题>这不是Boost.Program_options中的一个bug;它是编译器提供的Microsoft C/c++启动代码的预期行为。也就是说,当"
在argv
中传递到main
时,它已经转化为"
。
摘自Visual Studio 2015参考文档中的解析C命令行参数:
前面带反斜杠的双引号
"
被解释为文字双引号("
)。Command-Line Input | argv[1] | argv[2] | argv[3] -------------------+---------+---------+--------- "ab"c" "\" d | ab"c | | d
一个可能的解决方案可能是调用GetCommandLine
来获取lpCommandLine
并将其传递给split_winmain
(尽管这可能与Microsoft启动代码具有相同的行为)或自己拆分命令行