// Copyright Vladimir Prus 2002-2004. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) /* Shows how to use both command line and config file. */ #include #include #include namespace po = boost::program_options; using namespace boost::algorithm; #include #include #include using namespace std; // A helper function to simplify the main part. template ostream& operator<<(ostream& os, const vector& v) { copy(v.begin(), v.end(), ostream_iterator(cout, " ")); return os; } int main(int ac, char* av[]) { try { set modules; po::options_description config("Configuration"); // Scan the configuration file to find all the used modules. { po::options_description inner_config("Configuration"); inner_config.add_options() ("module.*", po::value(), "") ; ifstream ifs("dynamic.cfg"); po::parsed_options p = parse_config_file(ifs, inner_config); for (size_t i = 0; i < p.options.size(); ++i) { vector parts; split(parts, p.options[i].string_key, is_any_of(".")); if (parts.size() > 2) modules.insert(parts[1]); } for(set::iterator i = modules.begin(), e = modules.end(); i != e; ++i) { config.add_options() (("module." + *i + ".dll").c_str(), po::value(), "") (("module." + *i + ".port").c_str(), po::value(), "") ; } } po::variables_map vm; ifstream ifs("dynamic.cfg"); store(parse_config_file(ifs, config), vm); notify(vm); for(set::iterator i = modules.begin(), e = modules.end(); i != e; ++i) { cout << *i << " : " << vm["module." + *i + ".dll"].as() << " : " << vm["module." + *i + ".port"].as() << "\n"; ; } } catch(exception& e) { cout << e.what() << "\n"; return 1; } return 0; }