3#include <boost/algorithm/string.hpp>
4#include <boost/io/ios_state.hpp>
55SettingsManager::~SettingsManager() {
61 return settingsManager;
66 this->executableName = executableName;
72 std::vector<std::string> argumentVector(argc - 1);
73 for (
int i = 1; i < argc; ++i) {
74 argumentVector[i - 1] = std::string(argv[i]);
81 if (commandLineString.empty()) {
84 std::vector<std::string> argumentVector;
85 boost::split(argumentVector, commandLineString, boost::is_any_of(
"\t "));
91 std::string optionNameWithDashes = (isShort ?
"-" :
"--") + optionName;
93 std::map<std::string, std::vector<std::string>> similarOptionNames;
94 for (
auto const& longOption : longNameToOptions) {
95 if (similarStrings.
add(
"--" + longOption.first)) {
96 similarOptionNames[
"--" + longOption.first].push_back(longOption.first);
99 for (
auto const& shortOption : shortNameToOptions) {
100 if (similarStrings.
add(
"-" + shortOption.first)) {
101 for (
auto const& option : shortOption.second) {
102 similarOptionNames[
"-" + shortOption.first].push_back(option->getLongName());
106 std::string errorMessage =
"Unknown option '" + optionNameWithDashes +
"'.";
107 if (!similarOptionNames.empty()) {
109 std::vector<std::string> sortedSimilarOptionNames;
110 auto similarStringsList = similarStrings.
toList();
111 for (
auto const& s : similarStringsList) {
112 for (
auto const& longOptionName : similarOptionNames.at(s)) {
113 sortedSimilarOptionNames.push_back(longOptionName);
116 errorMessage += getHelpForSelection({}, sortedSimilarOptionNames,
"",
"##### Suggested options:");
118 STORM_LOG_THROW(
false, storm::exceptions::OptionParserException, errorMessage);
123 bool optionActive =
false;
124 bool activeOptionIsShortName =
false;
125 std::string activeOptionName =
"";
126 std::vector<std::string> argumentCache;
129 for (uint_fast64_t i = 0; i < commandLineArguments.size(); ++i) {
130 std::string
const& currentArgument = commandLineArguments[i];
136 bool isNewOption = !currentArgument.empty() && currentArgument.at(0) ==
'-';
137 if (isNewOption && optionActive) {
138 auto const& activeOptionMap = activeOptionIsShortName ? shortNameToOptions : longNameToOptions;
139 auto activeOptionIterator = activeOptionMap.find(activeOptionName);
140 bool activeOptionExpectsMandatoryArgument = activeOptionIterator != activeOptionMap.end() && !activeOptionIterator->second.empty() &&
141 argumentCache.size() < activeOptionIterator->second.front()->getArgumentCount() &&
142 !activeOptionIterator->second.front()->getArgument(argumentCache.size()).getIsOptional();
143 bool currentArgumentIsKnownOption = currentArgument.size() > 1 && currentArgument.at(1) ==
'-'
144 ? longNameToOptions.find(currentArgument.substr(2)) != longNameToOptions.end()
145 : shortNameToOptions.find(currentArgument.substr(1)) != shortNameToOptions.end();
146 if (activeOptionExpectsMandatoryArgument && !currentArgumentIsKnownOption) {
155 setOptionsArguments(activeOptionName, activeOptionIsShortName ? this->shortNameToOptions : this->longNameToOptions, argumentCache);
158 argumentCache.clear();
163 if (currentArgument.size() > 1 && currentArgument.at(1) ==
'-') {
166 std::string optionName = currentArgument.substr(2);
167 auto optionIterator = this->longNameToOptions.find(optionName);
168 if (optionIterator == this->longNameToOptions.end()) {
171 activeOptionIsShortName =
false;
172 activeOptionName = optionName;
176 std::string optionName = currentArgument.substr(1);
177 auto optionIterator = this->shortNameToOptions.find(optionName);
178 if (optionIterator == this->shortNameToOptions.end()) {
181 activeOptionIsShortName =
true;
182 activeOptionName = optionName;
184 }
else if (optionActive) {
186 argumentCache.push_back(currentArgument);
189 "Found stray argument '" << currentArgument <<
"' that is not preceeded by a matching option.");
195 setOptionsArguments(activeOptionName, activeOptionIsShortName ? this->shortNameToOptions : this->longNameToOptions, argumentCache);
205 this->finalizeAllModules();
209 std::map<std::string, std::vector<std::string>> configurationFileSettings = parseConfigFile(configFilename);
211 for (
auto const& optionArgumentsPair : configurationFileSettings) {
212 auto options = this->longNameToOptions.find(optionArgumentsPair.first);
218 for (
auto option : options->second) {
219 if (option->getHasOptionBeenSet()) {
222 STORM_LOG_WARN(
"The option '" << option->getLongName() <<
"' of module '" << option->getModuleName()
223 <<
"' has been set in the configuration file '" << configFilename
224 <<
"', but was overwritten on the command line.\n");
228 setOptionArguments(optionArgumentsPair.first, option, optionArgumentsPair.second);
233 this->finalizeAllModules();
237 std::cout <<
"usage: " << executableName <<
" [options]\n\n";
239 if (filter ==
"frequent" || filter ==
"all") {
240 bool includeAdvanced = (filter ==
"all");
242 uint_fast64_t maxLength = getPrintLengthOfLongestOption(includeAdvanced);
244 std::vector<std::string> invisibleModules;
245 uint64_t numHidden = 0;
246 for (
auto const& moduleName : this->moduleNames) {
251 if (!includeAdvanced) {
252 auto moduleIterator = moduleOptions.find(moduleName);
253 if (moduleIterator != this->moduleOptions.end()) {
254 bool allAdvanced =
true;
255 for (
auto const& option : moduleIterator->second) {
256 if (!option->getIsAdvanced()) {
262 if (!moduleIterator->second.empty() && allAdvanced) {
263 invisibleModules.push_back(moduleName);
269 if (!includeAdvanced) {
270 if (numHidden == 1) {
271 std::cout << numHidden <<
" hidden option.\n";
273 std::cout << numHidden <<
" hidden options.\n";
275 if (!invisibleModules.empty()) {
276 if (invisibleModules.size() == 1) {
277 std::cout << invisibleModules.size() <<
" hidden module (" << boost::join(invisibleModules,
", ") <<
").\n";
279 std::cout << invisibleModules.size() <<
" hidden modules (" << boost::join(invisibleModules,
", ") <<
").\n";
282 std::cout <<
"\nType '" + executableName +
" --help modulename' to display all options of a specific module.\n";
283 std::cout <<
"Type '" + executableName +
" --help all' to display a complete list of options.\n";
287 std::regex hintRegex(filter, std::regex_constants::ECMAScript | std::regex_constants::icase);
290 std::vector<std::string> matchingModuleNames;
291 for (
auto const& moduleName : this->moduleNames) {
292 if (std::regex_search(moduleName, hintRegex)) {
294 matchingModuleNames.push_back(moduleName);
300 std::vector<std::string> matchingOptionNames;
301 for (
auto const& optionName : this->longOptionNames) {
302 if (std::regex_search(optionName, hintRegex)) {
303 matchingOptionNames.push_back(optionName);
307 std::string optionList = getHelpForSelection(matchingModuleNames, matchingOptionNames,
308 "Matching modules for filter '" + filter +
"':",
"Matching options for filter '" + filter +
"':");
309 if (optionList.empty()) {
310 std::cout <<
"Filter '" << filter <<
"' did not match any modules or options.\n";
312 std::cout << optionList;
317std::string SettingsManager::getHelpForSelection(std::vector<std::string>
const& selectedModuleNames, std::vector<std::string>
const& selectedLongOptionNames,
318 std::string modulesHeader, std::string optionsHeader)
const {
319 std::stringstream stream;
322 std::set<std::shared_ptr<Option>> printedOptions;
325 uint_fast64_t maxLengthModules = 0;
326 for (
auto const& moduleName : selectedModuleNames) {
327 maxLengthModules = std::max(maxLengthModules, getPrintLengthOfLongestOption(moduleName,
true));
329 auto optionIterator = this->moduleOptions.find(moduleName);
330 STORM_LOG_ASSERT(optionIterator != this->moduleOptions.end(),
"Unable to find selected module " << moduleName <<
".");
331 printedOptions.insert(optionIterator->second.begin(), optionIterator->second.end());
335 std::vector<std::shared_ptr<Option>> matchingOptions;
336 uint_fast64_t maxLengthOptions = 0;
337 for (
auto const& optionName : selectedLongOptionNames) {
338 auto optionIterator = this->longNameToOptions.find(optionName);
339 STORM_LOG_ASSERT(optionIterator != this->longNameToOptions.end(),
"Unable to find selected option " << optionName <<
".");
340 for (
auto const& option : optionIterator->second) {
343 if (printedOptions.find(option) == printedOptions.end()) {
344 maxLengthOptions = std::max(maxLengthOptions, option->getPrintLength());
345 matchingOptions.push_back(option);
346 printedOptions.insert(option);
352 uint_fast64_t maxLength = std::max(maxLengthModules, maxLengthOptions);
353 if (selectedModuleNames.size() > 0) {
354 if (modulesHeader !=
"") {
355 stream << modulesHeader <<
'\n';
357 for (
auto const& matchingModuleName : selectedModuleNames) {
358 stream << getHelpForModule(matchingModuleName, maxLength,
true);
363 if (matchingOptions.size() > 0) {
364 if (optionsHeader !=
"") {
365 stream << optionsHeader <<
'\n';
367 for (
auto const& option : matchingOptions) {
368 stream << std::setw(maxLength) << std::left << *option <<
'\n';
375 auto moduleIterator = moduleOptions.find(moduleName);
376 if (moduleIterator == this->moduleOptions.end()) {
383 uint64_t numOfOptions = 0;
384 for (
auto const& option : moduleIterator->second) {
385 if (includeAdvanced || !option->getIsAdvanced()) {
390 std::stringstream stream;
391 if (numOfOptions > 0) {
392 std::string displayedModuleName =
"'" + moduleName +
"'";
393 if (!includeAdvanced) {
394 displayedModuleName +=
" (" + std::to_string(numOfOptions) +
"/" + std::to_string(moduleIterator->second.size()) +
" shown)";
396 stream <<
"##### Module " << displayedModuleName <<
" " << std::string(std::min(maxLength, maxLength - displayedModuleName.length() - 14),
'#') <<
'\n';
400 boost::io::ios_flags_saver out(std::cout);
402 for (
auto const& option : moduleIterator->second) {
403 if (includeAdvanced || !option->getIsAdvanced()) {
404 stream << std::setw(maxLength) << std::left << *option <<
'\n';
412uint_fast64_t SettingsManager::getPrintLengthOfLongestOption(
bool includeAdvanced)
const {
413 uint_fast64_t length = 0;
414 for (
auto const& moduleName : this->moduleNames) {
415 length = std::max(getPrintLengthOfLongestOption(moduleName, includeAdvanced), length);
420uint_fast64_t SettingsManager::getPrintLengthOfLongestOption(std::string
const& moduleName,
bool includeAdvanced)
const {
421 auto moduleIterator = modules.find(moduleName);
422 STORM_LOG_THROW(moduleIterator != modules.end(), storm::exceptions::IllegalFunctionCallException,
423 "Unable to retrieve option length of unknown module '" << moduleName <<
"'.");
424 return moduleIterator->second->getPrintLengthOfLongestOption(includeAdvanced);
428 auto moduleIterator = this->modules.find(moduleSettings->getModuleName());
429 STORM_LOG_THROW(moduleIterator == this->modules.end(), storm::exceptions::IllegalFunctionCallException,
430 "Unable to register module '" << moduleSettings->getModuleName() <<
"' because a module with the same name already exists.");
433 std::string moduleName = moduleSettings->getModuleName();
434 this->moduleNames.push_back(moduleName);
435 this->modules.emplace(moduleSettings->getModuleName(), std::move(moduleSettings));
436 auto iterator = this->modules.find(moduleName);
437 std::unique_ptr<modules::ModuleSettings>
const&
settings = iterator->second;
440 this->moduleOptions.emplace(moduleName, std::vector<std::shared_ptr<Option>>());
442 for (
auto const& option :
settings->getOptions()) {
443 this->addOption(option);
448void SettingsManager::addOption(std::shared_ptr<Option>
const& option) {
450 auto moduleOptionIterator = this->moduleOptions.find(option->getModuleName());
451 STORM_LOG_THROW(moduleOptionIterator != this->moduleOptions.end(), storm::exceptions::IllegalFunctionCallException,
452 "Cannot add option for unknown module '" << option->getModuleName() <<
"'.");
453 moduleOptionIterator->second.emplace_back(option);
458 if (!option->getRequiresModulePrefix()) {
459 bool isCompatible = storm::settings::SettingsManager::isCompatible(option, option->getLongName(), this->longNameToOptions);
460 STORM_LOG_THROW(isCompatible, storm::exceptions::IllegalFunctionCallException,
461 "Unable to add option '" << option->getLongName() <<
"', because an option with the same name is incompatible with it.");
462 addOptionToMap(option->getLongName(), option, this->longNameToOptions);
465 addOptionToMap(option->getModuleName() +
":" + option->getLongName(), option, this->longNameToOptions);
466 longOptionNames.push_back(option->getModuleName() +
":" + option->getLongName());
468 if (option->getHasShortName()) {
469 if (!option->getRequiresModulePrefix()) {
470 bool isCompatible = storm::settings::SettingsManager::isCompatible(option, option->getShortName(), this->shortNameToOptions);
471 STORM_LOG_THROW(isCompatible, storm::exceptions::IllegalFunctionCallException,
472 "Unable to add option '" << option->getLongName() <<
"', because an option with the same name is incompatible with it.");
473 addOptionToMap(option->getShortName(), option, this->shortNameToOptions);
475 addOptionToMap(option->getModuleName() +
":" + option->getShortName(), option, this->shortNameToOptions);
481 return this->moduleOptions.find(moduleName) != this->moduleOptions.end();
483 return this->modules.find(moduleName) != this->modules.end();
488 auto moduleIterator = this->modules.find(moduleName);
489 STORM_LOG_THROW(moduleIterator != this->modules.end(), storm::exceptions::IllegalFunctionCallException,
490 "Cannot retrieve unknown module '" << moduleName <<
"'.");
491 return *moduleIterator->second;
495 auto moduleIterator = this->modules.find(moduleName);
496 STORM_LOG_THROW(moduleIterator != this->modules.end(), storm::exceptions::IllegalFunctionCallException,
497 "Cannot retrieve unknown module '" << moduleName <<
"'.");
498 return *moduleIterator->second;
501bool SettingsManager::isCompatible(std::shared_ptr<Option>
const& option, std::string
const& optionName,
502 std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>>
const& optionMap) {
503 auto optionIterator = optionMap.find(optionName);
504 if (optionIterator != optionMap.end()) {
505 for (
auto const& otherOption : optionIterator->second) {
506 bool locallyCompatible = option->isCompatibleWith(*otherOption);
507 if (!locallyCompatible) {
515void SettingsManager::setOptionArguments(std::string
const& optionName, std::shared_ptr<Option> option, std::vector<std::string>
const& argumentCache) {
516 STORM_LOG_THROW(argumentCache.size() <= option->getArgumentCount(), storm::exceptions::OptionParserException,
517 "Too many arguments for option '" << optionName <<
"'.");
518 STORM_LOG_THROW(!option->getHasOptionBeenSet(), storm::exceptions::OptionParserException,
"Option '" << optionName <<
"' is set multiple times.");
521 for (uint_fast64_t i = 0;
i < argumentCache.size(); ++
i) {
522 ArgumentBase& argument = option->getArgument(i);
523 bool conversionOk = argument.setFromStringValue(argumentCache[i]);
524 STORM_LOG_THROW(conversionOk, storm::exceptions::OptionParserException,
525 "Value '" << argumentCache[i] <<
"' is invalid for argument <" << argument.getName() <<
"> of option:\n"
530 for (uint_fast64_t i = argumentCache.size(); i < option->getArgumentCount(); ++
i) {
531 ArgumentBase& argument = option->getArgument(i);
532 STORM_LOG_THROW(argument.getIsOptional(), storm::exceptions::OptionParserException,
533 "Non-optional argument <" << argument.getName() <<
"> of option:\n"
535 argument.setFromDefaultValue();
538 option->setHasOptionBeenSet();
539 if (optionName != option->getLongName() && optionName != option->getShortName() && boost::starts_with(optionName, option->getModuleName())) {
540 option->setHasOptionBeenSetWithModulePrefix();
544void SettingsManager::setOptionsArguments(std::string
const& optionName, std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>>
const& optionMap,
545 std::vector<std::string>
const& argumentCache) {
546 auto optionIterator = optionMap.find(optionName);
547 STORM_LOG_THROW(optionIterator != optionMap.end(), storm::exceptions::OptionParserException,
"Unknown option '" << optionName <<
"'.");
550 for (
auto& option : optionIterator->second) {
551 setOptionArguments(optionName, option, argumentCache);
555void SettingsManager::addOptionToMap(std::string
const& name, std::shared_ptr<Option>
const& option,
556 std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>>& optionMap) {
557 auto optionIterator = optionMap.find(name);
558 if (optionIterator == optionMap.end()) {
559 std::vector<std::shared_ptr<Option>> optionVector;
560 optionVector.push_back(option);
561 optionMap.emplace(name, optionVector);
563 optionIterator->second.push_back(option);
567void SettingsManager::finalizeAllModules() {
568 for (
auto const& nameModulePair : this->modules) {
569 nameModulePair.second->finalize();
570 nameModulePair.second->check();
574std::map<std::string, std::vector<std::string>> SettingsManager::parseConfigFile(std::string
const& filename)
const {
575 std::map<std::string, std::vector<std::string>> result;
580 bool globalScope =
true;
581 std::string activeModule =
"";
582 uint_fast64_t lineNumber = 1;
586 if (line.at(0) ==
'[') {
588 line.at(0) ==
'[' && line.find(
"]") == line.length() - 1 && line.find(
"[", 1) == line.npos, storm::exceptions::OptionParserException,
589 "Illegal module name header in configuration file '" << filename <<
" in line " << std::to_string(lineNumber)
590 <<
". Expected [<module>] where <module> is a placeholder for a known module.");
593 std::string moduleName = line.substr(1, line.length() - 2);
594 STORM_LOG_THROW(moduleName !=
"" && (moduleName ==
"global" || (this->modules.find(moduleName) != this->modules.end())),
595 storm::exceptions::OptionParserException,
596 "Module header in configuration file '" << filename <<
" in line " << std::to_string(lineNumber) <<
" refers to unknown module '"
597 << moduleName <<
".");
600 if (moduleName ==
"global") {
603 activeModule = moduleName;
609 std::size_t assignmentSignIndex = line.find(
"=");
610 bool containsAssignment =
false;
611 if (assignmentSignIndex != std::string::npos) {
612 containsAssignment =
true;
615 std::string optionName;
616 if (containsAssignment) {
617 optionName = line.substr(0, assignmentSignIndex);
623 STORM_LOG_THROW(this->longNameToOptions.find(optionName) != this->longNameToOptions.end(), storm::exceptions::OptionParserException,
624 "Option assignment in configuration file '" << filename <<
" in line " << lineNumber <<
" refers to unknown option '"
625 << optionName <<
"'.");
627 STORM_LOG_THROW(this->longNameToOptions.find(activeModule +
":" + optionName) != this->longNameToOptions.end(),
628 storm::exceptions::OptionParserException,
629 "Option assignment in configuration file '" << filename <<
" in line " << lineNumber <<
" refers to unknown option '"
630 << activeModule <<
":" << optionName <<
"'.");
633 std::string fullOptionName = (!globalScope ? activeModule +
":" :
"") + optionName;
634 STORM_LOG_WARN_COND(result.find(fullOptionName) == result.end(),
"Option '" << fullOptionName <<
"' is set in line " << lineNumber
635 <<
" of configuration file " << filename
636 <<
", but has been set before.");
640 if (containsAssignment) {
641 std::string assignedValues = line.substr(assignmentSignIndex + 1);
642 std::vector<std::string> argumentCache;
646 std::regex argumentRegex(
"\"(([^\\\\\"]|((\\\\\\\\)*\\\\\")|\\\\[^\"])*)\"|(([^ \\\\\"]|((\\\\\\\\)*\\\\\")|\\\\[^\"])+)");
647 boost::algorithm::trim_left(assignedValues);
649 while (!assignedValues.empty()) {
651 bool hasMatch = std::regex_search(assignedValues, match, argumentRegex);
655 hasMatch, storm::exceptions::OptionParserException,
656 "Parsing error in configuration file '" << filename <<
"' in line " << lineNumber <<
". Unexpected input '" << assignedValues <<
"'.");
659 std::string matchedArgument = std::string(match[0].first, match[0].second);
660 if (matchedArgument.at(0) ==
'"') {
661 matchedArgument = matchedArgument.substr(1, matchedArgument.length() - 2);
663 argumentCache.push_back(matchedArgument);
665 assignedValues = assignedValues.substr(match.length());
666 boost::algorithm::trim_left(assignedValues);
670 result.emplace(fullOptionName, argumentCache);
673 result.emplace(fullOptionName, std::vector<std::string>());
698void initializeAll(std::string
const& name, std::string
const& executableName) {
Provides the central API for the registration of command line options and parsing the options from th...
void setFromCommandLine(int const argc, char const *const argv[])
This function parses the given command line arguments and sets all registered options accordingly.
std::string getHelpForModule(std::string const &moduleName, uint_fast64_t maxLength=30, bool includeAdvanced=true) const
This function prints a help message for the specified module to the standard output.
void setFromExplodedString(std::vector< std::string > const &commandLineArguments)
This function parses the given command line arguments (represented by several strings) and sets all r...
void setFromString(std::string const &commandLineString)
This function parses the given command line arguments (represented by one big string) and sets all re...
void setFromConfigurationFile(std::string const &configFilename)
This function parses the given file and sets all registered options accordingly.
void addModule(std::unique_ptr< modules::ModuleSettings > &&moduleSettings, bool doRegister=true)
Adds a new module with the given name.
void printHelp(std::string const &filter="frequent") const
This function prints a help message to the standard output.
void handleUnknownOption(std::string const &optionName, bool isShort) const
Throws an exception with a nice error message indicating similar valid option names.
void setName(std::string const &name, std::string const &executableName)
Sets the name of the tool.
SettingsManager(SettingsManager const &)=delete
modules::ModuleSettings const & getModule(std::string const &moduleName) const
Retrieves the settings of the module with the given name.
bool hasModule(std::string const &moduleName, bool checkHidden=false) const
Checks whether the module with the given name exists.
static SettingsManager & manager()
Retrieves the only existing instance of a settings manager.
This class represents the settings for the abstraction procedures.
static const std::string moduleName
static const std::string moduleName
This is the base class of the settings for a particular module.
bool add(std::string const &string)
Adds the given string to the set of similar strings (if it is similar).
std::string toDidYouMeanString() const
Returns a "Did you mean abc?" string.
std::vector< std::string > toList() const
Gets a list of all added strings that are similar to the reference string.
#define STORM_LOG_WARN(message)
#define STORM_LOG_ASSERT(cond, message)
#define STORM_LOG_WARN_COND(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
std::basic_istream< CharT, Traits > & getline(std::basic_istream< CharT, Traits > &input, std::basic_string< CharT, Traits, Allocator > &str)
Overloaded getline function which handles different types of newline ( and \r).
void closeFile(std::ofstream &stream)
Close the given file after writing.
void openFile(std::string const &filepath, std::ofstream &filestream, bool append=false, bool silent=false)
Open the given file for writing.
storm::settings::modules::BuildSettings & mutableBuildSettings()
Retrieves the build settings in a mutable form.
storm::settings::modules::AbstractionSettings & mutableAbstractionSettings()
Retrieves the abstraction settings in a mutable form.
bool hasModule()
Returns true if the given module is registered.
SettingsType const & getModule()
Get module.
void addModule(bool doRegister=true)
Add new module to use for the settings.
void initializeAll(std::string const &name, std::string const &executableName)
Initialize the settings manager with all available modules.
SettingsManager const & manager()
Retrieves the settings manager.
SettingsManager & mutableManager()
Retrieves the settings manager.
The contract of a DD backend.