根据文档,我可以解析样式中的配置文件:
[main section]
string = hello world.
[foo]
message = Hi !
但我需要解析插件列表:
[plugins]
somePlugin.
HelloWorldPlugin
AnotherPlugin
[settings]
type = hello world
如何获取插件部分中的字符串向量?
答案 0 :(得分:10)
对于提升程序选项配置文件,如果该行未声明某个部分(例如[settings]
),则它必须采用name=value
格式。对于您的示例,请将其写为以下内容:
[plugins] name = somePlugin name = HelloWorldPlugin name = AnotherPlugin [settings] type = hello world
插件列表现在将对应于“plugins.name”选项,该选项需要是多音色选项。
下面是一个示例程序,它从settings.ini文件中读取上述设置:
#include <boost/program_options.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
namespace po = boost::program_options;
typedef std::vector< std::string > plugin_names_t;
plugin_names_t plugin_names;
std::string settings_type;
// Setup options.
po::options_description desc("Options");
desc.add_options()
("plugins.name", po::value< plugin_names_t >( &plugin_names )->multitoken(),
"plugin names" )
("settings.type", po::value< std::string >( &settings_type ),
"settings_type" );
// Load setting file.
po::variables_map vm;
std::ifstream settings_file( "settings.ini" , std::ifstream::in );
po::store( po::parse_config_file( settings_file , desc ), vm );
settings_file.close();
po::notify( vm );
// Print settings.
typedef std::vector< std::string >::iterator iterator;
for ( plugin_names_t::iterator iterator = plugin_names.begin(),
end = plugin_names.end();
iterator < end;
++iterator )
{
std::cout << "plugin.name: " << *iterator << std::endl;
}
std::cout << "settings.type: " << settings_type << std::endl;
return 0;
}
产生以下输出:
plugin.name: somePlugin plugin.name: HelloWorldPlugin plugin.name: AnotherPlugin settings.type: hello world