boost 程序选项在从命令行读取时更改数据(这是 boost 中的错误吗?)
boost program options changes data when it reads from command line (is it a bug in boost?)
我在 boost::program 选项中有此代码:
("output_path,o", po::value< std::string >(&outputPath)->implicit_value(""), "path to where the output should be created.")
在命令行上我有:
-o "C:\My Data\ImagesWithDifferentResolution\"
当 boost 选项用数据填充 outputPath 时,我在变量中得到这个值:
C:\My Data\ImagesWithDifferentResolution"
注意路径末尾的额外引号。
我该如何解决?
编辑 1
需要说明的是,这是 boost 中的一个错误。我知道在编译代码时应该对字符串进行转义,但这是 boost 程序选项的工作方式以及从命令行提取输入数据的方式。
所以再解释一下:
我的程序名为 testPrg.exe,我正尝试按以下方式调用它:
testprg.exe -o "C:\My Data\ImagesWithDifferentResolution\"
这是正确的,我的用户应该能够做到这一点。无需在命令行上转义 \。
但是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 中的错误;这是编译器提供的 Microsoft C/C++ 启动代码的预期行为。也就是说,在argv
.
传递给main
时,\"
已经转换为"
来自 Parsing C Command-Line Arguments Visual Studio 2015 参考:
A double quotation mark preceded by a backslash, \"
, is interpreted as a literal double quotation mark ("
).
Command-Line Input | argv[1] | argv[2] | argv[3]
-------------------+---------+---------+---------
"ab\"c" "\" d | ab"c | \ | d
一个可能的解决方法是调用 GetCommandLine
来获取 lpCommandLine
和 pass that to split_winmain
(尽管这可能与 Microsoft 启动代码具有相同的行为)或自己拆分命令行.
我在 boost::program 选项中有此代码:
("output_path,o", po::value< std::string >(&outputPath)->implicit_value(""), "path to where the output should be created.")
在命令行上我有:
-o "C:\My Data\ImagesWithDifferentResolution\"
当 boost 选项用数据填充 outputPath 时,我在变量中得到这个值:
C:\My Data\ImagesWithDifferentResolution"
注意路径末尾的额外引号。
我该如何解决?
编辑 1
需要说明的是,这是 boost 中的一个错误。我知道在编译代码时应该对字符串进行转义,但这是 boost 程序选项的工作方式以及从命令行提取输入数据的方式。
所以再解释一下:
我的程序名为 testPrg.exe,我正尝试按以下方式调用它:
testprg.exe -o "C:\My Data\ImagesWithDifferentResolution\"
这是正确的,我的用户应该能够做到这一点。无需在命令行上转义 \。
但是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 中的错误;这是编译器提供的 Microsoft C/C++ 启动代码的预期行为。也就是说,在argv
.
main
时,\"
已经转换为"
来自 Parsing C Command-Line Arguments Visual Studio 2015 参考:
A double quotation mark preceded by a backslash,
\"
, is interpreted as a literal double quotation mark ("
).Command-Line Input | argv[1] | argv[2] | argv[3] -------------------+---------+---------+--------- "ab\"c" "\" d | ab"c | \ | d
一个可能的解决方法是调用 GetCommandLine
来获取 lpCommandLine
和 pass that to split_winmain
(尽管这可能与 Microsoft 启动代码具有相同的行为)或自己拆分命令行.