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