multiple_sources.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /* Shows how to use both command line and config file. */
  6. #include <boost/program_options.hpp>
  7. namespace po = boost::program_options;
  8. #include <iostream>
  9. #include <fstream>
  10. #include <iterator>
  11. using namespace std;
  12. // A helper function to simplify the main part.
  13. template<class T>
  14. ostream& operator<<(ostream& os, const vector<T>& v)
  15. {
  16. copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
  17. return os;
  18. }
  19. int main(int ac, char* av[])
  20. {
  21. try {
  22. int opt;
  23. string config_file;
  24. // Declare a group of options that will be
  25. // allowed only on command line
  26. po::options_description generic("Generic options");
  27. generic.add_options()
  28. ("version,v", "print version string")
  29. ("help", "produce help message")
  30. ("config,c", po::value<string>(&config_file)->default_value("multiple_sources.cfg"),
  31. "name of a file of a configuration.")
  32. ;
  33. // Declare a group of options that will be
  34. // allowed both on command line and in
  35. // config file
  36. po::options_description config("Configuration");
  37. config.add_options()
  38. ("optimization", po::value<int>(&opt)->default_value(10),
  39. "optimization level")
  40. ("include-path,I",
  41. po::value< vector<string> >()->composing(),
  42. "include path")
  43. ;
  44. // Hidden options, will be allowed both on command line and
  45. // in config file, but will not be shown to the user.
  46. po::options_description hidden("Hidden options");
  47. hidden.add_options()
  48. ("input-file", po::value< vector<string> >(), "input file")
  49. ;
  50. po::options_description cmdline_options;
  51. cmdline_options.add(generic).add(config).add(hidden);
  52. po::options_description config_file_options;
  53. config_file_options.add(config).add(hidden);
  54. po::options_description visible("Allowed options");
  55. visible.add(generic).add(config);
  56. po::positional_options_description p;
  57. p.add("input-file", -1);
  58. po::variables_map vm;
  59. store(po::command_line_parser(ac, av).
  60. options(cmdline_options).positional(p).run(), vm);
  61. notify(vm);
  62. ifstream ifs(config_file.c_str());
  63. if (!ifs)
  64. {
  65. cout << "can not open config file: " << config_file << "\n";
  66. return 0;
  67. }
  68. else
  69. {
  70. store(parse_config_file(ifs, config_file_options), vm);
  71. notify(vm);
  72. }
  73. if (vm.count("help")) {
  74. cout << visible << "\n";
  75. return 0;
  76. }
  77. if (vm.count("version")) {
  78. cout << "Multiple sources example, version 1.0\n";
  79. return 0;
  80. }
  81. if (vm.count("include-path"))
  82. {
  83. cout << "Include paths are: "
  84. << vm["include-path"].as< vector<string> >() << "\n";
  85. }
  86. if (vm.count("input-file"))
  87. {
  88. cout << "Input files are: "
  89. << vm["input-file"].as< vector<string> >() << "\n";
  90. }
  91. cout << "Optimization level is " << opt << "\n";
  92. }
  93. catch(exception& e)
  94. {
  95. cout << e.what() << "\n";
  96. return 1;
  97. }
  98. return 0;
  99. }