【问题标题】:boost program options - append multitoken options from config file to command-lineboost 程序选项 - 将多令牌选项从配置文件附加到命令行
【发布时间】:2015-10-06 16:41:46
【问题描述】:

使用 boost 程序选项我试图允许用户在配置文件 (.ini) 中为多令牌参数设置默认值,该参数将附加到他们的命令行选择中。

例子:

程序选项:

m_Desc.add_options()
    ("settings,s", po::value<string>("FILE")->default_value("~/.config.ini")->multitoken(), "Settings")
    ("tax,t", po::value<vector<string>>("name|rate")->multitoken(), "Tax")
;

try {

    po::store(
        po::command_line_parser(argc, argv).
        options(m_Desc).
        positional(m_Pos).
        run(),
        m_Vm);

    ifstream config(m_Vm["settings"].as<string>(), ifstream::in);

    if(config) {
        po::store(
            po::parse_config_file(config, m_Desc),
            m_Vm);

    }

    if (m_Vm.count("help")) {
        Usage();
        return;
    }

    po::notify(m_Vm);

} catch(const po::error &e) {
    throw MyCustomException(e.what());
}

用户配置

// config.ini
tax = gst|7
tax = vat|5

// What happens:
$ ./a.out --tax another|3
Tax:
another|3

$ ./a.out
Tax:
gst|7
vat|5

// What I'd like:
$ ./a.out --tax another|3
Tax:
gst|7
another|3
vat|5

$ ./a.out
Tax:
gst|7
vat|5

如何自定义 boost PO 以合并多令牌选项而不是覆盖?

我尝试将命令行和配置文件中的选项存储在单独的变量映射中,然后合并,但这成为我的其他命令行选项的问题。

【问题讨论】:

    标签: c++ boost boost-program-options


    【解决方案1】:

    你要找的价值函数是-&gt;composing()

    ("settings,s", 
        po::value<string>("FILE")->default_value("~/.config.ini")->multitoken()->composing(), 
        "Settings")
    
    ("tax,t", 
        po::value<vector<string>>("name|rate")->multitoken()->composing(), 
        "Tax")
    

    【讨论】:

    • 啊,我错过了一些东西(www.boost.org/doc/libs/1_55_0/doc/html/program_options/tutorial.html)。 '对于“组合”选项,例如“包含文件”,这些值被合并。 '。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2015-11-10
    • 2010-11-02
    • 1970-01-01
    • 2020-03-02
    相关资源
    最近更新 更多