Changeset 9dcce3


Ignore:
Timestamp:
Jul 6, 2012, 10:18:43 AM (13 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
685e28
Parents:
311137
git-author:
Frederik Heber <heber@…> (05/31/12 08:33:43)
git-committer:
Frederik Heber <heber@…> (07/06/12 10:18:43)
Message:

Refactored parsing of command line parameters into structure ControllerOptions.

  • this structure can be derived from to add other parsing functions and options.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/controller.cpp

    r311137 r9dcce3  
    306306}
    307307
     308struct ControllerOptions
     309{
     310  int parseHelp(boost::program_options::variables_map &vm, const boost::program_options::options_description &desc) {
     311    if (vm.count("help")) {
     312      std::cout << desc << "\n";
     313      return 1;
     314    }
     315    return 0;
     316  }
     317  int parseVerbosity(boost::program_options::variables_map &vm) {
     318    if (vm.count("verbosity")) {
     319      LOG(0, "STATUS: Verbosity level was set to " << vm["verbosity"].as<size_t>() << ".");
     320      setVerbosity(vm["verbosity"].as<size_t>());
     321    } else {
     322      LOG(0, "STATUS: Verbosity level was not set, defaulting to 5.");
     323      setVerbosity(5);
     324    }
     325    return 0;
     326  }
     327
     328  int parseServerPort(boost::program_options::variables_map &vm) {
     329    if (vm.count("server")) {
     330      server = vm["server"].as< std::string >();
     331      serverport = server.substr(server.find_last_of(':')+1, std::string::npos);
     332      server = server.substr(0, server.find_last_of(':'));
     333      try {
     334        boost::lexical_cast<size_t>(serverport);
     335      } catch (boost::bad_lexical_cast &) {
     336        ELOG(1, "Could not interpret " << serverport << " as server:port.");
     337        return 255;
     338      }
     339      LOG(1, "INFO: Using " << server << ":" << serverport << " as server's address.");
     340    } else {
     341      ELOG(1, "Requiring server's address (host:port) to connect to.");
     342      return 255;
     343    }
     344    return 0;
     345  }
     346
     347  int parseCommand(boost::program_options::variables_map &vm, const std::vector<std::string> &Commands) {
     348    if (!vm.count("command")) {
     349      ELOG(1, "Controller requires one of the following commands: "+toString(Commands));
     350      return 255;
     351    }
     352    command = vm["command"].as< std::string >();
     353    return 0;
     354  }
     355
     356  std::string command;
     357  std::string server;
     358  std::string serverport;
     359
     360};
     361
     362struct ControllerOptions_SystemCommandJob : public ControllerOptions
     363{
     364  int parseExecutable(boost::program_options::variables_map &vm) {
     365    if ((command == "createjobs") || (command == "addjobs")) {
     366      if (!vm.count("executable")) {
     367        ELOG(1, "'"+command+"' requires two options: [executable] [jobcommand].");
     368        return 255;
     369      }
     370      executable = vm["executable"].as< std::string >();
     371    }
     372    return 0;
     373  }
     374
     375  int parseJobCommand(boost::program_options::variables_map &vm) {
     376    if (command == "createjobs") {
     377      if (!vm.count("jobcommand")) {
     378        ELOG(1, "'"+command+"' requires two options: [executable] [jobcommand].");
     379        return 255;
     380      }
     381      jobcommand = vm["jobcommand"].as< std::string >();
     382    }
     383    return 0;
     384  }
     385
     386  std::string executable;
     387  std::string jobcommand;
     388};
    308389
    309390int main(int argc, char* argv[])
     
    345426  boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
    346427  boost::program_options::notify(vm);
    347 
    348   if (vm.count("help")) {
    349     std::cout << desc << "\n";
    350     return 1;
    351   }
    352 
    353   if (vm.count("verbosity")) {
    354     LOG(0, "STATUS: Verbosity level was set to " << vm["verbosity"].as<size_t>() << ".");
    355     setVerbosity(vm["verbosity"].as<size_t>());
    356   } else {
    357     LOG(0, "STATUS: Verbosity level was not set, defaulting to 5.");
    358     setVerbosity(5);
    359   }
    360 
    361   std::string server;
    362   std::string serverport;
    363   if (vm.count("server")) {
    364     server = vm["server"].as< std::string >();
    365     serverport = server.substr(server.find_last_of(':')+1, std::string::npos);
    366     server = server.substr(0, server.find_last_of(':'));
    367     try {
    368       boost::lexical_cast<size_t>(serverport);
    369     } catch (boost::bad_lexical_cast) {
    370       ELOG(1, "Could not interpret " << serverport << " as server:port.");
    371       return 255;
    372     }
    373     LOG(1, "INFO: Using " << server << ":" << serverport << " as server's address.");
    374   } else {
    375     ELOG(1, "Requiring server's address (host:port) to connect to.");
    376     return 255;
    377   }
    378 
    379   if (!vm.count("command")) {
    380     ELOG(1, "Controller requires one of the following commands: "+toString(Commands));
    381     return 255;
    382   }
    383   const std::string command = vm["command"].as< std::string >();
    384   const CommandIndices commandIndex = getCommandIndex(CommandsMap, command);
     428  ControllerOptions_SystemCommandJob ControllerInfo;
     429
     430  ControllerInfo.parseHelp(vm, desc);
     431
     432  ControllerInfo.parseVerbosity(vm);
     433
     434  ControllerInfo.parseServerPort(vm);
     435
     436  ControllerInfo.parseCommand(vm, Commands);
     437  const CommandIndices commandIndex = getCommandIndex(CommandsMap, ControllerInfo.command);
     438
     439  ControllerInfo.parseExecutable(vm);
     440
     441  ControllerInfo.parseJobCommand(vm);
    385442
    386443  // check arguments
    387444  switch(commandIndex) {
    388445    case AddJobsIndex:
    389       if (!vm.count("executable") && !vm.count("jobfiles")) {
    390         ELOG(1, "'"+command+"' requires at least two options: [executable] [list of input files ...].");
    391         return 255;
    392       }
    393       break;
    394446    case CreateJobsIndex:
    395       if (!vm.count("executable") && !vm.count("jobcommand")) {
    396         ELOG(1, "'"+command+"' requires two options: [executable] [jobcommand].");
    397         return 255;
    398       }
    399447      break;
    400448    case CheckResultsIndex:
     
    404452    case ReceiveMPQCIndex:
    405453      if (!vm.count("fragment-path")) {
    406         ELOG(1, "'"+command+"' require one option: [path to fragment files].");
     454        ELOG(1, "'"+ControllerInfo.command+"' require one option: [path to fragment files].");
    407455        return 255;
    408456      }
     
    414462    case UnknownCommandIndex:
    415463    default:
    416       ELOG(1, "Unrecognized command '"+toString(command)+"'.");
     464      ELOG(1, "Unrecognized command '"+toString(ControllerInfo.command)+"'.");
    417465      return 255;
    418466      break;
     
    429477    switch(commandIndex) {
    430478      case AddJobsIndex:
    431         controller.requestIds(server, serverport, vm["jobfiles"].as< std::vector<std::string> >().size());
     479        controller.requestIds(ControllerInfo.server, ControllerInfo.serverport, vm["jobfiles"].as< std::vector<std::string> >().size());
    432480        break;
    433481      case CreateJobsIndex:
    434         controller.requestIds(server, serverport, 1);
     482        controller.requestIds(ControllerInfo.server, ControllerInfo.serverport, 1);
    435483        break;
    436484      case CheckResultsIndex:
     
    446494      case UnknownCommandIndex:
    447495      default:
    448         ELOG(0, "Unrecognized command '"+toString(command)+"'.");
     496        ELOG(0, "Unrecognized command '"+toString(ControllerInfo.command)+"'.");
    449497        return 255;
    450498        break;
     
    474522        }
    475523        controller.addJobs(jobs);
    476         controller.sendJobs(server, serverport);
     524        controller.sendJobs(ControllerInfo.server, ControllerInfo.serverport);
    477525        break;
    478526      }
     
    481529        const JobId_t next_id = controller.getAvailableId();
    482530        std::vector<FragmentJob::ptr> jobs;
    483         const std::string executable = vm["executable"].as< std::string >();
    484         const std::string jobcommand = vm["jobcommand"].as< std::string >();
    485         createjobs(jobs, executable, jobcommand, next_id);
     531        createjobs(jobs, ControllerInfo.executable, ControllerInfo.jobcommand, next_id);
    486532        controller.addJobs(jobs);
    487         controller.sendJobs(server, serverport);
     533        controller.sendJobs(ControllerInfo.server, ControllerInfo.serverport);
    488534        break;
    489535      }
    490536      case CheckResultsIndex:
    491         controller.checkResults(server, serverport);
     537        controller.checkResults(ControllerInfo.server, ControllerInfo.serverport);
    492538        break;
    493539      case ReceiveResultsIndex:
    494540      case ReceiveMPQCIndex:
    495         controller.receiveResults(server, serverport);
     541        controller.receiveResults(ControllerInfo.server, ControllerInfo.serverport);
    496542        break;
    497543      case RemoveAllIndex:
    498         controller.removeall(server, serverport);
     544        controller.removeall(ControllerInfo.server, ControllerInfo.serverport);
    499545        break;
    500546      case ShutdownIndex:
    501         controller.shutdown(server, serverport);
     547        controller.shutdown(ControllerInfo.server, ControllerInfo.serverport);
    502548        break;
    503549      case UnknownCommandIndex:
    504550      default:
    505         ELOG(0, "Unrecognized command '"+toString(command)+"'.");
     551        ELOG(0, "Unrecognized command '"+toString(ControllerInfo.command)+"'.");
    506552        return 255;
    507553        break;
     
    549595      case UnknownCommandIndex:
    550596      default:
    551         ELOG(0, "Unrecognized command '"+toString(command)+"'.");
     597        ELOG(0, "Unrecognized command '"+toString(ControllerInfo.command)+"'.");
    552598        return 255;
    553599        break;
Note: See TracChangeset for help on using the changeset viewer.