2015-10-06 35 views
3

使用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 

如何自升压PO合并多令牌选项而不是覆盖?

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

回答

2

你正在寻找的价值功能是->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") 
+0

啊,小东西我错过了(www.boost.org/doc/libs/1_55_0/doc/html/program_options/tutorial.html) 。 '对于“组成”选项,如“包含文件”,这些值将被合并。 ”。谢谢! –