| [e4afb4] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | /*
 | 
|---|
 | 9 |  * CommandLineParser.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: May 8, 2010
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [e4afb4] | 21 | 
 | 
|---|
 | 22 | #include <boost/filesystem.hpp>
 | 
|---|
 | 23 | #include <boost/program_options.hpp>
 | 
|---|
 | 24 | #include <fstream>
 | 
|---|
 | 25 | #include <iostream>
 | 
|---|
 | 26 | #include <map>
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | #include "Actions/Action.hpp"
 | 
|---|
 | 29 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
 | 30 | #include "Actions/ActionTraits.hpp"
 | 
|---|
 | 31 | #include "Actions/OptionRegistry.hpp"
 | 
|---|
 | 32 | #include "Actions/OptionTrait.hpp"
 | 
|---|
 | 33 | #include "Actions/Values.hpp"
 | 
|---|
| [ad011c] | 34 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 35 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [e4afb4] | 36 | #include "CommandLineParser.hpp"
 | 
|---|
| [3a21a7] | 37 | #include "CommandLineParser_validate.hpp"
 | 
|---|
| [e4afb4] | 38 | 
 | 
|---|
| [ad011c] | 39 | #include "CodePatterns/Singleton_impl.hpp"
 | 
|---|
| [e4afb4] | 40 | 
 | 
|---|
 | 41 | class element;
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 | /** Constructor of class CommandLineParser.
 | 
|---|
 | 44 |  *
 | 
|---|
 | 45 |  */
 | 
|---|
 | 46 | CommandLineParser::CommandLineParser() :
 | 
|---|
 | 47 |   analysis("Analysis options"),
 | 
|---|
 | 48 |   atom("Atom options"),
 | 
|---|
 | 49 |   command("Command options"),
 | 
|---|
 | 50 |   fragmentation("Fragmentation options"),
 | 
|---|
| [d09093] | 51 |   graph("Graph options"),
 | 
|---|
| [e4afb4] | 52 |   molecule("Molecule options"),
 | 
|---|
| [d09093] | 53 |   options("Secondary options"),
 | 
|---|
| [e4afb4] | 54 |   parser("Parser options"),
 | 
|---|
 | 55 |   selection("Selection options"),
 | 
|---|
 | 56 |   tesselation("Tesselation options"),
 | 
|---|
| [d09093] | 57 |   world("World options")
 | 
|---|
| [e4afb4] | 58 | {
 | 
|---|
 | 59 |   // put all options lists into a lookup
 | 
|---|
 | 60 |   CmdParserLookup["analysis"] = &analysis;
 | 
|---|
 | 61 |   CmdParserLookup["atom"] = &atom;
 | 
|---|
 | 62 |   CmdParserLookup["command"] = &command;
 | 
|---|
 | 63 |   CmdParserLookup["edit"] = &edit;
 | 
|---|
 | 64 |   CmdParserLookup["fragmentation"] = &fragmentation;
 | 
|---|
| [d09093] | 65 |   CmdParserLookup["graph"] = &graph;
 | 
|---|
 | 66 |   CmdParserLookup["options"] = &options;
 | 
|---|
| [e4afb4] | 67 |   CmdParserLookup["molecule"] = &molecule;
 | 
|---|
 | 68 |   CmdParserLookup["parser"] = &parser;
 | 
|---|
 | 69 |   CmdParserLookup["selection"] = &selection;
 | 
|---|
 | 70 |   CmdParserLookup["tesselation"] = &tesselation;
 | 
|---|
 | 71 |   CmdParserLookup["world"] = &world;
 | 
|---|
 | 72 | }
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 | /** Destructor of class CommandLineParser.
 | 
|---|
 | 75 |  *
 | 
|---|
 | 76 |  */
 | 
|---|
 | 77 | CommandLineParser::~CommandLineParser()
 | 
|---|
 | 78 | {}
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | /** Initializes command arguments to accept.
 | 
|---|
 | 81 |  * Goes through ActionRegistry and puts all actions therein into the map.
 | 
|---|
 | 82 |  */
 | 
|---|
 | 83 | void CommandLineParser::InitializeCommandArguments()
 | 
|---|
 | 84 | {
 | 
|---|
 | 85 |   ActionRegistry &AR = ActionRegistry::getInstance();
 | 
|---|
 | 86 |   bool ActionAlreadyAdded_flag = false;
 | 
|---|
 | 87 |   for (ActionRegistry::const_iterator actioniter = AR.getBeginIter(); actioniter != AR.getEndIter(); ++actioniter) {
 | 
|---|
 | 88 |     ActionAlreadyAdded_flag = false;
 | 
|---|
 | 89 |     Action* const currentAction = actioniter->second;
 | 
|---|
 | 90 |     //std::cout << "Current Action to initialize is: " << actioniter->first << std::endl;
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |     for (ActionTraits::options_const_iterator optioniter = currentAction->Traits.getBeginIter();
 | 
|---|
 | 93 |         optioniter != currentAction->Traits.getEndIter();
 | 
|---|
 | 94 |         ++optioniter) {
 | 
|---|
 | 95 |       if (optioniter->first == actioniter->first)
 | 
|---|
 | 96 |         ActionAlreadyAdded_flag = true;
 | 
|---|
 | 97 |       ASSERT( OptionRegistry::getInstance().isOptionPresentByName(optioniter->first),
 | 
|---|
 | 98 |           "CommandLineParser::Init() - Option not present in OptionRegistry." );
 | 
|---|
 | 99 |       const OptionTrait* const currentOption = OptionRegistry::getInstance().getOptionByName(optioniter->first);
 | 
|---|
 | 100 |       // add the option
 | 
|---|
| [ad7270] | 101 | //      std::cout << "Registering Option "
 | 
|---|
 | 102 | //          << currentOption->getName()
 | 
|---|
 | 103 | //          << " with type '" << currentOption->getTypeName() << "' "
 | 
|---|
 | 104 | //          << " with description '" << currentOption->getDescription() << "' ";
 | 
|---|
 | 105 | //      if (currentOption->hasShortForm())
 | 
|---|
 | 106 | //        std::cout << ", with short form " << currentOption->getShortForm();
 | 
|---|
 | 107 | //      else
 | 
|---|
 | 108 | //        std::cout << ", with no short form ";
 | 
|---|
 | 109 | //      if (currentOption->hasDefaultValue())
 | 
|---|
 | 110 | //        std::cout << ", with default value " << currentOption->getDefaultValue();
 | 
|---|
 | 111 | //      else
 | 
|---|
 | 112 | //        std::cout << ", with no default value ";
 | 
|---|
 | 113 | //      std::cout << std::endl;
 | 
|---|
| [e4afb4] | 114 | 
 | 
|---|
 | 115 |       AddOptionToParser(currentOption, (CmdParserLookup["options"]));
 | 
|---|
 | 116 |     }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 |     if (!ActionAlreadyAdded_flag) {
 | 
|---|
 | 119 |       // add the action
 | 
|---|
| [ad7270] | 120 | //      std::cout << "Registering Action "
 | 
|---|
 | 121 | //          << currentAction->Traits.getName()
 | 
|---|
 | 122 | //          << " in menu " << currentAction->Traits.getMenuName()
 | 
|---|
 | 123 | //          << " with type '" << currentAction->Traits.getTypeName() << "' "
 | 
|---|
 | 124 | //          << " with description '" << currentAction->Traits.getDescription() << "' ";
 | 
|---|
 | 125 | //      if (currentAction->Traits.hasShortForm())
 | 
|---|
 | 126 | //        std::cout << ", with short form " << currentAction->Traits.getShortForm();
 | 
|---|
 | 127 | //      else
 | 
|---|
 | 128 | //        std::cout << ", with no short form ";
 | 
|---|
 | 129 | //      if (currentAction->Traits.hasDefaultValue())
 | 
|---|
 | 130 | //        std::cout << ", with default value " << currentAction->Traits.getDefaultValue();
 | 
|---|
 | 131 | //      else
 | 
|---|
 | 132 | //        std::cout << ", with no default value ";
 | 
|---|
 | 133 | //      std::cout  << std::endl;
 | 
|---|
| [e4afb4] | 134 | 
 | 
|---|
 | 135 |       ASSERT(CmdParserLookup.find(currentAction->Traits.getMenuName()) != CmdParserLookup.end(),
 | 
|---|
 | 136 |           "CommandLineParser: boost::program_options::options_description for this Action not present.");
 | 
|---|
 | 137 |       AddOptionToParser(dynamic_cast<const OptionTrait * const>(&(currentAction->Traits)),(CmdParserLookup[currentAction->Traits.getMenuName()]));
 | 
|---|
 | 138 |     }
 | 
|---|
 | 139 |   }
 | 
|---|
 | 140 |   // note: positioning is not important on the command line
 | 
|---|
 | 141 | }
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 | /** Adds an Action or Option to the CommandLineParser.
 | 
|---|
 | 144 |  * Note that Action is derived from Option(Trait)
 | 
|---|
 | 145 |  *
 | 
|---|
 | 146 |  * This ugly switch function is necessary because of the compile-time problem:
 | 
|---|
 | 147 |  * po::value<T> has to be instantiated at compile-time however we do know the type not until run-time.
 | 
|---|
 | 148 |  * Not even a templated function like po::value<T> getProgramOptionValuefromType() does help, specialized
 | 
|---|
 | 149 |  * to each available type, as the signatures of all the functions differ. Hence, they cannot not put into
 | 
|---|
 | 150 |  * one type_info -> po::value<T> map ...
 | 
|---|
 | 151 |  *
 | 
|---|
 | 152 |  * \param *currentOption pointer to Action/Option to add
 | 
|---|
 | 153 |  * \param *OptionList program_options list to add to
 | 
|---|
 | 154 |  */
 | 
|---|
 | 155 | void CommandLineParser::AddOptionToParser(const OptionTrait * const currentOption, po::options_description* OptionList)
 | 
|---|
 | 156 | {
 | 
|---|
 | 157 |   // check whether dynamic_cast in Init() suceeded
 | 
|---|
 | 158 |   ASSERT(currentOption != NULL, "CommandLineParser::AddOptionToParser() - currentOption is NULL!");
 | 
|---|
 | 159 |   // add other options
 | 
|---|
| [ec098d] | 160 | //  std::cout << "Adding Action " << currentOption->getName() << " with type "
 | 
|---|
 | 161 | //      << currentOption->getType()->name() << ", " << (currentOption->hasDefaultValue() ? "with" : "without")
 | 
|---|
 | 162 | //      << " default value, and KeyandShortform " << currentOption->getKeyAndShortForm()
 | 
|---|
 | 163 | //      << " to CommandLineParser." << std::endl;
 | 
|---|
| [e4afb4] | 164 |   switch(TypeToEnums.getEnumforType(currentOption->getType())) {
 | 
|---|
 | 165 |     default:
 | 
|---|
 | 166 |     case TypeEnumContainer::NoneType:
 | 
|---|
 | 167 |       OptionList->add_options()
 | 
|---|
 | 168 |         (currentOption->getKeyAndShortForm().c_str(), currentOption->getDescription().c_str())
 | 
|---|
 | 169 |         ;
 | 
|---|
 | 170 |       break;
 | 
|---|
 | 171 |     case TypeEnumContainer::BooleanType:
 | 
|---|
 | 172 |       OptionList->add_options()
 | 
|---|
 | 173 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 174 |             currentOption->hasDefaultValue() ?
 | 
|---|
 | 175 |                   po::value < bool >()->default_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 176 |                   po::value < bool >(),
 | 
|---|
 | 177 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 178 |         ;
 | 
|---|
 | 179 |       break;
 | 
|---|
 | 180 |     case TypeEnumContainer::BoxType:
 | 
|---|
 | 181 |       OptionList->add_options()
 | 
|---|
 | 182 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 183 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 184 | //                  po::value < BoxValue >()->default_value(boost::lexical_cast<BoxValue>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 185 |                   po::value < BoxValue >(),
 | 
|---|
 | 186 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 187 |         ;
 | 
|---|
 | 188 |       break;
 | 
|---|
 | 189 |     case TypeEnumContainer::FileType:
 | 
|---|
 | 190 |       OptionList->add_options()
 | 
|---|
 | 191 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 192 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 193 | //                  po::value < boost::filesystem::path >()->default_value(boost::lexical_cast<boost::filesystem::path>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 194 |                   po::value < boost::filesystem::path >(),
 | 
|---|
 | 195 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 196 |         ;
 | 
|---|
 | 197 |       break;
 | 
|---|
 | 198 |     case TypeEnumContainer::ListOfFilesType:
 | 
|---|
 | 199 |       OptionList->add_options()
 | 
|---|
 | 200 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 201 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 202 | //                  po::value < std::vector<boost::filesystem::path> >()->default_value(boost::lexical_cast< std::vector<boost::filesystem::path> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 203 |                   po::value < std::vector<boost::filesystem::path> >()->multitoken(),
 | 
|---|
 | 204 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 205 |         ;
 | 
|---|
 | 206 |       break;
 | 
|---|
 | 207 |     case TypeEnumContainer::IntegerType:
 | 
|---|
 | 208 |       OptionList->add_options()
 | 
|---|
 | 209 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 210 |             currentOption->hasDefaultValue() ?
 | 
|---|
 | 211 |                   po::value < int >()->default_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 212 |                   po::value < int >(),
 | 
|---|
 | 213 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 214 |         ;
 | 
|---|
 | 215 |       break;
 | 
|---|
 | 216 |     case TypeEnumContainer::ListOfIntegersType:
 | 
|---|
 | 217 |       OptionList->add_options()
 | 
|---|
 | 218 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 219 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 220 | //                  po::value < std::vector<int> >()->default_value(boost::lexical_cast< std::vector<int> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 221 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
 | 222 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 223 |         ;
 | 
|---|
 | 224 |       break;
 | 
|---|
 | 225 |     case TypeEnumContainer::DoubleType:
 | 
|---|
 | 226 |       OptionList->add_options()
 | 
|---|
 | 227 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 228 |             currentOption->hasDefaultValue() ?
 | 
|---|
 | 229 |                   po::value < double >()->default_value(boost::lexical_cast<double>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 230 |                   po::value < double >(),
 | 
|---|
 | 231 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 232 |         ;
 | 
|---|
 | 233 |       break;
 | 
|---|
 | 234 |     case TypeEnumContainer::ListOfDoublesType:
 | 
|---|
 | 235 |       OptionList->add_options()
 | 
|---|
 | 236 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 237 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 238 | //                  po::value < std::vector<double> >()->default_value(boost::lexical_cast< std::vector<double> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 239 |                   po::value < std::vector<double> >()->multitoken(),
 | 
|---|
 | 240 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 241 |         ;
 | 
|---|
 | 242 |       break;
 | 
|---|
 | 243 |     case TypeEnumContainer::StringType:
 | 
|---|
 | 244 |       OptionList->add_options()
 | 
|---|
 | 245 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 246 |             currentOption->hasDefaultValue() ?
 | 
|---|
 | 247 |                   po::value < std::string >()->default_value(currentOption->getDefaultValue()) :
 | 
|---|
 | 248 |                   po::value < std::string >(),
 | 
|---|
 | 249 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 250 |         ;
 | 
|---|
 | 251 |       break;
 | 
|---|
 | 252 |     case TypeEnumContainer::ListOfStringsType:
 | 
|---|
 | 253 |       OptionList->add_options()
 | 
|---|
 | 254 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 255 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 256 | //                  po::value < std::vector<std::string> >()->default_value(boost::lexical_cast< std::vector<std::string> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 257 |                   po::value < std::vector<std::string> >()->multitoken(),
 | 
|---|
 | 258 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 259 |         ;
 | 
|---|
 | 260 |       break;
 | 
|---|
 | 261 |     case TypeEnumContainer::VectorType:
 | 
|---|
 | 262 |       OptionList->add_options()
 | 
|---|
 | 263 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 264 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 265 | //                  po::value < VectorValue >()->default_value(boost::lexical_cast<VectorValue>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 266 |                   po::value < VectorValue >(),
 | 
|---|
 | 267 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 268 |         ;
 | 
|---|
 | 269 |       break;
 | 
|---|
 | 270 |     case TypeEnumContainer::ListOfVectorsType:
 | 
|---|
 | 271 |       OptionList->add_options()
 | 
|---|
 | 272 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 273 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 274 | //                  po::value < std::vector<VectorValue> >()->default_value(boost::lexical_cast< std::vector<VectorValue> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 275 |                   po::value < std::vector<VectorValue> >()->multitoken(),
 | 
|---|
 | 276 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 277 |         ;
 | 
|---|
 | 278 |       break;
 | 
|---|
 | 279 |     case TypeEnumContainer::MoleculeType:
 | 
|---|
 | 280 |       OptionList->add_options()
 | 
|---|
 | 281 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 282 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 283 | //                  po::value < const molecule * >()->default_value(boost::lexical_cast<const molecule *>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 284 |                   po::value < int >(),
 | 
|---|
 | 285 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 286 |         ;
 | 
|---|
 | 287 |       break;
 | 
|---|
 | 288 |     case TypeEnumContainer::ListOfMoleculesType:
 | 
|---|
 | 289 |       OptionList->add_options()
 | 
|---|
 | 290 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 291 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 292 | //                  po::value < std::vector<const molecule *> >()->default_value(boost::lexical_cast< std::vector<const molecule *> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 293 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
 | 294 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 295 |         ;
 | 
|---|
 | 296 |       break;
 | 
|---|
 | 297 |     case TypeEnumContainer::AtomType:
 | 
|---|
 | 298 |       OptionList->add_options()
 | 
|---|
 | 299 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 300 |             currentOption->hasDefaultValue() ?
 | 
|---|
 | 301 |                   po::value < int >()->default_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 302 |                   po::value < int >(),
 | 
|---|
 | 303 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 304 |         ;
 | 
|---|
 | 305 |       break;
 | 
|---|
 | 306 |     case TypeEnumContainer::ListOfAtomsType:
 | 
|---|
 | 307 |       OptionList->add_options()
 | 
|---|
 | 308 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 309 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 310 | //                  po::value < std::vector<const atom *> >()->default_value(boost::lexical_cast< std::vector<const atom *> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 311 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
 | 312 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 313 |         ;
 | 
|---|
 | 314 |       break;
 | 
|---|
 | 315 |     case TypeEnumContainer::ElementType:
 | 
|---|
 | 316 |       OptionList->add_options()
 | 
|---|
 | 317 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 318 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 319 | //                  po::value < const element * >()->default_value(boost::lexical_cast<const element *>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 320 |                   po::value < int >(),
 | 
|---|
 | 321 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 322 |         ;
 | 
|---|
 | 323 |       break;
 | 
|---|
 | 324 |     case TypeEnumContainer::ListOfElementsType:
 | 
|---|
 | 325 |       OptionList->add_options()
 | 
|---|
 | 326 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 327 | //            currentOption->hasDefaultValue() ?
 | 
|---|
 | 328 | //                  po::value < std::vector<const element *> >()->default_value(boost::lexical_cast< std::vector<const element *> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 329 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
 | 330 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 331 |         ;
 | 
|---|
 | 332 |       break;
 | 
|---|
| [0275ad] | 333 |     case TypeEnumContainer::RandomNumberDistribution_ParametersType:
 | 
|---|
 | 334 |       OptionList->add_options()
 | 
|---|
 | 335 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
 | 336 |             currentOption->hasDefaultValue() ?
 | 
|---|
 | 337 |                   po::value < std::string >()->default_value(boost::lexical_cast< std::string >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
 | 338 |                   po::value < std::string >(),
 | 
|---|
 | 339 |                   currentOption->getDescription().c_str())
 | 
|---|
 | 340 |         ;
 | 
|---|
 | 341 |       break;
 | 
|---|
| [e4afb4] | 342 |   }
 | 
|---|
 | 343 | }
 | 
|---|
 | 344 | 
 | 
|---|
 | 345 | /** States whether there are command line arguments.
 | 
|---|
 | 346 |  * \return true - there are none, false - there is at least one command line argument
 | 
|---|
 | 347 |  */
 | 
|---|
 | 348 | bool CommandLineParser::isEmpty()
 | 
|---|
 | 349 | {
 | 
|---|
 | 350 |   return vm.empty();
 | 
|---|
 | 351 | }
 | 
|---|
 | 352 | 
 | 
|---|
 | 353 | /** Sets the options.
 | 
|---|
 | 354 |  * \param _argc arg count from main()
 | 
|---|
 | 355 |  * \param **_argv argument array from main()
 | 
|---|
 | 356 |  */
 | 
|---|
 | 357 | void CommandLineParser::setOptions(int _argc, char **_argv)
 | 
|---|
 | 358 | {
 | 
|---|
 | 359 |   argc = _argc;
 | 
|---|
 | 360 |   argv = _argv;
 | 
|---|
 | 361 |   config_file_options.add(options);
 | 
|---|
| [c6e5eb] | 362 |   // append all option_descriptions to both cmdline_options and visible
 | 
|---|
 | 363 |   for (CmdParserLookupMap::iterator iter = CmdParserLookup.begin();
 | 
|---|
 | 364 |       iter != CmdParserLookup.end();
 | 
|---|
 | 365 |       ++iter) {
 | 
|---|
 | 366 |     cmdline_options.add(*(iter->second));
 | 
|---|
 | 367 |     visible.add(*(iter->second));
 | 
|---|
 | 368 |   }
 | 
|---|
| [e4afb4] | 369 | }
 | 
|---|
 | 370 | 
 | 
|---|
 | 371 | /** Parses the command line arguments.
 | 
|---|
 | 372 |  * Calls program_options::store() and program_options::notify()
 | 
|---|
 | 373 |  */
 | 
|---|
 | 374 | void CommandLineParser::Parse()
 | 
|---|
 | 375 | {
 | 
|---|
 | 376 |   po::store(po::command_line_parser(argc,argv).options(cmdline_options).run(), vm);
 | 
|---|
| [3a21a7] | 377 |   std::ifstream input;
 | 
|---|
| [e4afb4] | 378 |   input.open("example.cfg");
 | 
|---|
 | 379 |   if (!input.fail())
 | 
|---|
 | 380 |     po::store(po::parse_config_file(input, config_file_options), vm);
 | 
|---|
 | 381 |   input.close();
 | 
|---|
 | 382 |   po::notify(vm);
 | 
|---|
 | 383 | }
 | 
|---|
 | 384 | 
 | 
|---|
 | 385 | /** Scan the argument list for -a or --arguments and store their order for later use.
 | 
|---|
 | 386 |  */
 | 
|---|
 | 387 | void CommandLineParser::scanforSequenceOfArguments()
 | 
|---|
 | 388 | {
 | 
|---|
| [3a21a7] | 389 |   std::map <std::string, std::string> ShortFormToActionMap = getShortFormToActionMap();
 | 
|---|
| [ad7270] | 390 |   DoLog(0) && (Log() << Verbose(0) << "Scanning command line arguments and recognizing Actions." << std::endl);
 | 
|---|
| [e4afb4] | 391 |   // go through all arguments
 | 
|---|
 | 392 |   for (int i=1;i<argc;i++) {
 | 
|---|
| [ad7270] | 393 |     DoLog(2) && (Log() << Verbose(2) << "Checking on " << argv[i] << std::endl);
 | 
|---|
| [e4afb4] | 394 |     // check whether they
 | 
|---|
 | 395 |     if (argv[i][0] == '-') { // .. begin with -
 | 
|---|
| [ad7270] | 396 |       DoLog(2) && (Log() << Verbose(2) << "Possible argument: " << argv[i] << endl);
 | 
|---|
| [e4afb4] | 397 |       if (argv[i][1] == '-') { // .. or --
 | 
|---|
| [ad7270] | 398 |         DoLog(1) && (Log() << Verbose(1) << "Putting " << argv[i] << " into the sequence." << endl);
 | 
|---|
| [e4afb4] | 399 |         SequenceOfActions.push_back(&(argv[i][2]));
 | 
|---|
 | 400 |         //  .. and check that next letter is not numeric, if so insert
 | 
|---|
 | 401 |       } else if (((argv[i][1] < '0') || (argv[i][1] > '9')) && ((argv[i][1] != '.'))) {
 | 
|---|
| [3a21a7] | 402 |         std::map <std::string, std::string>::iterator iter = ShortFormToActionMap.find(&(argv[i][1]));
 | 
|---|
| [e4afb4] | 403 |         if (iter != ShortFormToActionMap.end()) {
 | 
|---|
| [ad7270] | 404 |           DoLog(1) && (Log() << Verbose(1) << "Putting " << iter->second << " for " << iter->first << " into the sequence." << endl);
 | 
|---|
| [e4afb4] | 405 |           SequenceOfActions.push_back(iter->second);
 | 
|---|
 | 406 |         }
 | 
|---|
 | 407 |       }
 | 
|---|
 | 408 |     }
 | 
|---|
 | 409 |   }
 | 
|---|
 | 410 | }
 | 
|---|
 | 411 | 
 | 
|---|
 | 412 | /** Makes the Parser parse the command line options with current known options.
 | 
|---|
 | 413 |  * \param _argc arg count from main()
 | 
|---|
 | 414 |  * \param **_argv argument array from main()
 | 
|---|
 | 415 |  */
 | 
|---|
 | 416 | void CommandLineParser::Run(int _argc, char **_argv)
 | 
|---|
 | 417 | {
 | 
|---|
 | 418 |   setOptions(_argc,_argv);
 | 
|---|
 | 419 |   Parse();
 | 
|---|
 | 420 |   scanforSequenceOfArguments();
 | 
|---|
 | 421 | }
 | 
|---|
 | 422 | 
 | 
|---|
 | 423 | /** Go through all Actions and create a map from short form to their token.
 | 
|---|
 | 424 |  * \return map from Action's ShortForm to token.
 | 
|---|
 | 425 |  */
 | 
|---|
| [2a0a9e3] | 426 | std::map <std::string, std::string> CommandLineParser::getShortFormToActionMap() const
 | 
|---|
| [e4afb4] | 427 | {
 | 
|---|
| [3a21a7] | 428 |   std::map <std::string, std::string> result;
 | 
|---|
| [e4afb4] | 429 | 
 | 
|---|
 | 430 |   ActionRegistry &AR = ActionRegistry::getInstance();
 | 
|---|
 | 431 |   for (ActionRegistry::const_iterator iter = AR.getBeginIter(); iter != AR.getEndIter(); ++iter)
 | 
|---|
 | 432 |     if ((iter->second)->Traits.hasShortForm()) {
 | 
|---|
 | 433 |       ASSERT(result.find((iter->second)->Traits.getShortForm()) == result.end(),
 | 
|---|
| [cfc53b] | 434 |           "Short form "+toString((iter->second)->Traits.getShortForm())+
 | 
|---|
 | 435 |           " for action "+toString(iter->first)+" already present from "+
 | 
|---|
| [e4afb4] | 436 |           std::string(result[(iter->second)->Traits.getShortForm()])+"!");
 | 
|---|
 | 437 |       result[(iter->second)->Traits.getShortForm()] = (iter->second)->getName();
 | 
|---|
 | 438 |     }
 | 
|---|
 | 439 | 
 | 
|---|
 | 440 |   return result;
 | 
|---|
 | 441 | }
 | 
|---|
 | 442 | 
 | 
|---|
 | 443 | CONSTRUCT_SINGLETON(CommandLineParser)
 | 
|---|