#include #include #include #include #include #include #include namespace boostpo = boost::program_options; boost::program_options::options_description desc("options"); void usage() { std::cout << "program_options_tester help" << std::endl; std::cout << "this program demonstrates the boost program_options library" << std::endl; std::cout << desc << std::endl; } int main(int argc, char** argv) { desc.add_options() ("help", "display usage") ("mint,i", boostpo::value(), "my int ") // --mint or -i ("mdouble", boostpo::value(), "my double") ("mstring", boostpo::value(), "my string") ("mnames,n", boostpo::value >(), "vector of strings") ; boostpo::variables_map vm; try { boostpo::store(boostpo::parse_command_line(argc, argv, desc), vm); } catch(boostpo::error& e) { std::cerr << e.what() << std::endl; return 1; } boostpo::notify(vm); if (vm.count("help")) { usage(); return 1; } std::cout << std::left; if (vm.count("mint")) { std::cout << std::setw(20) << "mint: " << vm["mint"].as() << std::endl; } if (vm.count("mdouble")) { std::cout << std::setw(20) << "mdouble: " << vm["mdouble"].as() << std::endl; } if (vm.count("mstring")) { std::cout << std::setw(20) << "mmstring: " << vm["mstring"].as() << std::endl; } if (vm.count("mnames")) { std::cout << std::setw(20) << "mnames count: " << vm["mnames"].as< std::vector >().size() << std::endl; std::cout << std::setw(20) << "mnames: "; std::vector::const_iterator end = vm["mnames"].as< std::vector >().end(); for (std::vector::const_iterator iter = vm["mnames"].as< std::vector >().begin(); iter != end; ++iter) { std::cout << *iter << " "; } std::cout << std::endl; } return 0; }