Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
OptionsCont.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
21// A storage for options (typed value containers)
22/****************************************************************************/
23#include <config.h>
24
25#include <map>
26#include <string>
27#include <exception>
28#include <algorithm>
29#include <vector>
30#include <iostream>
31#include <cstdlib>
32#include <ctime>
33#include <cstring>
34#include <cerrno>
35#include <iterator>
36#include <sstream>
43#include <utils/xml/XMLSubSys.h>
45#include "Option.h"
46#include "OptionsIO.h"
47#include "OptionsCont.h"
48
49
50// ===========================================================================
51// static member definitions
52// ===========================================================================
53
56
57// ===========================================================================
58// method definitions
59// ===========================================================================
60
65
66
68 myCopyrightNotices.push_back(TL("Copyright (C) 2001-2026 German Aerospace Center (DLR) and others; https://sumo.dlr.de"));
69}
70
71
75
76
77void
78OptionsCont::doRegister(const std::string& name, Option* o) {
79 // first check that option isn't null
80 if (o == nullptr) {
81 throw ProcessError("Option cannot be null");
82 }
83 // now check that there isn't another addresse (or synonym) related with the option
84 if (myValues.find(name) != myValues.end()) {
85 throw ProcessError(name + " is an already used option name.");
86 }
87 // check if previously was inserted in addresses (to avoid synonyms in addresses)
88 bool isSynonym = false;
89 for (const auto& addresse : myAddresses) {
90 if (addresse.second == o) {
91 isSynonym = true;
92 }
93 }
94 if (!isSynonym) {
95 myAddresses.push_back(std::make_pair(name, o));
96 }
97 // insert in values
98 myValues[name] = o;
99}
100
101
102void
103OptionsCont::doRegister(const std::string& name1, char abbr, Option* o) {
104 doRegister(name1, o);
105 doRegister(convertChar(abbr), o);
106}
107
108
109void
110OptionsCont::addSynonyme(const std::string& name1, const std::string& name2, bool isDeprecated) {
111 auto i1 = myValues.find(name1);
112 auto i2 = myValues.find(name2);
113 if (i1 == myValues.end() && i2 == myValues.end()) {
114 throw ProcessError("Neither the option '" + name1 + "' nor the option '" + name2 + "' is known yet");
115 }
116 if (i1 != myValues.end() && i2 != myValues.end()) {
117 if ((*i1).second == (*i2).second) {
118 return;
119 }
120 throw ProcessError("Both options '" + name1 + "' and '" + name2 + "' do exist and differ.");
121 }
122 if (i1 == myValues.end() && i2 != myValues.end()) {
123 doRegister(name1, (*i2).second);
124 if (isDeprecated) {
125 myDeprecatedSynonymes[name1] = false;
126 }
127 }
128 if (i1 != myValues.end() && i2 == myValues.end()) {
129 doRegister(name2, (*i1).second);
130 if (isDeprecated) {
131 myDeprecatedSynonymes[name2] = false;
132 }
133 }
134}
135
136
137void
138OptionsCont::addXMLDefault(const std::string& name, const std::string& xmlRoot) {
139 myXMLDefaults[xmlRoot] = name;
140}
141
142
143bool
144OptionsCont::exists(const std::string& name) const {
145 return myValues.count(name) > 0;
146}
147
148
149bool
150OptionsCont::isSet(const std::string& name, bool failOnNonExistant) const {
151 auto i = myValues.find(name);
152 if (i == myValues.end()) {
153 if (failOnNonExistant) {
154 throw ProcessError(TLF("Internal request for unknown option '%'!", name));
155 } else {
156 return false;
157 }
158 }
159 return (*i).second->isSet();
160}
161
162
163bool
164OptionsCont::isDefault(const std::string& name) const {
165 auto i = myValues.find(name);
166 if (i == myValues.end()) {
167 return false;
168 }
169 return (*i).second->isDefault();
170}
171
172
173Option*
174OptionsCont::getSecure(const std::string& name) const {
175 const auto& valuesFinder = myValues.find(name);
176 if (valuesFinder == myValues.end()) {
177 throw ProcessError(TLF("No option with the name '%' exists.", name));
178 }
179 const auto& synonymFinder = myDeprecatedSynonymes.find(name);
180 if ((synonymFinder != myDeprecatedSynonymes.end()) && !synonymFinder->second) {
181 std::string defaultName;
182 for (const auto& subtopicEntry : mySubTopicEntries) {
183 for (const auto& value : subtopicEntry.second) {
184 const auto l = myValues.find(value);
185 if ((l != myValues.end()) && (l->second == valuesFinder->second)) {
186 defaultName = value;
187 break;
188 }
189 }
190 if (defaultName != "") {
191 break;
192 }
193 }
194 WRITE_WARNINGF(TL("Please note that '%' is deprecated.\n Use '%' instead."), name, defaultName);
195 synonymFinder->second = true;
196 }
197 return valuesFinder->second;
198}
199
200
201std::string
202OptionsCont::getValueString(const std::string& name) const {
203 Option* o = getSecure(name);
204 return o->getValueString();
205}
206
207
208std::string
209OptionsCont::getString(const std::string& name) const {
210 Option* o = getSecure(name);
211 return o->getString();
212}
213
214
215double
216OptionsCont::getFloat(const std::string& name) const {
217 Option* o = getSecure(name);
218 return o->getFloat();
219}
220
221
222int
223OptionsCont::getInt(const std::string& name) const {
224 Option* o = getSecure(name);
225 return o->getInt();
226}
227
228
229bool
230OptionsCont::getBool(const std::string& name) const {
231 Option* o = getSecure(name);
232 return o->getBool();
233}
234
235
236const IntVector&
237OptionsCont::getIntVector(const std::string& name) const {
238 Option* o = getSecure(name);
239 return o->getIntVector();
240}
241
242const StringVector&
243OptionsCont::getStringVector(const std::string& name) const {
244 Option* o = getSecure(name);
245 return o->getStringVector();
246}
247
248
249bool
250OptionsCont::set(const std::string& name, const std::string& value, const bool append) {
251 Option* o = getSecure(name);
252 if (!o->isWriteable()) {
254 return false;
255 }
256 try {
257 // Substitute environment variables defined by ${NAME} with their value
258 if (!o->set(StringUtils::substituteEnvironment(value, &OptionsIO::getLoadTime()), value, append)) {
259 return false;
260 }
261 } catch (ProcessError& e) {
262 WRITE_ERROR("While processing option '" + name + "':\n " + e.what());
263 return false;
264 }
265 return true;
266}
267
268
269bool
270OptionsCont::setDefault(const std::string& name, const std::string& value) {
271 Option* const o = getSecure(name);
272 if (o->isWriteable() && set(name, value)) {
273 o->resetDefault();
274 return true;
275 }
276 return false;
277}
278
279
280bool
281OptionsCont::setByRootElement(const std::string& root, const std::string& value) {
282 if (myXMLDefaults.count(root) > 0) {
283 return set(myXMLDefaults[root], value);
284 }
285 if (myXMLDefaults.count("") > 0) {
286 return set(myXMLDefaults[""], value);
287 }
288 return false;
289}
290
291
292std::vector<std::string>
293OptionsCont::getSynonymes(const std::string& name) const {
294 Option* o = getSecure(name);
295 std::vector<std::string> synonymes;
296 for (const auto& value : myValues) {
297 if ((value.second == o) && (name != value.first)) {
298 synonymes.push_back(value.first);
299 }
300 }
301 return synonymes;
302}
303
304
305const std::string&
306OptionsCont::getDescription(const std::string& name) const {
307 return getSecure(name)->getDescription();
308}
309
310
311const std::string&
312OptionsCont::getSubTopic(const std::string& name) const {
313 return getSecure(name)->getSubTopic();
314}
315
316
317std::ostream&
318operator<<(std::ostream& os, const OptionsCont& oc) {
319 std::vector<std::string> done;
320 os << "Options set:" << std::endl;
321 for (const auto& value : oc.myValues) {
322 const auto& finder = std::find(done.begin(), done.end(), value.first);
323 if (finder == done.end()) {
324 std::vector<std::string> synonymes = oc.getSynonymes(value.first);
325 if (synonymes.size() != 0) {
326 os << value.first << " (";
327 for (auto synonym = synonymes.begin(); synonym != synonymes.end(); synonym++) {
328 if (synonym != synonymes.begin()) {
329 os << ", ";
330 }
331 os << (*synonym);
332 }
333 os << ")";
334 } else {
335 os << value.first;
336 }
337 if (value.second->isSet()) {
338 os << ": " << value.second->getValueString() << std::endl;
339 } else {
340 os << ": <INVALID>" << std::endl;
341 }
342 done.push_back(value.first);
343 copy(synonymes.begin(), synonymes.end(), back_inserter(done));
344 }
345 }
346 return os;
347}
348
349
350void
351OptionsCont::relocateFiles(const std::string& configuration) const {
352 for (const auto& addresse : myAddresses) {
353 if (addresse.second->isFileName() && addresse.second->isSet()) {
354 StringVector fileList = StringVector(addresse.second->getStringVector());
355 for (auto& file : fileList) {
356 if (addresse.first != "configuration-file") {
357 file = FileHelpers::checkForRelativity(file, configuration);
358 }
359 try {
360 file = StringUtils::urlDecode(file);
361 } catch (NumberFormatException& e) {
362 WRITE_WARNING(toString(e.what()) + " when trying to decode filename '" + file + "'.");
363 }
364 }
365 StringVector rawList = StringTokenizer(addresse.second->getValueString(), ",").getVector();
366 for (auto& file : rawList) {
367 file = FileHelpers::checkForRelativity(file, configuration);
368 }
369 const std::string conv = joinToString(fileList, ',');
370 if (conv != joinToString(addresse.second->getStringVector(), ',')) {
371 const bool hadDefault = addresse.second->isDefault();
372 addresse.second->set(conv, joinToString(rawList, ','), false);
373 if (hadDefault) {
374 addresse.second->resetDefault();
375 }
376 }
377 }
378 }
379}
380
381
382bool
383OptionsCont::isUsableFileList(const std::string& name) const {
384 Option* const o = getSecure(name);
385 if (!o->isSet()) {
386 return false;
387 }
388 // check whether the list of files is valid
389 bool ok = true;
390 std::vector<std::string> files = getStringVector(name);
391 if (files.size() == 0) {
392 WRITE_ERRORF(TL("The file list for '%' is empty."), name);
393 ok = false;
394 }
395 for (const auto& file : files) {
396 if (!FileHelpers::isReadable(file)) {
397 if (file != "") {
398 WRITE_ERRORF(TL("File '%' is not accessible (%)."), file, std::strerror(errno));
399 ok = false;
400 } else {
401 WRITE_WARNING(TL("Empty file name given; ignoring."));
402 }
403 }
404 }
405 return ok;
406}
407
408
409bool
410OptionsCont::checkDependingSuboptions(const std::string& name, const std::string& prefix) const {
411 Option* o = getSecure(name);
412 if (o->isSet()) {
413 return true;
414 }
415 bool ok = true;
416 std::vector<std::string> seenSynonymes;
417 for (const auto& value : myValues) {
418 if (std::find(seenSynonymes.begin(), seenSynonymes.end(), value.first) != seenSynonymes.end()) {
419 continue;
420 }
421 if (value.second->isSet() && !value.second->isDefault() && value.first.find(prefix) == 0) {
422 WRITE_ERRORF(TL("Option '%' needs option '%'."), value.first, name);
423 std::vector<std::string> synonymes = getSynonymes(value.first);
424 std::copy(synonymes.begin(), synonymes.end(), std::back_inserter(seenSynonymes));
425 ok = false;
426 }
427 }
428 return ok;
429}
430
431
432void
433OptionsCont::reportDoubleSetting(const std::string& arg) const {
434 std::vector<std::string> synonymes = getSynonymes(arg);
435 std::ostringstream s;
436 s << TLF("A value for the option '%' was already set.\n Possible synonymes: ", arg);
437 auto synonym = synonymes.begin();
438 while (synonym != synonymes.end()) {
439 s << (*synonym);
440 synonym++;
441 if (synonym != synonymes.end()) {
442 s << ", ";
443 }
444 }
445 WRITE_ERROR(s.str());
446}
447
448
449std::string
450OptionsCont::convertChar(char abbr) const {
451 char buf[2];
452 buf[0] = abbr;
453 buf[1] = 0;
454 std::string s(buf);
455 return s;
456}
457
458
459bool
460OptionsCont::isBool(const std::string& name) const {
461 Option* o = getSecure(name);
462 return o->isBool();
463}
464
465
466void
468 for (const auto& addresse : myAddresses) {
469 addresse.second->resetWritable();
470 }
471}
472
473
474void
475OptionsCont::resetWritable(const std::string& name) {
476 getSecure(name)->resetWritable();
477}
478
479
480void
482 for (const auto& addresse : myAddresses) {
483 addresse.second->resetDefault();
484 }
485}
486
487
488void
489OptionsCont::resetDefault(const std::string& name) {
490 getSecure(name)->resetDefault();
491}
492
493
494bool
495OptionsCont::isWriteable(const std::string& name) {
496 return getSecure(name)->isWriteable();
497}
498
499
500bool
501OptionsCont::isEditable(const std::string& name) {
502 return getSecure(name)->isEditable();
503
504}
505
506
507void
509 // delete only address (because synonyms placed in values aim to the same Option)
510 for (const auto& addresse : myAddresses) {
511 delete addresse.second;
512 }
513 myAddresses.clear();
514 myValues.clear();
515 mySubTopics.clear();
516 mySubTopicEntries.clear();
517}
518
519
520void
521OptionsCont::addDescription(const std::string& name, const std::string& subtopic,
522 const std::string& description) {
523 Option* o = getSecure(name);
524 if (o == nullptr) {
525 throw ProcessError("Option doesn't exist");
526 }
527 if (find(mySubTopics.begin(), mySubTopics.end(), subtopic) == mySubTopics.end()) {
528 throw ProcessError("SubTopic '" + subtopic + "' doesn't exist");
529 }
530 o->setDescription(description);
531 o->setSubtopic(subtopic);
532 mySubTopicEntries[subtopic].push_back(name);
533}
534
535
536void
537OptionsCont::setFurtherAttributes(const std::string& name, const std::string& subtopic, bool required, bool positional, const std::string& listSep) {
538 Option* o = getSecure(name);
539 if (o == nullptr) {
540 throw ProcessError("Option doesn't exist");
541 }
542 if (find(mySubTopics.begin(), mySubTopics.end(), subtopic) == mySubTopics.end()) {
543 throw ProcessError("SubTopic '" + subtopic + "' doesn't exist");
544 }
545 if (required) {
546 o->setRequired();
547 }
548 if (positional) {
549 o->setPositional();
550 }
551 o->setListSeparator(listSep);
552}
553
554
555void
556OptionsCont::setOptionEditable(const std::string& name, const bool value) {
557 getSecure(name)->setEditable(value);
558}
559
560
561void
562OptionsCont::setApplicationName(const std::string& appName, const std::string& fullName) {
563 myAppName = appName;
564 myFullName = fullName;
565}
566
567
568void
569OptionsCont::setApplicationDescription(const std::string& appDesc) {
570 myAppDescription = appDesc;
571}
572
573
574void
575OptionsCont::addCallExample(const std::string& example, const std::string& desc) {
576 myCallExamples.push_back(std::make_pair(example, desc));
577}
578
579
580void
583}
584
585
586void
587OptionsCont::addCopyrightNotice(const std::string& copyrightLine) {
588 myCopyrightNotices.push_back(copyrightLine);
589}
590
591
592void
596
597
598void
599OptionsCont::addOptionSubTopic(const std::string& topic) {
600 mySubTopics.push_back(topic);
601 mySubTopicEntries[topic] = std::vector<std::string>();
602}
603
604
605void
606OptionsCont::splitLines(std::ostream& os, std::string what,
607 int offset, int nextOffset) {
608 while (what.length() > 0) {
609 if ((int)what.length() > 79 - offset) {
610 std::string::size_type splitPos = what.rfind(';', 79 - offset);
611 if (splitPos == std::string::npos) {
612 splitPos = what.rfind(' ', 79 - offset);
613 } else {
614 splitPos++;
615 }
616 if (splitPos != std::string::npos) {
617 os << what.substr(0, splitPos) << std::endl;
618 what = what.substr(splitPos + 1);
619 for (int r = 0; r < (nextOffset + 1); ++r) {
620 os << ' ';
621 }
622 } else {
623 os << what;
624 what = "";
625 }
626 offset = nextOffset;
627 } else {
628 os << what;
629 what = "";
630 }
631 }
632 os << std::endl;
633}
634
635
636bool
638 MsgHandler::setupI18n(getString("language"));
640 if (missingOptions) {
641 // no options are given
642 std::cout << myFullName << std::endl;
643 std::cout << TL(" Build features: ") << HAVE_ENABLED << std::endl;
644 for (const auto& copyrightNotice : myCopyrightNotices) {
645 std::cout << " " << copyrightNotice.data() << std::endl;
646 }
647 std::cout << TL(" License EPL-2.0: Eclipse Public License Version 2 <https://eclipse.org/legal/epl-v20.html>") << std::endl;
648 std::cout << TL(" Use --help to get the list of options.") << std::endl;
649 return true;
650 }
651
652 // check whether the help shall be printed
653 if (getBool("help")) {
654 std::cout << myFullName << std::endl;
655 for (const auto& copyrightNotice : myCopyrightNotices) {
656 std::cout << " " << copyrightNotice.data() << std::endl;
657 }
658 printHelp(std::cout);
659 return true;
660 }
661 // check whether the help shall be printed
662 if (getBool("version")) {
663 std::cout << myFullName << std::endl;
664 std::cout << TL(" Build features: ") << HAVE_ENABLED << std::endl;
665 for (const auto& copyrightNotice : myCopyrightNotices) {
666 std::cout << " " << copyrightNotice.data() << std::endl;
667 }
668 std::cout << "\n" << myFullName << " is part of SUMO.\n";
669 std::cout << "This program and the accompanying materials\n";
670 std::cout << "are made available under the terms of the Eclipse Public License v2.0\n";
671 std::cout << "which accompanies this distribution, and is available at\n";
672 std::cout << "http://www.eclipse.org/legal/epl-v20.html\n";
673 std::cout << "This program may also be made available under the following Secondary\n";
674 std::cout << "Licenses when the conditions for such availability set forth in the Eclipse\n";
675 std::cout << "Public License 2.0 are satisfied: GNU General Public License, version 2\n";
676 std::cout << "or later which is available at\n";
677 std::cout << "https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html\n";
678 std::cout << "SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later" << std::endl;
679 return true;
680 }
681 // check whether the settings shall be printed
682 if (getBool("print-options")) {
683 std::cout << (*this);
684 }
685 // check whether something has to be done with options
686 // whether the current options shall be saved
687 if (isSet("save-configuration")) {
688 const std::string& configPath = getString("save-configuration");
689 if (configPath == "-" || configPath == "stdout") {
690 writeConfiguration(std::cout, true, false, getBool("save-commented"));
691 return true;
692 }
693 std::ofstream out(XMLSubSys::transcodeToLocal(configPath).c_str());
694 if (!out.good()) {
695 throw ProcessError(TLF("Could not save configuration to '%'", configPath));
696 } else {
697 writeConfiguration(out, true, false, getBool("save-commented"), configPath);
698 if (getBool("verbose")) {
699 WRITE_MESSAGEF(TL("Written configuration to '%'"), configPath);
700 }
701 return true;
702 }
703 }
704 // whether the template shall be saved
705 if (isSet("save-template")) {
706 if (getString("save-template") == "-" || getString("save-template") == "stdout") {
707 writeConfiguration(std::cout, false, true, getBool("save-commented"));
708 return true;
709 }
710 std::ofstream out(XMLSubSys::transcodeToLocal(getString("save-template")).c_str());
711 if (!out.good()) {
712 throw ProcessError(TLF("Could not save template to '%'", getString("save-template")));
713 } else {
714 writeConfiguration(out, false, true, getBool("save-commented"));
715 if (getBool("verbose")) {
716 WRITE_MESSAGEF(TL("Written template to '%'"), getString("save-template"));
717 }
718 return true;
719 }
720 }
721 if (isSet("save-schema")) {
722 if (getString("save-schema") == "-" || getString("save-schema") == "stdout") {
723 writeSchema(std::cout);
724 return true;
725 }
726 std::ofstream out(XMLSubSys::transcodeToLocal(getString("save-schema")).c_str());
727 if (!out.good()) {
728 throw ProcessError(TLF("Could not save schema to '%'", getString("save-schema")));
729 } else {
730 writeSchema(out);
731 if (getBool("verbose")) {
732 WRITE_MESSAGEF(TL("Written schema to '%'"), getString("save-schema"));
733 }
734 return true;
735 }
736 }
737 return false;
738}
739
740
741void
744 // options
745 for (auto option : myAddresses) {
746 option.second->setDescription(TL(option.second->getDescription().c_str()));
747 }
748 // examples
749 for (auto example : myCallExamples) {
750 example.second = TL(example.second.c_str());
751 }
752 // other text
754 myAmLocalized = true;
755 }
756}
757
758
759const std::vector<std::string>&
761 return mySubTopics;
762}
763
764
765std::vector<std::string>
766OptionsCont::getSubTopicsEntries(const std::string& subtopic) const {
767 if (mySubTopicEntries.count(subtopic) > 0) {
768 return mySubTopicEntries.find(subtopic)->second;
769 } else {
770 return std::vector<std::string>();
771 }
772}
773
774
775std::string
776OptionsCont::getTypeName(const std::string name) {
777 return getSecure(name)->getTypeName();
778}
779
780
781const std::string&
783 return myFullName;
784}
785
786
787bool
789 return myAddresses.size() == 0;
790}
791
792
793std::vector<std::pair<std::string, Option*> >::const_iterator
795 return myAddresses.cbegin();
796}
797
798
799std::vector<std::pair<std::string, Option*> >::const_iterator
801 return myAddresses.cend();
802}
803
804
805void
806OptionsCont::printHelp(std::ostream& os) {
807 // print application description
808 splitLines(os, TL(myAppDescription.c_str()), 0, 0);
809 os << std::endl;
810
811 // check option sizes first
812 // we want to know how large the largest not-too-large-entry will be
813 int tooLarge = 40;
814 int maxSize = 0;
815 for (const auto& subTopic : mySubTopics) {
816 for (const auto& entry : mySubTopicEntries[subTopic]) {
817 Option* o = getSecure(entry);
818 // name, two leading spaces and "--"
819 int csize = (int)entry.length() + 2 + 4;
820 // abbreviation length ("-X, "->4chars) if any
821 const auto synonymes = getSynonymes(entry);
822 for (const auto& synonym : synonymes) {
823 if (synonym.length() == 1 && myDeprecatedSynonymes.count(synonym) == 0) {
824 csize += 4;
825 break;
826 }
827 }
828 // the type name
829 if (!o->isBool()) {
830 csize += 1 + (int)o->getTypeName().length();
831 }
832 // divider
833 csize += 2;
834 if (csize < tooLarge && maxSize < csize) {
835 maxSize = csize;
836 }
837 }
838 }
839
840 const std::string helpTopic = StringUtils::to_lower_case(getSecure("help")->getValueString());
841 if (helpTopic != "") {
842 bool foundTopic = false;
843 for (const auto& topic : mySubTopics) {
844 if (StringUtils::to_lower_case(topic).find(helpTopic) != std::string::npos) {
845 foundTopic = true;
846 printHelpOnTopic(topic, tooLarge, maxSize, os);
847 }
848 }
849 if (!foundTopic) {
850 // print topic list
851 os << TL("Help Topics:") << std::endl;
852 for (const std::string& t : mySubTopics) {
853 os << " " << t << std::endl;
854 }
855 }
856 return;
857 }
858 // print usage BNF
859 os << TL("Usage: ") << myAppName << TL(" [OPTION]*") << std::endl;
860 // print additional text if any
861 if (myAdditionalMessage.length() > 0) {
862 os << myAdditionalMessage << std::endl << std::endl;
863 }
864 // print the options
865 for (const auto& subTopic : mySubTopics) {
866 printHelpOnTopic(subTopic, tooLarge, maxSize, os);
867 }
868 os << std::endl;
869 // print usage examples, calc size first
870 if (myCallExamples.size() != 0) {
871 os << TL("Examples:") << std::endl;
872 for (const auto& callExample : myCallExamples) {
873 os << " " << myAppName << ' ' << callExample.first << std::endl;
874 os << " " << callExample.second << std::endl;
875 }
876 }
877 os << std::endl;
878 os << TLF("Report bugs at %.", "<https://github.com/eclipse-sumo/sumo/issues>") << std::endl;
879 os << TLF("Get in contact via %.", "<sumo@dlr.de>") << std::endl;
880}
881
882
883void
884OptionsCont::printHelpOnTopic(const std::string& topic, int tooLarge, int maxSize, std::ostream& os) {
885 os << TLF("% Options:", topic) << std::endl;
886 for (const auto& entry : mySubTopicEntries[topic]) {
887 // start length computation
888 int csize = (int)entry.length() + 2;
889 Option* o = getSecure(entry);
890 os << " ";
891 // write abbreviation if given
892 const auto synonymes = getSynonymes(entry);
893 for (const auto& synonym : synonymes) {
894 if (synonym.length() == 1 && myDeprecatedSynonymes.count(synonym) == 0) {
895 os << '-' << synonym << ", ";
896 csize += 4;
897 break;
898 }
899 }
900 // write leading '-'/"--"
901 os << "--";
902 csize += 2;
903 // write the name
904 os << entry;
905 // write the type if not a bool option
906 if (!o->isBool()) {
907 os << ' ' << o->getTypeName();
908 csize += 1 + (int)o->getTypeName().length();
909 }
910 csize += 2;
911 // write the description formatting it
912 os << " ";
913 for (int r = maxSize; r > csize; --r) {
914 os << ' ';
915 }
916 int offset = csize > tooLarge ? csize : maxSize;
917 splitLines(os, o->getDescription(), offset, maxSize);
918 }
919 os << std::endl;
920}
921
922
923void
924OptionsCont::writeConfiguration(std::ostream& os, const bool filled,
925 const bool complete, const bool addComments, const std::string& relativeTo,
926 const bool forceRelative, const bool inComment, const std::string& indent) const {
927 if (!inComment) {
928 writeXMLHeader(os, false);
929 }
930 const std::string& app = myAppName == "sumo-gui" ? "sumo" : myAppName;
931 os << indent << "<" << app << "Configuration xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
932 << "xsi:noNamespaceSchemaLocation=\"http://sumo.dlr.de/xsd/" << app << "Configuration.xsd\">\n\n";
933 for (std::string subtopic : mySubTopics) {
934 if (subtopic == "Configuration" && !complete) {
935 continue;
936 }
937 const std::vector<std::string>& entries = mySubTopicEntries.find(subtopic)->second;
938 std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
939 subtopic = StringUtils::to_lower_case(subtopic);
940 bool hadOne = false;
941 for (const std::string& name : entries) {
942 Option* o = getSecure(name);
943 bool write = complete || (filled && !o->isDefault());
944 if (!write) {
945 continue;
946 }
947 if (name == "registry-viewport" && !complete) {
948 continue;
949 }
950 if (!hadOne) {
951 os << indent << " <" << subtopic << ">\n";
952 }
953 // add the comment if wished
954 if (addComments) {
955 os << indent << " <!-- " << StringUtils::escapeXML(o->getDescription(), inComment) << " -->\n";
956 }
957 // write the option and the value (if given)
958 os << indent << " <" << name << " value=\"";
959 if (o->isSet() && (filled || o->isDefault())) {
960 if (o->isFileName() && relativeTo != "") {
962 for (auto& file : fileList) {
963 if (StringUtils::startsWith(file, "${")) {
964 // there is an environment variable up front, assume it points to an absolute path
965 // not even forcing relativity makes sense here
966 file = StringUtils::urlEncode(file, " ;%");
967 } else {
969 StringUtils::urlEncode(file, " ;%"),
970 StringUtils::urlEncode(relativeTo, " ;%"),
971 forceRelative || getBool("save-configuration.relative"));
972 }
973 }
974 os << StringUtils::escapeXML(joinToString(fileList, ','), inComment);
975 } else {
976 os << StringUtils::escapeXML(o->getValueString(), inComment);
977 }
978 }
979 if (complete) {
980 const std::vector<std::string> synonymes = getSynonymes(name);
981 if (!synonymes.empty()) {
982 os << "\" synonymes=\"" << toString(synonymes);
983 }
984 std::string deprecated;
985 for (const auto& synonym : synonymes) {
986 if (myDeprecatedSynonymes.count(synonym) > 0) {
987 deprecated += " " + synonym;
988 }
989 }
990 if (deprecated != "") {
991 os << "\" deprecated=\"" << deprecated.substr(1);
992 }
993 os << "\" type=\"" << o->getTypeName();
994 if (!addComments) {
995 os << "\" help=\"" << StringUtils::escapeXML(o->getDescription());
996 }
997 }
998 os << "\"/>\n";
999 // append an endline if a comment was printed
1000 if (addComments) {
1001 os << "\n";
1002 }
1003 hadOne = true;
1004 }
1005 if (hadOne) {
1006 os << indent << " </" << subtopic << ">\n\n";
1007 }
1008 }
1009 os << indent << "</" << app << "Configuration>" << std::endl; // flushing seems like a good idea here
1010}
1011
1012
1013void
1014OptionsCont::writeSchema(std::ostream& os) {
1015 const std::string& app = myAppName == "sumo-gui" ? "sumo" : myAppName;
1016 writeXMLHeader(os, false);
1017 os << "<xsd:schema elementFormDefault=\"qualified\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n";
1018 os << " <xsd:complexType name=\"" << app << "ConfigurationType\">\n";
1019 os << " <xsd:all>\n";
1020 for (std::string subtopic : mySubTopics) {
1021 if (subtopic == "Configuration") {
1022 continue;
1023 }
1024 std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
1025 subtopic = StringUtils::to_lower_case(subtopic);
1026 os << " <xsd:element name=\"" << subtopic << "\" type=\"" << app << subtopic << "TopicType\" minOccurs=\"0\"/>\n";
1027 }
1028 os << " </xsd:all>\n";
1029 os << " </xsd:complexType>\n\n";
1030 for (std::string subtopic : mySubTopics) {
1031 if (subtopic == "Configuration") {
1032 continue;
1033 }
1034 const std::vector<std::string>& entries = mySubTopicEntries.find(subtopic)->second;
1035 std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
1036 subtopic = StringUtils::to_lower_case(subtopic);
1037 os << " <xsd:complexType name=\"" << app << subtopic << "TopicType\">\n";
1038 os << " <xsd:all>\n";
1039 for (const auto& entry : entries) {
1040 Option* o = getSecure(entry);
1041 std::string type = o->getTypeName();
1042 type = StringUtils::to_lower_case(type);
1043 if (type == "int[]") {
1044 type = "intArray";
1045 }
1046 if (type == "str[]") {
1047 type = "strArray";
1048 }
1049 os << " <xsd:element name=\"" << entry << "\" type=\"" << type << "OptionType\" minOccurs=\"0\"/>\n";
1050 }
1051 os << " </xsd:all>\n";
1052 os << " </xsd:complexType>\n\n";
1053 }
1054 os << "</xsd:schema>\n";
1055}
1056
1057
1058void
1059OptionsCont::writeXMLHeader(std::ostream& os, const bool includeConfig) const {
1060 os << "<?xml version=\"1.0\"" << SUMOSAXAttributes::ENCODING << "?>\n\n";
1061 os << "<!-- ";
1062 if (!getBool("write-metadata")) {
1063 os << "generated on " << StringUtils::isoTimeString() << " by " << myFullName << "\n";
1064 }
1065 if (getBool("write-license")) {
1066 os << "This data file and the accompanying materials\n"
1067 "are made available under the terms of the Eclipse Public License v2.0\n"
1068 "which accompanies this distribution, and is available at\n"
1069 "http://www.eclipse.org/legal/epl-v20.html\n"
1070 "This file may also be made available under the following Secondary\n"
1071 "Licenses when the conditions for such availability set forth in the Eclipse\n"
1072 "Public License 2.0 are satisfied: GNU General Public License, version 2\n"
1073 "or later which is available at\n"
1074 "https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html\n"
1075 "SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later\n";
1076 }
1077 if (includeConfig && !getBool("write-metadata")) {
1078 writeConfiguration(os, true, false, false, "", false, true);
1079 }
1080 os << "-->\n\n";
1081}
1082
1083
1084bool
1085OptionsCont::isInStringVector(const std::string& optionName,
1086 const std::string& itemName) const {
1087 if (isSet(optionName)) {
1088 std::vector<std::string> values = getStringVector(optionName);
1089 return std::find(values.begin(), values.end(), itemName) != values.end();
1090 }
1091 return false;
1092}
1093
1094
1097 // build a clone to call writeConfiguration on
1098 // (with the possibility of changing a few settings and not affecting the original)
1099 OptionsCont* oc = new OptionsCont(*this);
1100 oc->resetWritable();
1101 for (auto& addr : oc->myAddresses) {
1102 addr.second = addr.second->clone();
1103 }
1104 return oc;
1105}
1106
1107
1108/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define WRITE_MESSAGEF(...)
Definition MsgHandler.h:289
#define WRITE_ERRORF(...)
Definition MsgHandler.h:296
#define WRITE_ERROR(msg)
Definition MsgHandler.h:295
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
std::vector< std::string > StringVector
Definition of a vector of strings.
Definition Option.h:42
std::vector< int > IntVector
Definition of a vector of ints.
Definition Option.h:37
std::ostream & operator<<(std::ostream &os, const OptionsCont &oc)
bool gLocaleInitialized
Definition StdDefs.cpp:34
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:314
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
static std::string fixRelative(const std::string &filename, const std::string &basePath, const bool force, std::string curDir="")
Fixes the relative path for the given filename in relation to the basePath (usually a config file).
static std::string checkForRelativity(const std::string &filename, const std::string &basePath)
Returns the path from a configuration so that it is accessible from the current working directory.
static bool isReadable(std::string path)
Checks whether the given file is readable.
static void setupI18n(const std::string &locale="")
set up gettext stuff
A class representing a single program option.
Definition Option.h:74
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition Option.cpp:184
bool isSet() const
returns the information whether this options holds a valid value
Definition Option.cpp:53
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition Option.cpp:112
void setRequired()
mark option as required
Definition Option.cpp:220
virtual std::string getString() const
Returns the stored string value.
Definition Option.cpp:71
virtual const IntVector & getIntVector() const
Returns the stored integer vector.
Definition Option.cpp:83
void resetWritable()
Resets the option to be writeable.
Definition Option.cpp:190
const std::string & getDescription() const
Returns the description of what this option does.
Definition Option.cpp:202
void setListSeparator(const std::string &listSep)
set list separator
Definition Option.cpp:252
virtual bool isFileName() const
Returns the information whether this option is a file name.
Definition Option.cpp:136
virtual const StringVector & getStringVector() const
Returns the stored string vector.
Definition Option.cpp:89
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition Option.cpp:208
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition Option.cpp:269
virtual int getInt() const
Returns the stored integer value.
Definition Option.cpp:65
virtual double getFloat() const
Returns the stored double value.
Definition Option.cpp:59
virtual bool getBool() const
Returns the stored boolean value.
Definition Option.cpp:77
void setPositional()
mark option as positional
Definition Option.cpp:230
void resetDefault()
Resets the option to be on its default value.
Definition Option.cpp:196
const std::string & getSubTopic() const
Returns the subtopic to which this option belongs.
Definition Option.cpp:257
virtual bool set(const std::string &v, const std::string &orig, const bool append)=0
Stores the given value.
bool isEditable() const
check if this option is editable
Definition Option.cpp:236
virtual bool isBool() const
Returns the information whether the option is a bool option.
Definition Option.cpp:130
void setEditable(const bool value)
set editable
Definition Option.cpp:241
const std::string & getValueString() const
Returns the string-representation of the value.
Definition Option.cpp:106
void setSubtopic(const std::string &subtopic)
Sets the subtopic to which this option belongs.
Definition Option.cpp:263
A storage for options typed value containers)
Definition OptionsCont.h:89
void setAdditionalHelpMessage(const std::string &add)
Sets an additional message to be printed at the begin of the help screen.
~OptionsCont()
Destructor.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void writeConfiguration(std::ostream &os, const bool filled, const bool complete, const bool addComments, const std::string &relativeTo="", const bool forceRelative=false, const bool inComment=false, const std::string &indent="") const
Writes the configuration.
void resetDefault()
Resets all options to default.
void setFurtherAttributes(const std::string &name, const std::string &subtopic, bool required, bool positional, const std::string &listSep)
mark option as required
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
std::vector< std::pair< std::string, std::string > > myCallExamples
list of call examples
bool isWriteable(const std::string &name)
Returns the information whether the named option may be set.
std::map< std::string, std::vector< std::string > > mySubTopicEntries
A map from subtopic to option.
void writeXMLHeader(std::ostream &os, const bool includeConfig=true) const
Writes a standard XML header, including the configuration.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
void splitLines(std::ostream &os, std::string what, int offset, int nextOffset)
Writes the given string 'formatted'.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
OptionsCont * clone() const
make a copy of this OptionsCont instance
void printHelpOnTopic(const std::string &topic, int tooLarge, int maxSize, std::ostream &os)
Prints help on the given topic.
const std::string & getSubTopic(const std::string &name) const
Returns the option category.
std::map< std::string, Option * > myValues
option maps sorted by name (for addresses AND their synonyms)
bool isEmpty() const
check if options container is empty
std::string myAdditionalMessage
std::vector< std::string > myCopyrightNotices
const std::vector< std::string > & getSubTopics() const
return the list of subtopics
void setOptionEditable(const std::string &name, const bool value)
set option editable
bool myAmLocalized
Whether the descriptino has already been translated to the locale language.
std::vector< std::pair< std::string, Option * > > myAddresses
option-addresses
const IntVector & getIntVector(const std::string &name) const
Returns the list of integer-value of the named option (only for Option_IntVector)
std::vector< std::string > getSynonymes(const std::string &name) const
Returns the synonymes of an option name.
void reportDoubleSetting(const std::string &arg) const
Reports an error that the option has already been set.
std::vector< std::string > mySubTopics
lists of option subtopics and copyright notices
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
static OptionsCont EMPTY_OPTIONS
empty option container
Definition OptionsCont.h:96
void localizeDescriptions()
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
bool setDefault(const std::string &name, const std::string &value)
Sets the given value for the named option as new default value.
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
bool exists(const std::string &name) const
Returns the information whether the named option is known.
const std::string & getFullName() const
get options full name
bool isBool(const std::string &name) const
Returns the information whether the option is a boolean option.
void addCopyrightNotice(const std::string &copyrightLine)
Adds a copyright notice to the help output.
std::string getTypeName(const std::string name)
return the type name for the given option
void writeSchema(std::ostream &os)
Writes the xml schema for the configuration.
void clear()
Removes all information from the container.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
std::vector< std::pair< std::string, Option * > >::const_iterator begin() const
get begin addresses iterator
bool set(const std::string &name, const std::string &value, const bool append=false)
Sets the given value for the named option.
bool isEditable(const std::string &name)
Returns the information whether the named option is editable.
void clearCopyrightNotices()
Removes all copyright information.
std::string convertChar(char abbr) const
Converts an abbreviation into a name.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
OptionsCont()
Constructor.
void printHelp(std::ostream &os)
Prints the help.
std::vector< std::pair< std::string, Option * > >::const_iterator end() const
get begin addresses iterator
std::string getValueString(const std::string &name) const
Returns the string-value of the named option (all options)
std::string myAppDescription
const std::string & getDescription(const std::string &name) const
Returns the option description.
bool setByRootElement(const std::string &name, const std::string &value)
Sets the given value for the option which can handle the given XML root.
std::map< std::string, bool > myDeprecatedSynonymes
A map from deprecated options to a bool indicating whether we warned about deprecation.
static OptionsCont myOptions
The static options container used.
bool checkDependingSuboptions(const std::string &name, const std::string &prefix) const
Checks whether an option is set, which has options with a prefix depending on it.
std::map< std::string, std::string > myXMLDefaults
A map from XML root element to option.
std::string myAppName
some information on the application
void resetWritable()
Resets all options to be writeable.
void addXMLDefault(const std::string &name, const std::string &xmlRoot="")
Adds an XML root element to handle by default. The special root "" denotes the default handler.
static OptionsCont & getOptions()
Retrieves the options.
std::string myFullName
std::vector< std::string > getSubTopicsEntries(const std::string &subtopic) const
return the list of entries for the given subtopic
Option * getSecure(const std::string &name) const
Returns the named option.
void relocateFiles(const std::string &configuration) const
Modifies file name options according to the configuration path.
bool isInStringVector(const std::string &optionName, const std::string &itemName) const
Returns the named option is a list of string values containing the specified item.
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
bool isUsableFileList(const std::string &name) const
Checks whether the named option is usable as a file list (with at least a single file)
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
static const std::chrono::time_point< std::chrono::system_clock > & getLoadTime()
Return the time stamp of the last init.
Definition OptionsIO.h:101
static const std::string ENCODING
The encoding of parsed strings.
std::vector< std::string > getVector()
return vector of strings
static std::string urlEncode(const std::string &url, const std::string encodeWhich="")
encode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/)
static std::string urlDecode(const std::string &encoded)
decode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/)
static std::string to_lower_case(const std::string &str)
Transfers the content to lower case.
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
static std::string substituteEnvironment(const std::string &str, const std::chrono::time_point< std::chrono::system_clock > *const timeRef=nullptr)
Replaces an environment variable with its value (similar to bash); syntax for a variable is ${NAME}.
static std::string isoTimeString(const std::chrono::time_point< std::chrono::system_clock > *const timeRef=nullptr)
Returns an ISO8601 formatted time string with microsecond precision.
static std::string transcodeToLocal(const std::string &utf8String)
convert a string from UTF-8 to the local codepage