根据文件,我可以在风格解析配置文件:
[main section]
string = hello world.
[foo]
message = Hi !
但我需要解析插件列表:
[plugins]
somePlugin.
HelloWorldPlugin
AnotherPlugin
[settings]
type = hello world
我怎样才能得到的字符串,其在插件部分的载体?
根据文件,我可以在风格解析配置文件:
[main section]
string = hello world.
[foo]
message = Hi !
但我需要解析插件列表:
[plugins]
somePlugin.
HelloWorldPlugin
AnotherPlugin
[settings]
type = hello world
我怎样才能得到的字符串,其在插件部分的载体?
对于升压程序选项的配置文件,如果线路未声明的部分,如[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