| [e4afb4] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [e4afb4] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
 | 24 |  * CommandLineParser.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: May 8, 2010
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [e4afb4] | 36 | 
 | 
|---|
 | 37 | #include <boost/filesystem.hpp>
 | 
|---|
 | 38 | #include <boost/program_options.hpp>
 | 
|---|
 | 39 | #include <fstream>
 | 
|---|
 | 40 | #include <iostream>
 | 
|---|
| [5a8f38] | 41 | #include <set>
 | 
|---|
| [e4afb4] | 42 | #include <map>
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | #include "Actions/Action.hpp"
 | 
|---|
| [628577] | 45 | #include "Actions/ActionQueue.hpp"
 | 
|---|
| [3139b2] | 46 | #include "Actions/ActionTrait.hpp"
 | 
|---|
| [e4afb4] | 47 | #include "Actions/OptionRegistry.hpp"
 | 
|---|
 | 48 | #include "Actions/OptionTrait.hpp"
 | 
|---|
 | 49 | #include "Actions/Values.hpp"
 | 
|---|
| [ad011c] | 50 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 51 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [e4afb4] | 52 | #include "CommandLineParser.hpp"
 | 
|---|
| [3a21a7] | 53 | #include "CommandLineParser_validate.hpp"
 | 
|---|
| [11d433] | 54 | #include "World.hpp"
 | 
|---|
| [e4afb4] | 55 | 
 | 
|---|
| [ad011c] | 56 | #include "CodePatterns/Singleton_impl.hpp"
 | 
|---|
| [e4afb4] | 57 | 
 | 
|---|
| [ce7fdc] | 58 | using namespace MoleCuilder;
 | 
|---|
 | 59 | 
 | 
|---|
| [e4afb4] | 60 | class element;
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 | /** Constructor of class CommandLineParser.
 | 
|---|
 | 63 |  *
 | 
|---|
 | 64 |  */
 | 
|---|
 | 65 | CommandLineParser::CommandLineParser() :
 | 
|---|
 | 66 |   analysis("Analysis options"),
 | 
|---|
 | 67 |   atom("Atom options"),
 | 
|---|
| [f63e41] | 68 |   bond("Bond options"),
 | 
|---|
| [e4afb4] | 69 |   command("Command options"),
 | 
|---|
| [a88452] | 70 |   fill("fill options"),
 | 
|---|
| [4dc309] | 71 |   shape("shape options"),
 | 
|---|
| [e4afb4] | 72 |   fragmentation("Fragmentation options"),
 | 
|---|
| [d09093] | 73 |   graph("Graph options"),
 | 
|---|
| [e4afb4] | 74 |   molecule("Molecule options"),
 | 
|---|
| [d09093] | 75 |   options("Secondary options"),
 | 
|---|
| [e4afb4] | 76 |   parser("Parser options"),
 | 
|---|
| [f5724f] | 77 |   potential("Potential options"),
 | 
|---|
| [e4afb4] | 78 |   selection("Selection options"),
 | 
|---|
 | 79 |   tesselation("Tesselation options"),
 | 
|---|
| [d09093] | 80 |   world("World options")
 | 
|---|
| [e4afb4] | 81 | {
 | 
|---|
 | 82 |   // put all options lists into a lookup
 | 
|---|
 | 83 |   CmdParserLookup["analysis"] = &analysis;
 | 
|---|
 | 84 |   CmdParserLookup["atom"] = &atom;
 | 
|---|
| [f63e41] | 85 |   CmdParserLookup["bond"] = &bond;
 | 
|---|
| [e4afb4] | 86 |   CmdParserLookup["command"] = &command;
 | 
|---|
 | 87 |   CmdParserLookup["edit"] = &edit;
 | 
|---|
| [a88452] | 88 |   CmdParserLookup["fill"] = &fill;
 | 
|---|
| [4dc309] | 89 |   CmdParserLookup["shape"] = &shape;
 | 
|---|
| [e4afb4] | 90 |   CmdParserLookup["fragmentation"] = &fragmentation;
 | 
|---|
| [d09093] | 91 |   CmdParserLookup["graph"] = &graph;
 | 
|---|
 | 92 |   CmdParserLookup["options"] = &options;
 | 
|---|
| [e4afb4] | 93 |   CmdParserLookup["molecule"] = &molecule;
 | 
|---|
 | 94 |   CmdParserLookup["parser"] = &parser;
 | 
|---|
| [f5724f] | 95 |   CmdParserLookup["potential"] = &potential;
 | 
|---|
| [e4afb4] | 96 |   CmdParserLookup["selection"] = &selection;
 | 
|---|
 | 97 |   CmdParserLookup["tesselation"] = &tesselation;
 | 
|---|
 | 98 |   CmdParserLookup["world"] = &world;
 | 
|---|
 | 99 | }
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 | /** Destructor of class CommandLineParser.
 | 
|---|
 | 102 |  *
 | 
|---|
 | 103 |  */
 | 
|---|
 | 104 | CommandLineParser::~CommandLineParser()
 | 
|---|
 | 105 | {}
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 | /** Initializes command arguments to accept.
 | 
|---|
 | 108 |  * Goes through ActionRegistry and puts all actions therein into the map.
 | 
|---|
 | 109 |  */
 | 
|---|
 | 110 | void CommandLineParser::InitializeCommandArguments()
 | 
|---|
 | 111 | {
 | 
|---|
| [5a8f38] | 112 |   // we need a list of already added options, otherwise we get ambigious exceptions
 | 
|---|
 | 113 |   std::set<std::string> AlreadyAddedOptionNames;
 | 
|---|
 | 114 | 
 | 
|---|
| [e4afb4] | 115 |   bool ActionAlreadyAdded_flag = false;
 | 
|---|
| [690741] | 116 |   ActionQueue &AQ = ActionQueue::getInstance();
 | 
|---|
 | 117 |   ActionQueue::ActionTokens_t tokens = AQ.getListOfActions();
 | 
|---|
 | 118 |   for (ActionQueue::ActionTokens_t::const_iterator iter = tokens.begin();
 | 
|---|
 | 119 |       iter != tokens.end(); ++iter) {
 | 
|---|
 | 120 |     const ActionTrait &CurrentTrait = AQ.getActionsTrait(*iter);
 | 
|---|
| [e4afb4] | 121 |     ActionAlreadyAdded_flag = false;
 | 
|---|
 | 122 |     //std::cout << "Current Action to initialize is: " << actioniter->first << std::endl;
 | 
|---|
 | 123 | 
 | 
|---|
| [690741] | 124 |     for (ActionTrait::options_const_iterator optioniter = CurrentTrait.getBeginIter();
 | 
|---|
 | 125 |         optioniter != CurrentTrait.getEndIter();
 | 
|---|
| [e4afb4] | 126 |         ++optioniter) {
 | 
|---|
| [690741] | 127 |       if (optioniter->first == *iter)
 | 
|---|
| [e4afb4] | 128 |         ActionAlreadyAdded_flag = true;
 | 
|---|
 | 129 |       ASSERT( OptionRegistry::getInstance().isOptionPresentByName(optioniter->first),
 | 
|---|
| [5a8f38] | 130 |           "CommandLineParser::Init() - Option "+optioniter->first+" not present in OptionRegistry." );
 | 
|---|
| [e4afb4] | 131 |       const OptionTrait* const currentOption = OptionRegistry::getInstance().getOptionByName(optioniter->first);
 | 
|---|
 | 132 | 
 | 
|---|
| [5a8f38] | 133 |       if (AlreadyAddedOptionNames.find(optioniter->first) == AlreadyAddedOptionNames.end()) {
 | 
|---|
 | 134 |         // add the option
 | 
|---|
 | 135 | //        std::cout << "Registering Option "
 | 
|---|
 | 136 | //            << currentOption->getName()
 | 
|---|
 | 137 | //            << " with type '" << currentOption->getTypeName() << "' "
 | 
|---|
 | 138 | //            << " with description '" << currentOption->getDescription() << "' ";
 | 
|---|
 | 139 | //        if (currentOption->hasShortForm())
 | 
|---|
 | 140 | //          std::cout << ", with short form " << currentOption->getShortForm();
 | 
|---|
 | 141 | //        else
 | 
|---|
 | 142 | //          std::cout << ", with no short form ";
 | 
|---|
 | 143 | //        if (currentOption->hasDefaultValue())
 | 
|---|
 | 144 | //          std::cout << ", with default value " << currentOption->getDefaultValue();
 | 
|---|
 | 145 | //        else
 | 
|---|
 | 146 | //          std::cout << ", with no default value ";
 | 
|---|
 | 147 | //        std::cout << std::endl;
 | 
|---|
 | 148 | 
 | 
|---|
| [7912e9] | 149 |         AddOptionToParser(currentOption, (CmdParserLookup["options"]),
 | 
|---|
 | 150 |             (optioniter->first == *iter) ?
 | 
|---|
 | 151 |                 true : false);
 | 
|---|
| [5a8f38] | 152 | 
 | 
|---|
 | 153 |         AlreadyAddedOptionNames.insert(optioniter->first);
 | 
|---|
 | 154 |       } else {
 | 
|---|
 | 155 | //        std::cout << "Option " << currentOption->getName() << " already registered." << std::endl;
 | 
|---|
 | 156 |       }
 | 
|---|
| [e4afb4] | 157 |     }
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 |     if (!ActionAlreadyAdded_flag) {
 | 
|---|
 | 160 |       // add the action
 | 
|---|
| [ad7270] | 161 | //      std::cout << "Registering Action "
 | 
|---|
 | 162 | //          << currentAction->Traits.getName()
 | 
|---|
 | 163 | //          << " in menu " << currentAction->Traits.getMenuName()
 | 
|---|
 | 164 | //          << " with type '" << currentAction->Traits.getTypeName() << "' "
 | 
|---|
 | 165 | //          << " with description '" << currentAction->Traits.getDescription() << "' ";
 | 
|---|
 | 166 | //      if (currentAction->Traits.hasShortForm())
 | 
|---|
 | 167 | //        std::cout << ", with short form " << currentAction->Traits.getShortForm();
 | 
|---|
 | 168 | //      else
 | 
|---|
 | 169 | //        std::cout << ", with no short form ";
 | 
|---|
 | 170 | //      if (currentAction->Traits.hasDefaultValue())
 | 
|---|
 | 171 | //        std::cout << ", with default value " << currentAction->Traits.getDefaultValue();
 | 
|---|
 | 172 | //      else
 | 
|---|
 | 173 | //        std::cout << ", with no default value ";
 | 
|---|
 | 174 | //      std::cout  << std::endl;
 | 
|---|
| [e4afb4] | 175 | 
 | 
|---|
| [690741] | 176 |       ASSERT(CmdParserLookup.find(CurrentTrait.getMenuName()) != CmdParserLookup.end(),
 | 
|---|
| [e4afb4] | 177 |           "CommandLineParser: boost::program_options::options_description for this Action not present.");
 | 
|---|
| [7912e9] | 178 |       AddOptionToParser(
 | 
|---|
 | 179 |           dynamic_cast<const OptionTrait * const>(&CurrentTrait),
 | 
|---|
 | 180 |           (CmdParserLookup[CurrentTrait.getMenuName()]),
 | 
|---|
 | 181 |           false);
 | 
|---|
| [e4afb4] | 182 |     }
 | 
|---|
 | 183 |   }
 | 
|---|
 | 184 |   // note: positioning is not important on the command line
 | 
|---|
 | 185 | }
 | 
|---|
 | 186 | 
 | 
|---|
 | 187 | /** Adds an Action or Option to the CommandLineParser.
 | 
|---|
 | 188 |  * Note that Action is derived from Option(Trait)
 | 
|---|
 | 189 |  *
 | 
|---|
 | 190 |  * This ugly switch function is necessary because of the compile-time problem:
 | 
|---|
 | 191 |  * po::value<T> has to be instantiated at compile-time however we do know the type not until run-time.
 | 
|---|
 | 192 |  * Not even a templated function like po::value<T> getProgramOptionValuefromType() does help, specialized
 | 
|---|
 | 193 |  * to each available type, as the signatures of all the functions differ. Hence, they cannot not put into
 | 
|---|
 | 194 |  * one type_info -> po::value<T> map ...
 | 
|---|
 | 195 |  *
 | 
|---|
 | 196 |  * \param *currentOption pointer to Action/Option to add
 | 
|---|
 | 197 |  * \param *OptionList program_options list to add to
 | 
|---|
| [7912e9] | 198 |  * \param _DefaultAsImplicit whether to add a default value as default_value or
 | 
|---|
 | 199 |  *        as implicit_value (default = option token present or not, implicit =
 | 
|---|
 | 200 |           option token present but not necessarily followed by argument)
 | 
|---|
| [e4afb4] | 201 |  */
 | 
|---|
| [7912e9] | 202 | void CommandLineParser::AddOptionToParser(
 | 
|---|
 | 203 |     const OptionTrait * const currentOption, 
 | 
|---|
 | 204 |     po::options_description* OptionList,
 | 
|---|
 | 205 |     const bool _DefaultAsImplicit)
 | 
|---|
| [e4afb4] | 206 | {
 | 
|---|
 | 207 |   // check whether dynamic_cast in Init() suceeded
 | 
|---|
 | 208 |   ASSERT(currentOption != NULL, "CommandLineParser::AddOptionToParser() - currentOption is NULL!");
 | 
|---|
 | 209 |   // add other options
 | 
|---|
| [ec098d] | 210 | //  std::cout << "Adding Action " << currentOption->getName() << " with type "
 | 
|---|
 | 211 | //      << currentOption->getType()->name() << ", " << (currentOption->hasDefaultValue() ? "with" : "without")
 | 
|---|
 | 212 | //      << " default value, and KeyandShortform " << currentOption->getKeyAndShortForm()
 | 
|---|
 | 213 | //      << " to CommandLineParser." << std::endl;
 | 
|---|
| [e4afb4] | 214 |   switch(TypeToEnums.getEnumforType(currentOption->getType())) {
 | 
|---|
 | 215 |     default:
 | 
|---|
 | 216 |     case TypeEnumContainer::NoneType:
 | 
|---|
 | 217 |       OptionList->add_options()
 | 
|---|
 | 218 |         (currentOption->getKeyAndShortForm().c_str(), currentOption->getDescription().c_str())
 | 
|---|
 | 219 |         ;
 | 
|---|
 | 220 |       break;
 | 
|---|
 | 221 |     case TypeEnumContainer::BooleanType:
 | 
|---|
 | 222 |       OptionList->add_options()
 | 
|---|
 | 223 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 224 |             currentOption->hasDefaultValue() ?
 | 
|---|
| [7912e9] | 225 |                   (_DefaultAsImplicit ? 
 | 
|---|
 | 226 |                       po::value < bool >()->implicit_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 227 |                       po::value < bool >()->default_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str()))) :
 | 
|---|
| [e4afb4] | 228 |                   po::value < bool >(),
 | 
|---|
 | 229 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 230 |         ;
 | 
|---|
 | 231 |       break;
 | 
|---|
 | 232 |     case TypeEnumContainer::FileType:
 | 
|---|
 | 233 |       OptionList->add_options()
 | 
|---|
 | 234 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 235 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 236 | //                  po::value < boost::filesystem::path >()->default_value(boost::lexical_cast<boost::filesystem::path>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 237 |                   po::value < boost::filesystem::path >(),
 | 
|---|
 | 238 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 239 |         ;
 | 
|---|
 | 240 |       break;
 | 
|---|
 | 241 |     case TypeEnumContainer::ListOfFilesType:
 | 
|---|
 | 242 |       OptionList->add_options()
 | 
|---|
 | 243 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 244 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 245 | //                  po::value < std::vector<boost::filesystem::path> >()->default_value(boost::lexical_cast< std::vector<boost::filesystem::path> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 246 |                   po::value < std::vector<boost::filesystem::path> >()->multitoken(),
 | 
|---|
 | 247 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 248 |         ;
 | 
|---|
 | 249 |       break;
 | 
|---|
 | 250 |     case TypeEnumContainer::IntegerType:
 | 
|---|
 | 251 |       OptionList->add_options()
 | 
|---|
 | 252 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 253 |             currentOption->hasDefaultValue() ?
 | 
|---|
| [7912e9] | 254 |                   (_DefaultAsImplicit ? 
 | 
|---|
 | 255 |                       po::value < int >()->implicit_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 256 |                       po::value < int >()->default_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str()))) :
 | 
|---|
| [e4afb4] | 257 |                   po::value < int >(),
 | 
|---|
 | 258 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 259 |         ;
 | 
|---|
 | 260 |       break;
 | 
|---|
 | 261 |     case TypeEnumContainer::ListOfIntegersType:
 | 
|---|
 | 262 |       OptionList->add_options()
 | 
|---|
 | 263 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 264 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 265 | //                  po::value < std::vector<int> >()->default_value(boost::lexical_cast< std::vector<int> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 266 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
 | 267 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 268 |         ;
 | 
|---|
 | 269 |       break;
 | 
|---|
| [838cd0] | 270 |     case TypeEnumContainer::UnsignedIntegerType:
 | 
|---|
 | 271 |       OptionList->add_options()
 | 
|---|
 | 272 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 273 |             currentOption->hasDefaultValue() ?
 | 
|---|
| [7912e9] | 274 |                   (_DefaultAsImplicit ? 
 | 
|---|
 | 275 |                       po::value < unsigned int >()->implicit_value(boost::lexical_cast<unsigned int>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 276 |                       po::value < unsigned int >()->default_value(boost::lexical_cast<unsigned int>(currentOption->getDefaultValue().c_str()))) :
 | 
|---|
| [838cd0] | 277 |                   po::value < unsigned int >(),
 | 
|---|
 | 278 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 279 |         ;
 | 
|---|
 | 280 |       break;
 | 
|---|
| [12948c] | 281 |     case TypeEnumContainer::ListOfUnsignedIntegersType:
 | 
|---|
 | 282 |       OptionList->add_options()
 | 
|---|
 | 283 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 284 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 285 | //                  po::value < std::vector<unsigned int> >()->default_value(boost::lexical_cast< std::vector<unsigned int> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 286 |                   po::value < std::vector<unsigned int> >()->multitoken(),
 | 
|---|
 | 287 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 288 |         ;
 | 
|---|
 | 289 |       break;
 | 
|---|
| [e4afb4] | 290 |     case TypeEnumContainer::DoubleType:
 | 
|---|
 | 291 |       OptionList->add_options()
 | 
|---|
 | 292 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 293 |             currentOption->hasDefaultValue() ?
 | 
|---|
| [7912e9] | 294 |                   (_DefaultAsImplicit ? 
 | 
|---|
 | 295 |                       po::value < double >()->implicit_value(boost::lexical_cast<double>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 296 |                       po::value < double >()->default_value(boost::lexical_cast<double>(currentOption->getDefaultValue().c_str()))) :
 | 
|---|
| [e4afb4] | 297 |                   po::value < double >(),
 | 
|---|
 | 298 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 299 |         ;
 | 
|---|
 | 300 |       break;
 | 
|---|
 | 301 |     case TypeEnumContainer::ListOfDoublesType:
 | 
|---|
 | 302 |       OptionList->add_options()
 | 
|---|
 | 303 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 304 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 305 | //                  po::value < std::vector<double> >()->default_value(boost::lexical_cast< std::vector<double> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 306 |                   po::value < std::vector<double> >()->multitoken(),
 | 
|---|
 | 307 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 308 |         ;
 | 
|---|
 | 309 |       break;
 | 
|---|
 | 310 |     case TypeEnumContainer::StringType:
 | 
|---|
 | 311 |       OptionList->add_options()
 | 
|---|
 | 312 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 313 |             currentOption->hasDefaultValue() ?
 | 
|---|
| [7912e9] | 314 |                   (_DefaultAsImplicit ? 
 | 
|---|
 | 315 |                       po::value < std::string >()->implicit_value(currentOption->getDefaultValue()) :
 | 
|---|
 | 316 |                       po::value < std::string >()->default_value(currentOption->getDefaultValue())) :
 | 
|---|
| [e4afb4] | 317 |                   po::value < std::string >(),
 | 
|---|
 | 318 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 319 |         ;
 | 
|---|
 | 320 |       break;
 | 
|---|
 | 321 |     case TypeEnumContainer::ListOfStringsType:
 | 
|---|
 | 322 |       OptionList->add_options()
 | 
|---|
 | 323 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 324 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 325 | //                  po::value < std::vector<std::string> >()->default_value(boost::lexical_cast< std::vector<std::string> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 326 |                   po::value < std::vector<std::string> >()->multitoken(),
 | 
|---|
 | 327 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 328 |         ;
 | 
|---|
 | 329 |       break;
 | 
|---|
 | 330 |     case TypeEnumContainer::VectorType:
 | 
|---|
 | 331 |       OptionList->add_options()
 | 
|---|
 | 332 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 333 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 334 | //                  po::value < VectorValue >()->default_value(boost::lexical_cast<VectorValue>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 335 |                   po::value < VectorValue >(),
 | 
|---|
 | 336 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 337 |         ;
 | 
|---|
 | 338 |       break;
 | 
|---|
 | 339 |     case TypeEnumContainer::ListOfVectorsType:
 | 
|---|
 | 340 |       OptionList->add_options()
 | 
|---|
 | 341 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 342 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 343 | //                  po::value < std::vector<VectorValue> >()->default_value(boost::lexical_cast< std::vector<VectorValue> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 344 |                   po::value < std::vector<VectorValue> >()->multitoken(),
 | 
|---|
 | 345 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 346 |         ;
 | 
|---|
 | 347 |       break;
 | 
|---|
 | 348 |     case TypeEnumContainer::MoleculeType:
 | 
|---|
 | 349 |       OptionList->add_options()
 | 
|---|
 | 350 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 351 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 352 | //                  po::value < const molecule * >()->default_value(boost::lexical_cast<const molecule *>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 353 |                   po::value < int >(),
 | 
|---|
 | 354 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 355 |         ;
 | 
|---|
 | 356 |       break;
 | 
|---|
 | 357 |     case TypeEnumContainer::ListOfMoleculesType:
 | 
|---|
 | 358 |       OptionList->add_options()
 | 
|---|
 | 359 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 360 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 361 | //                  po::value < std::vector<const molecule *> >()->default_value(boost::lexical_cast< std::vector<const molecule *> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 362 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
 | 363 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 364 |         ;
 | 
|---|
 | 365 |       break;
 | 
|---|
 | 366 |     case TypeEnumContainer::AtomType:
 | 
|---|
 | 367 |       OptionList->add_options()
 | 
|---|
 | 368 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 369 |             currentOption->hasDefaultValue() ?
 | 
|---|
| [7912e9] | 370 |                   (_DefaultAsImplicit ? 
 | 
|---|
 | 371 |                       po::value < int >()->implicit_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 372 |                       po::value < int >()->default_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str()))) :
 | 
|---|
| [e4afb4] | 373 |                   po::value < int >(),
 | 
|---|
 | 374 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 375 |         ;
 | 
|---|
 | 376 |       break;
 | 
|---|
 | 377 |     case TypeEnumContainer::ListOfAtomsType:
 | 
|---|
 | 378 |       OptionList->add_options()
 | 
|---|
 | 379 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 380 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 381 | //                  po::value < std::vector<const atom *> >()->default_value(boost::lexical_cast< std::vector<const atom *> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 382 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
 | 383 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 384 |         ;
 | 
|---|
 | 385 |       break;
 | 
|---|
 | 386 |     case TypeEnumContainer::ElementType:
 | 
|---|
 | 387 |       OptionList->add_options()
 | 
|---|
 | 388 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 389 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 390 | //                  po::value < const element * >()->default_value(boost::lexical_cast<const element *>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 391 |                   po::value < int >(),
 | 
|---|
 | 392 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 393 |         ;
 | 
|---|
 | 394 |       break;
 | 
|---|
 | 395 |     case TypeEnumContainer::ListOfElementsType:
 | 
|---|
 | 396 |       OptionList->add_options()
 | 
|---|
 | 397 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 398 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 399 | //                  po::value < std::vector<const element *> >()->default_value(boost::lexical_cast< std::vector<const element *> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 400 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
 | 401 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 402 |         ;
 | 
|---|
 | 403 |       break;
 | 
|---|
| [7d9416] | 404 |     case TypeEnumContainer::RealSpaceMatrixType:
 | 
|---|
 | 405 |       OptionList->add_options()
 | 
|---|
 | 406 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 407 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 408 | //                  po::value < RealSpaceMatrixValue >()->default_value(boost::lexical_cast<BoxValue>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 409 |                   po::value < RealSpaceMatrixValue >(),
 | 
|---|
 | 410 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 411 |         ;
 | 
|---|
 | 412 |       break;
 | 
|---|
| [e4afb4] | 413 |   }
 | 
|---|
 | 414 | }
 | 
|---|
 | 415 | 
 | 
|---|
 | 416 | /** States whether there are command line arguments.
 | 
|---|
 | 417 |  * \return true - there are none, false - there is at least one command line argument
 | 
|---|
 | 418 |  */
 | 
|---|
 | 419 | bool CommandLineParser::isEmpty()
 | 
|---|
 | 420 | {
 | 
|---|
 | 421 |   return vm.empty();
 | 
|---|
 | 422 | }
 | 
|---|
 | 423 | 
 | 
|---|
 | 424 | /** Sets the options.
 | 
|---|
 | 425 |  * \param _argc arg count from main()
 | 
|---|
 | 426 |  * \param **_argv argument array from main()
 | 
|---|
 | 427 |  */
 | 
|---|
 | 428 | void CommandLineParser::setOptions(int _argc, char **_argv)
 | 
|---|
 | 429 | {
 | 
|---|
 | 430 |   argc = _argc;
 | 
|---|
 | 431 |   argv = _argv;
 | 
|---|
 | 432 |   config_file_options.add(options);
 | 
|---|
| [c6e5eb] | 433 |   // append all option_descriptions to both cmdline_options and visible
 | 
|---|
 | 434 |   for (CmdParserLookupMap::iterator iter = CmdParserLookup.begin();
 | 
|---|
 | 435 |       iter != CmdParserLookup.end();
 | 
|---|
 | 436 |       ++iter) {
 | 
|---|
 | 437 |     cmdline_options.add(*(iter->second));
 | 
|---|
 | 438 |     visible.add(*(iter->second));
 | 
|---|
 | 439 |   }
 | 
|---|
| [e4afb4] | 440 | }
 | 
|---|
 | 441 | 
 | 
|---|
 | 442 | /** Parses the command line arguments.
 | 
|---|
 | 443 |  * Calls program_options::store() and program_options::notify()
 | 
|---|
| [06b5df] | 444 |  *
 | 
|---|
 | 445 |  * @return true - all is ok, false - command-line options could not be parsed 
 | 
|---|
 | 446 |  *         correctly
 | 
|---|
| [e4afb4] | 447 |  */
 | 
|---|
| [06b5df] | 448 | bool CommandLineParser::Parse()
 | 
|---|
| [e4afb4] | 449 | {
 | 
|---|
| [06b5df] | 450 |   bool status = true;
 | 
|---|
| [11d433] | 451 |   try {
 | 
|---|
 | 452 |     po::store(po::command_line_parser(argc,argv).options(cmdline_options).run(), vm);
 | 
|---|
 | 453 |   } catch (std::exception &e) {
 | 
|---|
| [7912e9] | 454 |     std::cerr << "Something went wrong with parsing the command-line arguments: " 
 | 
|---|
 | 455 |         << e.what() << std::endl;
 | 
|---|
| [11d433] | 456 |     World::getInstance().setExitFlag(134);
 | 
|---|
 | 457 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
 | 458 |     // force action queue to stop thread
 | 
|---|
 | 459 |     ActionQueue::getInstance().stop();
 | 
|---|
| [601ef8] | 460 |     ActionQueue::getInstance().clearTempQueue();
 | 
|---|
| [11d433] | 461 | #endif
 | 
|---|
 | 462 |     ActionQueue::getInstance().clearQueue();
 | 
|---|
| [06b5df] | 463 |     status = false;
 | 
|---|
 | 464 |   }
 | 
|---|
 | 465 |   if (status) {
 | 
|---|
 | 466 |     std::ifstream input;
 | 
|---|
 | 467 |     input.open("example.cfg");
 | 
|---|
 | 468 |     if (!input.fail())
 | 
|---|
 | 469 |       po::store(po::parse_config_file(input, config_file_options), vm);
 | 
|---|
 | 470 |     input.close();
 | 
|---|
 | 471 |     po::notify(vm);
 | 
|---|
| [11d433] | 472 |   }
 | 
|---|
| [06b5df] | 473 |   return status;
 | 
|---|
| [e4afb4] | 474 | }
 | 
|---|
 | 475 | 
 | 
|---|
 | 476 | /** Scan the argument list for -a or --arguments and store their order for later use.
 | 
|---|
 | 477 |  */
 | 
|---|
 | 478 | void CommandLineParser::scanforSequenceOfArguments()
 | 
|---|
 | 479 | {
 | 
|---|
| [3a21a7] | 480 |   std::map <std::string, std::string> ShortFormToActionMap = getShortFormToActionMap();
 | 
|---|
| [47d041] | 481 |   LOG(0, "Scanning command line arguments and recognizing Actions.");
 | 
|---|
| [e4afb4] | 482 |   // go through all arguments
 | 
|---|
 | 483 |   for (int i=1;i<argc;i++) {
 | 
|---|
| [47d041] | 484 |     LOG(2, "Checking on " << argv[i]);
 | 
|---|
| [e4afb4] | 485 |     // check whether they
 | 
|---|
 | 486 |     if (argv[i][0] == '-') { // .. begin with -
 | 
|---|
| [47d041] | 487 |       LOG(2, "Possible argument: " << argv[i]);
 | 
|---|
| [e4afb4] | 488 |       if (argv[i][1] == '-') { // .. or --
 | 
|---|
| [47d041] | 489 |         LOG(1, "Putting " << argv[i] << " into the sequence.");
 | 
|---|
| [e4afb4] | 490 |         SequenceOfActions.push_back(&(argv[i][2]));
 | 
|---|
 | 491 |         //  .. and check that next letter is not numeric, if so insert
 | 
|---|
 | 492 |       } else if (((argv[i][1] < '0') || (argv[i][1] > '9')) && ((argv[i][1] != '.'))) {
 | 
|---|
| [3a21a7] | 493 |         std::map <std::string, std::string>::iterator iter = ShortFormToActionMap.find(&(argv[i][1]));
 | 
|---|
| [e4afb4] | 494 |         if (iter != ShortFormToActionMap.end()) {
 | 
|---|
| [47d041] | 495 |           LOG(1, "Putting " << iter->second << " for " << iter->first << " into the sequence.");
 | 
|---|
| [e4afb4] | 496 |           SequenceOfActions.push_back(iter->second);
 | 
|---|
 | 497 |         }
 | 
|---|
 | 498 |       }
 | 
|---|
 | 499 |     }
 | 
|---|
 | 500 |   }
 | 
|---|
 | 501 | }
 | 
|---|
 | 502 | 
 | 
|---|
 | 503 | /** Makes the Parser parse the command line options with current known options.
 | 
|---|
 | 504 |  * \param _argc arg count from main()
 | 
|---|
 | 505 |  * \param **_argv argument array from main()
 | 
|---|
 | 506 |  */
 | 
|---|
 | 507 | void CommandLineParser::Run(int _argc, char **_argv)
 | 
|---|
 | 508 | {
 | 
|---|
 | 509 |   setOptions(_argc,_argv);
 | 
|---|
| [06b5df] | 510 |   const bool status = Parse();
 | 
|---|
 | 511 |   if (status)
 | 
|---|
 | 512 |     scanforSequenceOfArguments();
 | 
|---|
| [e4afb4] | 513 | }
 | 
|---|
 | 514 | 
 | 
|---|
 | 515 | /** Go through all Actions and create a map from short form to their token.
 | 
|---|
 | 516 |  * \return map from Action's ShortForm to token.
 | 
|---|
 | 517 |  */
 | 
|---|
| [2a0a9e3] | 518 | std::map <std::string, std::string> CommandLineParser::getShortFormToActionMap() const
 | 
|---|
| [e4afb4] | 519 | {
 | 
|---|
| [3a21a7] | 520 |   std::map <std::string, std::string> result;
 | 
|---|
| [e4afb4] | 521 | 
 | 
|---|
| [690741] | 522 |   ActionQueue &AQ = ActionQueue::getInstance();
 | 
|---|
 | 523 |   ActionQueue::ActionTokens_t tokens = AQ.getListOfActions();
 | 
|---|
 | 524 |   for (ActionQueue::ActionTokens_t::const_iterator iter = tokens.begin();
 | 
|---|
 | 525 |       iter != tokens.end(); ++iter) {
 | 
|---|
 | 526 |     const ActionTrait &CurrentTrait = AQ.getActionsTrait(*iter);
 | 
|---|
 | 527 |     if (CurrentTrait.hasShortForm()) {
 | 
|---|
 | 528 |       ASSERT(result.find(CurrentTrait.getShortForm()) == result.end(),
 | 
|---|
 | 529 |           "Short form "+toString(CurrentTrait.getShortForm())+
 | 
|---|
 | 530 |           " for action "+toString(*iter)+" already present from "+
 | 
|---|
 | 531 |           std::string(result[CurrentTrait.getShortForm()])+"!");
 | 
|---|
 | 532 |       result[CurrentTrait.getShortForm()] = *iter;
 | 
|---|
| [e4afb4] | 533 |     }
 | 
|---|
| [690741] | 534 |   }
 | 
|---|
| [e4afb4] | 535 | 
 | 
|---|
 | 536 |   return result;
 | 
|---|
 | 537 | }
 | 
|---|
 | 538 | 
 | 
|---|
 | 539 | CONSTRUCT_SINGLETON(CommandLineParser)
 | 
|---|