简体   繁体   English

当选项字符串包含混合语言字符时,boost :: program_option :: store抛出异常

[英]boost::program_option::store throws exception when option string contains mixed language characters

I have simple code that works perfectly well with input option contains just ASCII characters, but throws an exception with error message of "error: character conversion failed". 我有简单的代码,与输入选项仅包含ASCII字符完美匹配,但抛出异常,错误消息为“错误:字符转换失败”。 Is there a solution? 有解决方案吗?

Background info: 背景资料:

    1. Compiler and OS: VC++2012 running on Windows 8.1 64 bit    
    2. "_UNICODE" option is ON    It works with command like: tmain.exe --input
    3. "c:\test_path\test_file_name.txt"    It fails with command like:
         tmain.exe --input "c:\test_path\test_file_name_中文.txt"    My default
    4. I am using boost v1.53.
    5. locale is Australian English.

This is the source code: 这是源代码:

#include <boost/program_options.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
    try {
        std::locale::global(std::locale(""));
        po::options_description desc("Allowed options");
        desc.add_options()
            ("input",  po::value<std::string>(), "Input file path.")
        ;

        po::variables_map vm;        
        po::store(po::parse_command_line(argc, argv, desc), vm);
        po::notify(vm);    

        if (vm.count("input")) { 
            std::cout << "Input file path: " << vm["input"].as<std::string>();
        }
        return 0;
    }
    catch(std::exception& e) {
        std::cerr << "error: " << e.what() << "\n";
        return 1;
    }
    catch(...) {
        std::cerr << "Exception of unknown type!\n";
    }
    return 0;
}

I have stepped into the boost codes, and found that the exception was thrown from this function (boost_1_53_0\\libs\\program_options\\src\\convert.cpp): 我已经进入了boost代码,发现异常是从这个函数抛出的(boost_1_53_0 \\ libs \\ program_options \\ src \\ convert.cpp):

BOOST_PROGRAM_OPTIONS_DECL std::string 
to_8_bit(const std::wstring& s, 
            const std::codecvt<wchar_t, char, std::mbstate_t>& cvt)
{
    return detail::convert<char>(
        s,                 
        boost::bind(&codecvt<wchar_t, char, mbstate_t>::out,
                    &cvt,
                    _1, _2, _3, _4, _5, _6, _7));
}

When I stepped into boost code, I found out that this statement 当我介入代码时,我发现了这个陈述

boost::program_options::parse_command_line(argc, argv, desc)

actually works fine, it is the boost::program_options::store() function that fails to convert string in utf-8 back to wstring. 实际上工作正常,它是boost :: program_options :: store()函数,无法将utf-8中的字符串转换回wstring。 The reason of this failure could be that my current code-page does not support non-ASCII characters. 这种失败的原因可能是我当前的代码页不支持非ASCII字符。 I guess my code would work well if my current locale were a Chinese based one. 如果我当前的语言环境是基于中文的语言环境,我想我的代码会运行良好。 Is there any solution to my problem? 我的问题有什么解决方案吗? Many thanks in advance. 提前谢谢了。

For an option to be Unicode aware it has to be added with wvalue instead of value . 对于可wvalue Unicode的选项,必须使用wvalue而不是value添加。 Try this: 尝试这个:

desc.add_options()("input",  po::wvalue<std::string>(), "Input file path.");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM