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