7

ドキュメントによると、設定ファイルをスタイルで解析できます:

 [main section]
 string = hello world. 
 [foo]
 message = Hi !

しかし、プラグインのリストを解析する必要があります:

 [plugins]
 somePlugin. 
 HelloWorldPlugin
 AnotherPlugin
 [settings]
 type = hello world

plugins セクションにある文字列のベクトルを取得するにはどうすればよいですか?

4

1 に答える 1

10

ブースト プログラム オプションの構成ファイルの場合、その行が などのセクションを宣言していない場合、その行は[settings]ある形式である必要がありname=valueます。あなたの例では、次のように書きます。

【プラグイン】
名前 = プラグイン
名前 = HelloWorldPlugin
name = 別のプラグイン
[設定]
タイプ = こんにちは世界

プラグインのリストは、マルチトークン オプションである必要がある "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: 別のプラグイン
settings.type: ハローワールド
于 2012-06-16T22:49:30.637 に答える