boost program options vector<int>
-
Hallo,
ich versuche seit Stunden, mit den boost program options eine Konfigurationsdatei mit folgendem Eintrag als vector<int> zu parsen:
compression = 1.9 vector = 1 2 3
Hier ist mein Code (Beispiel von boost, modifiziert):
#include <boost/program_options.hpp> namespace po = boost::program_options; #include <iostream> #include <iterator> #include <vector> #include <fstream> using namespace std; // A helper function to simplify the main part. template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { copy(v.begin(), v.end(), std::ostream_iterator<T>(os, " ")); return os; } int main(int argc, char* argv[]) { try { po::options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") ("compression", po::value<double>(), "set compression level") ("vector", po::value<std::vector<int>>()->multitoken(), "") ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); std::string config_file = ".\\tst.cfg"; std::ifstream ifs(config_file); if (!ifs) { cout << "can not open config file: " << config_file << "\n"; } else { po::store(po::parse_config_file(ifs, desc), vm); notify(vm); cout << "read config file: " << config_file << "\n"; } if (vm.count("help")) { cout << desc << "\n"; return 0; } if (vm.count("compression")) { cout << "Compression level was set to " << vm["compression"].as<double>() << ".\n"; } else { cout << "_Compression level was not set.\n"; } if (vm.count("vector")) { cout << "Vector was set to " << vm["vector"].as<std::vector<int> >() << ".\n"; } else { cout << "Vector was not set.\n"; } } catch (exception& e) { cerr << "error: " << e.what() << "\n"; } catch (...) { cerr << "Exception of unknown type!\n"; } char c; cin >> c; return 0; }
Wenn ich die Optionen per Kommandozeile übergebe, ist alles OK:
>ConsoleApplication1.exe --vector 1 2 3 --compression 6 can not open config file: .\tst.cfg Compression level was set to 6. Vector was set to 1 2 3 .
Wenn ich aber die Datei parse (ohne Kommandozeilenoptionen), bekomme ich:
>ConsoleApplication1.exe error: the argument ('1 2 3') for option 'vector' is invalid
Funktionieren die vector typen nicht mit dem po::parse_config_file?
-
Hey,
nee, geht nicht.
Du musst jeden Wert einzeln in der Datei ablegen:
compression = 1.9 vector = 1 vector = 2 vector = 3
Oder halt nen String nehmen und selber parsen.
-
Danke!