Advertisement
Guest User

boost.cpp

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  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.  
  6.  
  7. #include <boost/program_options.hpp>
  8. using namespace boost::program_options;
  9.  
  10. #include <iostream>
  11. #include <fstream>
  12. using namespace std;
  13.  
  14. /* Auxiliary functions for checking input for validity. */
  15.  
  16. /* Function used to check that 'opt1' and 'opt2' are not specified
  17.    at the same time. */
  18. void conflicting_options(const variables_map& vm, const char* opt1, const char* opt2)
  19. {
  20.     if (vm.count(opt1) && !vm[opt1].defaulted()
  21.         && vm.count(opt2) && !vm[opt2].defaulted())
  22.         throw logic_error(string("Conflicting options '")
  23.                           + opt1 + "' and '" + opt2 + "'.");
  24. }
  25.  
  26. /* Function used to check that of 'for_what' is specified, then
  27.    'required_option' is specified too. */
  28. void option_dependency(const variables_map& vm,
  29.                         const char* for_what, const char* required_option)
  30. {
  31.     if (vm.count(for_what) && !vm[for_what].defaulted())
  32.         if (vm.count(required_option) == 0 || vm[required_option].defaulted())
  33.             throw logic_error(string("Option '") + for_what
  34.                               + "' requires option '" + required_option + "'.");
  35. }
  36.  
  37. int main(int argc, char* argv[])
  38. {
  39.     try {
  40.         string ofile;
  41.         string macrofile, libmakfile;
  42.  
  43.         bool to_screen=true;
  44.         bool t_given = false;
  45.         bool b_given = false;
  46.         string input="gm";
  47.         string mainpackage;
  48.         string depends = "deps_file";
  49.         string sources = "src_file";
  50.         string root = ".";
  51.  
  52.         options_description desc("Allowed options");
  53.         desc.add_options()
  54.         // First parameter describes option name/short name
  55.         // The second is parameter to option
  56.         // The third is description
  57.         ("help,h", "print usage message")
  58.         ("output,o", value(&ofile), "pathname for output")
  59.         ("macrofile,m", value(&macrofile), "full pathname of macro.h")
  60.         ("two,t", bool_switch(&t_given), "preprocess both header and body")
  61.         ("body,b", bool_switch(&b_given), "preprocess body in the header context")
  62.         ("libmakfile,l", value(&libmakfile),
  63.              "write include makefile for library")
  64.         ("mainpackage,p", value(&mainpackage),
  65.              "output dependency information")
  66.         ("depends,d", value(&depends),
  67.          "write dependencies to <pathname>")
  68.         ("sources,s", value(&sources), "write source package list to <pathname>")
  69.         ("root,r", value(&root), "treat <dirname> as project root directory")
  70.         ("screen,с", value(&input) ,"write errors to file ")
  71.         ;
  72.    
  73.         variables_map vm;
  74.         store(parse_command_line(argc, argv, desc), vm);
  75.  
  76.         if (vm.count("help")) {  
  77.             cout << desc << "\n";
  78.             return 0;
  79.         }
  80.  
  81.        
  82.         /*if (vm.count("-p /var/log/zmq.log")){
  83.             std::ofstream file;
  84.             file.open("zmq.log");
  85.             file<<"-p + full name has been writen";//should be equal variable that contain error description
  86.             file.close();
  87.             to_screen = false;
  88.            // cout<<"executing -p";
  89.             return 0;
  90.         }
  91.         if (vm.count("-s screen")){
  92.             to_screen=true;
  93.             return 0;
  94.         }*/
  95.  
  96.        
  97.         conflicting_options(vm, "output", "two");
  98.         conflicting_options(vm, "output", "body");
  99.         conflicting_options(vm, "output", "mainpackage");
  100.         conflicting_options(vm, "two", "mainpackage");
  101.         conflicting_options(vm, "body", "mainpackage");
  102.         //conflicting_options(vm, "screen", "ipute");
  103.        
  104.         conflicting_options(vm, "two", "body");
  105.         conflicting_options(vm, "libmakfile", "mainpackage");
  106.         conflicting_options(vm, "libmakfile", "mainpackage");
  107.  
  108.         option_dependency(vm, "depends", "mainpackage");
  109.         option_dependency(vm, "sources", "mainpackage");
  110.         option_dependency(vm, "root", "mainpackage");
  111.  
  112.         cout << "two = " << vm["two"].as<bool>() << "\n";
  113.         //cout << "two = " << vm["ofile"].as<string>() << "\n";
  114.     }
  115.     catch(exception& e) {
  116.         cerr << e.what() << "\n";
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement