real.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include <boost/program_options.hpp>
  6. using namespace boost::program_options;
  7. #include <iostream>
  8. using namespace std;
  9. /* Auxiliary functions for checking input for validity. */
  10. /* Function used to check that 'opt1' and 'opt2' are not specified
  11. at the same time. */
  12. void conflicting_options(const variables_map& vm,
  13. const char* opt1, const char* opt2)
  14. {
  15. if (vm.count(opt1) && !vm[opt1].defaulted()
  16. && vm.count(opt2) && !vm[opt2].defaulted())
  17. throw logic_error(string("Conflicting options '")
  18. + opt1 + "' and '" + opt2 + "'.");
  19. }
  20. /* Function used to check that of 'for_what' is specified, then
  21. 'required_option' is specified too. */
  22. void option_dependency(const variables_map& vm,
  23. const char* for_what, const char* required_option)
  24. {
  25. if (vm.count(for_what) && !vm[for_what].defaulted())
  26. if (vm.count(required_option) == 0 || vm[required_option].defaulted())
  27. throw logic_error(string("Option '") + for_what
  28. + "' requires option '" + required_option + "'.");
  29. }
  30. int main(int argc, char* argv[])
  31. {
  32. try {
  33. string ofile;
  34. string macrofile, libmakfile;
  35. bool t_given = false;
  36. bool b_given = false;
  37. string mainpackage;
  38. string depends = "deps_file";
  39. string sources = "src_file";
  40. string root = ".";
  41. options_description desc("Allowed options");
  42. desc.add_options()
  43. // First parameter describes option name/short name
  44. // The second is parameter to option
  45. // The third is description
  46. ("help,h", "print usage message")
  47. ("output,o", value(&ofile), "pathname for output")
  48. ("macrofile,m", value(&macrofile), "full pathname of macro.h")
  49. ("two,t", bool_switch(&t_given), "preprocess both header and body")
  50. ("body,b", bool_switch(&b_given), "preprocess body in the header context")
  51. ("libmakfile,l", value(&libmakfile),
  52. "write include makefile for library")
  53. ("mainpackage,p", value(&mainpackage),
  54. "output dependency information")
  55. ("depends,d", value(&depends),
  56. "write dependencies to <pathname>")
  57. ("sources,s", value(&sources), "write source package list to <pathname>")
  58. ("root,r", value(&root), "treat <dirname> as project root directory")
  59. ;
  60. variables_map vm;
  61. store(parse_command_line(argc, argv, desc), vm);
  62. if (vm.count("help")) {
  63. cout << desc << "\n";
  64. return 0;
  65. }
  66. conflicting_options(vm, "output", "two");
  67. conflicting_options(vm, "output", "body");
  68. conflicting_options(vm, "output", "mainpackage");
  69. conflicting_options(vm, "two", "mainpackage");
  70. conflicting_options(vm, "body", "mainpackage");
  71. conflicting_options(vm, "two", "body");
  72. conflicting_options(vm, "libmakfile", "mainpackage");
  73. conflicting_options(vm, "libmakfile", "mainpackage");
  74. option_dependency(vm, "depends", "mainpackage");
  75. option_dependency(vm, "sources", "mainpackage");
  76. option_dependency(vm, "root", "mainpackage");
  77. cout << "two = " << vm["two"].as<bool>() << "\n";
  78. }
  79. catch(exception& e) {
  80. cerr << e.what() << "\n";
  81. }
  82. }