Advertisement
kokorins

boost::program_options example

Mar 26th, 2013
4,908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <boost/program_options.hpp>
  3. #include <boost/algorithm/string.hpp>
  4.  
  5. namespace po=boost::program_options;
  6.  
  7. void train(const po::variables_map& vm)
  8. {
  9.   std::string input_path, info_path, output_path;
  10.   if (vm.count("input"))
  11.     input_path = vm["input"].as<std::string>();
  12.   if(vm.count("info"))
  13.     info_path = vm["info"].as<std::string>();
  14.   if(vm.count("output"))
  15.     output_path = vm["output"].as<std::string>();
  16.   std::cout<<"train: "<<input_path<<" "<<info_path<<" "<<output_path<<std::endl;
  17. }
  18.  
  19. void recognize(const po::variables_map& vm)
  20. {
  21.   std::string params_path, output_path;
  22.   std::vector<std::string> input_files;
  23.   if (vm.count("input"))
  24.     input_files = vm["input"].as<std::vector<std::string> >();
  25.   if(vm.count("params"))
  26.     params_path = vm["params"].as<std::string>();
  27.   if(vm.count("output"))
  28.     output_path = vm["output"].as<std::string>();
  29.   std::cout<<"recognize: "<<boost::algorithm::join(input_files, " ")<<" "<<params_path<<" "<<output_path<<std::endl;
  30. }
  31.  
  32. void score(const po::variables_map& vm)
  33. {
  34.   std::string test_path, output_path;
  35.   std::string ethanol_files;
  36.   if (vm.count("ethanol"))
  37.     ethanol_files = vm["ethanol"].as<std::string>();
  38.   if(vm.count("test"))
  39.     test_path = vm["test"].as<std::string>();
  40.   if(vm.count("output"))
  41.     output_path = vm["output"].as<std::string>();
  42.   std::cout<<"test: "<<ethanol_files<<" "<<test_path<<" "<<output_path<<std::endl;
  43. }
  44.  
  45. int main(int ac, char* av[])
  46. {
  47.   po::options_description desc("General options");
  48.   std::string task_type;
  49.   desc.add_options()
  50.     ("help,h", "Show help")
  51.     ("type,t", po::value<std::string>(&task_type), "Select task: train, recognize, score")
  52.     ;
  53.   po::options_description train_desc("Train options");
  54.   train_desc.add_options()
  55.     ("input,I", po::value<std::string>(), "Input .dat file")
  56.     ("info,i", po::value<std::string>(), "Input .trn file")
  57.     ("output,O", po::value<std::string>(), "Output parameters file .prs")
  58.     ;
  59.   po::options_description recognize_desc("Recognize options");
  60.   recognize_desc.add_options()
  61.     ("input,I",  po::value<std::vector<std::string> >(), "Input .dat file")
  62.     ("params,p", po::value<std::string>(), "Input .prs file")
  63.     ("output,O", po::value<std::string>(), "Output directory")
  64.     ;
  65.   po::options_description score_desc("Score options");
  66.   score_desc.add_options()
  67.     ("ethanol,e",  po::value<std::string>(), "Etalon .trn file")
  68.     ("test,t", po::value<std::string>(), "Testing .trn file")
  69.     ("output,O", po::value<std::string>(), "Output comparison file")
  70.     ;
  71.   po::variables_map vm;
  72.   try {
  73.     po::parsed_options parsed = po::command_line_parser(ac, av).options(desc).allow_unregistered().run();
  74.     po::store(parsed, vm);
  75.     po::notify(vm);
  76.     if(task_type == "train") {
  77.       desc.add(train_desc);
  78.       po::store(po::parse_command_line(ac,av,desc), vm);
  79.       train(vm);
  80.     }
  81.     else if(task_type == "recognize") {
  82.       desc.add(recognize_desc);
  83.       po::store(po::parse_command_line(ac,av,desc), vm);
  84.       recognize(vm);
  85.     }
  86.     else if(task_type=="score") {
  87.       desc.add(score_desc);
  88.       po::store(po::parse_command_line(ac,av,desc), vm);
  89.       score(vm);
  90.     }
  91.     else {
  92.       desc.add(train_desc).add(recognize_desc).add(score_desc);
  93.       std::cout << desc << std::endl;
  94.     }
  95.   }
  96.   catch(std::exception& ex) {
  97.     std::cout << desc << std::endl;
  98.   }
  99.   return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement