option_groups.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /** This example shows how to handle options groups.
  6. For a test, run:
  7. option_groups --help
  8. option_groups --num-threads 10
  9. option_groups --help-module backend
  10. The first invocation would show to option groups, and will not show the
  11. '--num-threads' options. The second invocation will still get the value of
  12. the hidden '--num-threads' option. Finally, the third invocation will show
  13. the options for the 'backend' module, including the '--num-threads' option.
  14. */
  15. #include <boost/program_options/options_description.hpp>
  16. #include <boost/program_options/parsers.hpp>
  17. #include <boost/program_options/variables_map.hpp>
  18. #include <boost/tokenizer.hpp>
  19. #include <boost/token_functions.hpp>
  20. using namespace boost;
  21. using namespace boost::program_options;
  22. #include <iostream>
  23. #include <fstream>
  24. #include <exception>
  25. using namespace std;
  26. int main(int ac, char* av[])
  27. {
  28. try {
  29. // Declare three groups of options.
  30. options_description general("General options");
  31. general.add_options()
  32. ("help", "produce a help message")
  33. ("help-module", value<string>(),
  34. "produce a help for a given module")
  35. ("version", "output the version number")
  36. ;
  37. options_description gui("GUI options");
  38. gui.add_options()
  39. ("display", value<string>(), "display to use")
  40. ;
  41. options_description backend("Backend options");
  42. backend.add_options()
  43. ("num-threads", value<int>(), "the initial number of threads")
  44. ;
  45. // Declare an options description instance which will include
  46. // all the options
  47. options_description all("Allowed options");
  48. all.add(general).add(gui).add(backend);
  49. // Declare an options description instance which will be shown
  50. // to the user
  51. options_description visible("Allowed options");
  52. visible.add(general).add(gui);
  53. variables_map vm;
  54. store(parse_command_line(ac, av, all), vm);
  55. if (vm.count("help"))
  56. {
  57. cout << visible;
  58. return 0;
  59. }
  60. if (vm.count("help-module")) {
  61. const string& s = vm["help-module"].as<string>();
  62. if (s == "gui") {
  63. cout << gui;
  64. } else if (s == "backend") {
  65. cout << backend;
  66. } else {
  67. cout << "Unknown module '"
  68. << s << "' in the --help-module option\n";
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. if (vm.count("num-threads")) {
  74. cout << "The 'num-threads' options was set to "
  75. << vm["num-threads"].as<int>() << "\n";
  76. }
  77. }
  78. catch(std::exception& e) {
  79. cout << e.what() << "\n";
  80. }
  81. }