| 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 | 
 | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 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"
 | 
|---|
| 34 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 35 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 36 | #include "CommandLineParser.hpp"
 | 
|---|
| 37 | #include "CommandLineParser_validate.hpp"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include "CodePatterns/Singleton_impl.hpp"
 | 
|---|
| 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"),
 | 
|---|
| 51 |   graph("Graph options"),
 | 
|---|
| 52 |   molecule("Molecule options"),
 | 
|---|
| 53 |   options("Secondary options"),
 | 
|---|
| 54 |   parser("Parser options"),
 | 
|---|
| 55 |   selection("Selection options"),
 | 
|---|
| 56 |   tesselation("Tesselation options"),
 | 
|---|
| 57 |   world("World options")
 | 
|---|
| 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;
 | 
|---|
| 65 |   CmdParserLookup["graph"] = &graph;
 | 
|---|
| 66 |   CmdParserLookup["options"] = &options;
 | 
|---|
| 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
 | 
|---|
| 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;
 | 
|---|
| 114 | 
 | 
|---|
| 115 |       AddOptionToParser(currentOption, (CmdParserLookup["options"]));
 | 
|---|
| 116 |     }
 | 
|---|
| 117 | 
 | 
|---|
| 118 |     if (!ActionAlreadyAdded_flag) {
 | 
|---|
| 119 |       // add the action
 | 
|---|
| 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;
 | 
|---|
| 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
 | 
|---|
| 160 |   //std::cout << "Adding Action " << currentOption->getName() << " with type " << currentOption->getType()->name() << " and KeyandShortform " << currentOption->getKeyAndShortForm() << " to CommandLineParser." << std::endl;
 | 
|---|
| 161 |   switch(TypeToEnums.getEnumforType(currentOption->getType())) {
 | 
|---|
| 162 |     default:
 | 
|---|
| 163 |     case TypeEnumContainer::NoneType:
 | 
|---|
| 164 |       OptionList->add_options()
 | 
|---|
| 165 |         (currentOption->getKeyAndShortForm().c_str(), currentOption->getDescription().c_str())
 | 
|---|
| 166 |         ;
 | 
|---|
| 167 |       break;
 | 
|---|
| 168 |     case TypeEnumContainer::BooleanType:
 | 
|---|
| 169 |       OptionList->add_options()
 | 
|---|
| 170 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 171 |             currentOption->hasDefaultValue() ?
 | 
|---|
| 172 |                   po::value < bool >()->default_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 173 |                   po::value < bool >(),
 | 
|---|
| 174 |                   currentOption->getDescription().c_str())
 | 
|---|
| 175 |         ;
 | 
|---|
| 176 |       break;
 | 
|---|
| 177 |     case TypeEnumContainer::BoxType:
 | 
|---|
| 178 |       OptionList->add_options()
 | 
|---|
| 179 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 180 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 181 | //                  po::value < BoxValue >()->default_value(boost::lexical_cast<BoxValue>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 182 |                   po::value < BoxValue >(),
 | 
|---|
| 183 |                   currentOption->getDescription().c_str())
 | 
|---|
| 184 |         ;
 | 
|---|
| 185 |       break;
 | 
|---|
| 186 |     case TypeEnumContainer::FileType:
 | 
|---|
| 187 |       OptionList->add_options()
 | 
|---|
| 188 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 189 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 190 | //                  po::value < boost::filesystem::path >()->default_value(boost::lexical_cast<boost::filesystem::path>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 191 |                   po::value < boost::filesystem::path >(),
 | 
|---|
| 192 |                   currentOption->getDescription().c_str())
 | 
|---|
| 193 |         ;
 | 
|---|
| 194 |       break;
 | 
|---|
| 195 |     case TypeEnumContainer::ListOfFilesType:
 | 
|---|
| 196 |       OptionList->add_options()
 | 
|---|
| 197 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 198 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 199 | //                  po::value < std::vector<boost::filesystem::path> >()->default_value(boost::lexical_cast< std::vector<boost::filesystem::path> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 200 |                   po::value < std::vector<boost::filesystem::path> >()->multitoken(),
 | 
|---|
| 201 |                   currentOption->getDescription().c_str())
 | 
|---|
| 202 |         ;
 | 
|---|
| 203 |       break;
 | 
|---|
| 204 |     case TypeEnumContainer::IntegerType:
 | 
|---|
| 205 |       OptionList->add_options()
 | 
|---|
| 206 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 207 |             currentOption->hasDefaultValue() ?
 | 
|---|
| 208 |                   po::value < int >()->default_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 209 |                   po::value < int >(),
 | 
|---|
| 210 |                   currentOption->getDescription().c_str())
 | 
|---|
| 211 |         ;
 | 
|---|
| 212 |       break;
 | 
|---|
| 213 |     case TypeEnumContainer::ListOfIntegersType:
 | 
|---|
| 214 |       OptionList->add_options()
 | 
|---|
| 215 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 216 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 217 | //                  po::value < std::vector<int> >()->default_value(boost::lexical_cast< std::vector<int> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 218 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
| 219 |                   currentOption->getDescription().c_str())
 | 
|---|
| 220 |         ;
 | 
|---|
| 221 |       break;
 | 
|---|
| 222 |     case TypeEnumContainer::DoubleType:
 | 
|---|
| 223 |       OptionList->add_options()
 | 
|---|
| 224 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 225 |             currentOption->hasDefaultValue() ?
 | 
|---|
| 226 |                   po::value < double >()->default_value(boost::lexical_cast<double>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 227 |                   po::value < double >(),
 | 
|---|
| 228 |                   currentOption->getDescription().c_str())
 | 
|---|
| 229 |         ;
 | 
|---|
| 230 |       break;
 | 
|---|
| 231 |     case TypeEnumContainer::ListOfDoublesType:
 | 
|---|
| 232 |       OptionList->add_options()
 | 
|---|
| 233 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 234 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 235 | //                  po::value < std::vector<double> >()->default_value(boost::lexical_cast< std::vector<double> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 236 |                   po::value < std::vector<double> >()->multitoken(),
 | 
|---|
| 237 |                   currentOption->getDescription().c_str())
 | 
|---|
| 238 |         ;
 | 
|---|
| 239 |       break;
 | 
|---|
| 240 |     case TypeEnumContainer::StringType:
 | 
|---|
| 241 |       OptionList->add_options()
 | 
|---|
| 242 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 243 |             currentOption->hasDefaultValue() ?
 | 
|---|
| 244 |                   po::value < std::string >()->default_value(currentOption->getDefaultValue()) :
 | 
|---|
| 245 |                   po::value < std::string >(),
 | 
|---|
| 246 |                   currentOption->getDescription().c_str())
 | 
|---|
| 247 |         ;
 | 
|---|
| 248 |       break;
 | 
|---|
| 249 |     case TypeEnumContainer::ListOfStringsType:
 | 
|---|
| 250 |       OptionList->add_options()
 | 
|---|
| 251 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 252 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 253 | //                  po::value < std::vector<std::string> >()->default_value(boost::lexical_cast< std::vector<std::string> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 254 |                   po::value < std::vector<std::string> >()->multitoken(),
 | 
|---|
| 255 |                   currentOption->getDescription().c_str())
 | 
|---|
| 256 |         ;
 | 
|---|
| 257 |       break;
 | 
|---|
| 258 |     case TypeEnumContainer::VectorType:
 | 
|---|
| 259 |       OptionList->add_options()
 | 
|---|
| 260 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 261 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 262 | //                  po::value < VectorValue >()->default_value(boost::lexical_cast<VectorValue>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 263 |                   po::value < VectorValue >(),
 | 
|---|
| 264 |                   currentOption->getDescription().c_str())
 | 
|---|
| 265 |         ;
 | 
|---|
| 266 |       break;
 | 
|---|
| 267 |     case TypeEnumContainer::ListOfVectorsType:
 | 
|---|
| 268 |       OptionList->add_options()
 | 
|---|
| 269 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 270 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 271 | //                  po::value < std::vector<VectorValue> >()->default_value(boost::lexical_cast< std::vector<VectorValue> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 272 |                   po::value < std::vector<VectorValue> >()->multitoken(),
 | 
|---|
| 273 |                   currentOption->getDescription().c_str())
 | 
|---|
| 274 |         ;
 | 
|---|
| 275 |       break;
 | 
|---|
| 276 |     case TypeEnumContainer::MoleculeType:
 | 
|---|
| 277 |       OptionList->add_options()
 | 
|---|
| 278 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 279 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 280 | //                  po::value < const molecule * >()->default_value(boost::lexical_cast<const molecule *>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 281 |                   po::value < int >(),
 | 
|---|
| 282 |                   currentOption->getDescription().c_str())
 | 
|---|
| 283 |         ;
 | 
|---|
| 284 |       break;
 | 
|---|
| 285 |     case TypeEnumContainer::ListOfMoleculesType:
 | 
|---|
| 286 |       OptionList->add_options()
 | 
|---|
| 287 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 288 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 289 | //                  po::value < std::vector<const molecule *> >()->default_value(boost::lexical_cast< std::vector<const molecule *> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 290 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
| 291 |                   currentOption->getDescription().c_str())
 | 
|---|
| 292 |         ;
 | 
|---|
| 293 |       break;
 | 
|---|
| 294 |     case TypeEnumContainer::AtomType:
 | 
|---|
| 295 |       OptionList->add_options()
 | 
|---|
| 296 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 297 |             currentOption->hasDefaultValue() ?
 | 
|---|
| 298 |                   po::value < int >()->default_value(boost::lexical_cast<int>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 299 |                   po::value < int >(),
 | 
|---|
| 300 |                   currentOption->getDescription().c_str())
 | 
|---|
| 301 |         ;
 | 
|---|
| 302 |       break;
 | 
|---|
| 303 |     case TypeEnumContainer::ListOfAtomsType:
 | 
|---|
| 304 |       OptionList->add_options()
 | 
|---|
| 305 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 306 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 307 | //                  po::value < std::vector<const atom *> >()->default_value(boost::lexical_cast< std::vector<const atom *> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 308 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
| 309 |                   currentOption->getDescription().c_str())
 | 
|---|
| 310 |         ;
 | 
|---|
| 311 |       break;
 | 
|---|
| 312 |     case TypeEnumContainer::ElementType:
 | 
|---|
| 313 |       OptionList->add_options()
 | 
|---|
| 314 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 315 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 316 | //                  po::value < const element * >()->default_value(boost::lexical_cast<const element *>(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 317 |                   po::value < int >(),
 | 
|---|
| 318 |                   currentOption->getDescription().c_str())
 | 
|---|
| 319 |         ;
 | 
|---|
| 320 |       break;
 | 
|---|
| 321 |     case TypeEnumContainer::ListOfElementsType:
 | 
|---|
| 322 |       OptionList->add_options()
 | 
|---|
| 323 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 324 | //            currentOption->hasDefaultValue() ?
 | 
|---|
| 325 | //                  po::value < std::vector<const element *> >()->default_value(boost::lexical_cast< std::vector<const element *> >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 326 |                   po::value < std::vector<int> >()->multitoken(),
 | 
|---|
| 327 |                   currentOption->getDescription().c_str())
 | 
|---|
| 328 |         ;
 | 
|---|
| 329 |       break;
 | 
|---|
| 330 |     case TypeEnumContainer::RandomNumberDistribution_ParametersType:
 | 
|---|
| 331 |       OptionList->add_options()
 | 
|---|
| 332 |         (currentOption->getKeyAndShortForm().c_str(),
 | 
|---|
| 333 |             currentOption->hasDefaultValue() ?
 | 
|---|
| 334 |                   po::value < std::string >()->default_value(boost::lexical_cast< std::string >(currentOption->getDefaultValue().c_str())) :
 | 
|---|
| 335 |                   po::value < std::string >(),
 | 
|---|
| 336 |                   currentOption->getDescription().c_str())
 | 
|---|
| 337 |         ;
 | 
|---|
| 338 |       break;
 | 
|---|
| 339 |   }
 | 
|---|
| 340 | }
 | 
|---|
| 341 | 
 | 
|---|
| 342 | /** States whether there are command line arguments.
 | 
|---|
| 343 |  * \return true - there are none, false - there is at least one command line argument
 | 
|---|
| 344 |  */
 | 
|---|
| 345 | bool CommandLineParser::isEmpty()
 | 
|---|
| 346 | {
 | 
|---|
| 347 |   return vm.empty();
 | 
|---|
| 348 | }
 | 
|---|
| 349 | 
 | 
|---|
| 350 | /** Sets the options.
 | 
|---|
| 351 |  * \param _argc arg count from main()
 | 
|---|
| 352 |  * \param **_argv argument array from main()
 | 
|---|
| 353 |  */
 | 
|---|
| 354 | void CommandLineParser::setOptions(int _argc, char **_argv)
 | 
|---|
| 355 | {
 | 
|---|
| 356 |   argc = _argc;
 | 
|---|
| 357 |   argv = _argv;
 | 
|---|
| 358 |   config_file_options.add(options);
 | 
|---|
| 359 |   // append all option_descriptions to both cmdline_options and visible
 | 
|---|
| 360 |   for (CmdParserLookupMap::iterator iter = CmdParserLookup.begin();
 | 
|---|
| 361 |       iter != CmdParserLookup.end();
 | 
|---|
| 362 |       ++iter) {
 | 
|---|
| 363 |     cmdline_options.add(*(iter->second));
 | 
|---|
| 364 |     visible.add(*(iter->second));
 | 
|---|
| 365 |   }
 | 
|---|
| 366 | }
 | 
|---|
| 367 | 
 | 
|---|
| 368 | /** Parses the command line arguments.
 | 
|---|
| 369 |  * Calls program_options::store() and program_options::notify()
 | 
|---|
| 370 |  */
 | 
|---|
| 371 | void CommandLineParser::Parse()
 | 
|---|
| 372 | {
 | 
|---|
| 373 |   po::store(po::command_line_parser(argc,argv).options(cmdline_options).run(), vm);
 | 
|---|
| 374 |   std::ifstream input;
 | 
|---|
| 375 |   input.open("example.cfg");
 | 
|---|
| 376 |   if (!input.fail())
 | 
|---|
| 377 |     po::store(po::parse_config_file(input, config_file_options), vm);
 | 
|---|
| 378 |   input.close();
 | 
|---|
| 379 |   po::notify(vm);
 | 
|---|
| 380 | }
 | 
|---|
| 381 | 
 | 
|---|
| 382 | /** Scan the argument list for -a or --arguments and store their order for later use.
 | 
|---|
| 383 |  */
 | 
|---|
| 384 | void CommandLineParser::scanforSequenceOfArguments()
 | 
|---|
| 385 | {
 | 
|---|
| 386 |   std::map <std::string, std::string> ShortFormToActionMap = getShortFormToActionMap();
 | 
|---|
| 387 |   DoLog(0) && (Log() << Verbose(0) << "Scanning command line arguments and recognizing Actions." << std::endl);
 | 
|---|
| 388 |   // go through all arguments
 | 
|---|
| 389 |   for (int i=1;i<argc;i++) {
 | 
|---|
| 390 |     DoLog(2) && (Log() << Verbose(2) << "Checking on " << argv[i] << std::endl);
 | 
|---|
| 391 |     // check whether they
 | 
|---|
| 392 |     if (argv[i][0] == '-') { // .. begin with -
 | 
|---|
| 393 |       DoLog(2) && (Log() << Verbose(2) << "Possible argument: " << argv[i] << endl);
 | 
|---|
| 394 |       if (argv[i][1] == '-') { // .. or --
 | 
|---|
| 395 |         DoLog(1) && (Log() << Verbose(1) << "Putting " << argv[i] << " into the sequence." << endl);
 | 
|---|
| 396 |         SequenceOfActions.push_back(&(argv[i][2]));
 | 
|---|
| 397 |         //  .. and check that next letter is not numeric, if so insert
 | 
|---|
| 398 |       } else if (((argv[i][1] < '0') || (argv[i][1] > '9')) && ((argv[i][1] != '.'))) {
 | 
|---|
| 399 |         std::map <std::string, std::string>::iterator iter = ShortFormToActionMap.find(&(argv[i][1]));
 | 
|---|
| 400 |         if (iter != ShortFormToActionMap.end()) {
 | 
|---|
| 401 |           DoLog(1) && (Log() << Verbose(1) << "Putting " << iter->second << " for " << iter->first << " into the sequence." << endl);
 | 
|---|
| 402 |           SequenceOfActions.push_back(iter->second);
 | 
|---|
| 403 |         }
 | 
|---|
| 404 |       }
 | 
|---|
| 405 |     }
 | 
|---|
| 406 |   }
 | 
|---|
| 407 | }
 | 
|---|
| 408 | 
 | 
|---|
| 409 | /** Makes the Parser parse the command line options with current known options.
 | 
|---|
| 410 |  * \param _argc arg count from main()
 | 
|---|
| 411 |  * \param **_argv argument array from main()
 | 
|---|
| 412 |  */
 | 
|---|
| 413 | void CommandLineParser::Run(int _argc, char **_argv)
 | 
|---|
| 414 | {
 | 
|---|
| 415 |   setOptions(_argc,_argv);
 | 
|---|
| 416 |   Parse();
 | 
|---|
| 417 |   scanforSequenceOfArguments();
 | 
|---|
| 418 | }
 | 
|---|
| 419 | 
 | 
|---|
| 420 | /** Go through all Actions and create a map from short form to their token.
 | 
|---|
| 421 |  * \return map from Action's ShortForm to token.
 | 
|---|
| 422 |  */
 | 
|---|
| 423 | std::map <std::string, std::string> CommandLineParser::getShortFormToActionMap() const
 | 
|---|
| 424 | {
 | 
|---|
| 425 |   std::map <std::string, std::string> result;
 | 
|---|
| 426 | 
 | 
|---|
| 427 |   ActionRegistry &AR = ActionRegistry::getInstance();
 | 
|---|
| 428 |   for (ActionRegistry::const_iterator iter = AR.getBeginIter(); iter != AR.getEndIter(); ++iter)
 | 
|---|
| 429 |     if ((iter->second)->Traits.hasShortForm()) {
 | 
|---|
| 430 |       ASSERT(result.find((iter->second)->Traits.getShortForm()) == result.end(),
 | 
|---|
| 431 |           "Short form "+toString((iter->second)->Traits.getShortForm())+
 | 
|---|
| 432 |           " for action "+toString(iter->first)+" already present from "+
 | 
|---|
| 433 |           std::string(result[(iter->second)->Traits.getShortForm()])+"!");
 | 
|---|
| 434 |       result[(iter->second)->Traits.getShortForm()] = (iter->second)->getName();
 | 
|---|
| 435 |     }
 | 
|---|
| 436 | 
 | 
|---|
| 437 |   return result;
 | 
|---|
| 438 | }
 | 
|---|
| 439 | 
 | 
|---|
| 440 | CONSTRUCT_SINGLETON(CommandLineParser)
 | 
|---|