first.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright Vladimir Prus 2002-2004.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt
  4. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. /* The simplest usage of the library.
  6. */
  7. #include <boost/program_options.hpp>
  8. namespace po = boost::program_options;
  9. #include <iostream>
  10. #include <iterator>
  11. using namespace std;
  12. int main(int ac, char* av[])
  13. {
  14. try {
  15. po::options_description desc("Allowed options");
  16. desc.add_options()
  17. ("help", "produce help message")
  18. ("compression", po::value<double>(), "set compression level")
  19. ;
  20. po::variables_map vm;
  21. po::store(po::parse_command_line(ac, av, desc), vm);
  22. po::notify(vm);
  23. if (vm.count("help")) {
  24. cout << desc << "\n";
  25. return 0;
  26. }
  27. if (vm.count("compression")) {
  28. cout << "Compression level was set to "
  29. << vm["compression"].as<double>() << ".\n";
  30. } else {
  31. cout << "Compression level was not set.\n";
  32. }
  33. }
  34. catch(exception& e) {
  35. cerr << "error: " << e.what() << "\n";
  36. return 1;
  37. }
  38. catch(...) {
  39. cerr << "Exception of unknown type!\n";
  40. }
  41. return 0;
  42. }