Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Option.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <iomanip>
5#include <string>
6#include "Argument.h"
7#include "ArgumentBase.h"
8
12
13namespace storm {
14namespace settings {
15
16Option::Option(std::string const& moduleName, std::string const& longOptionName, std::string const& optionDescription, bool isOptionRequired,
17 bool requireModulePrefix, bool isAdvanced, std::vector<std::shared_ptr<ArgumentBase>> const& optionArguments)
18 : Option(moduleName, longOptionName, "", false, optionDescription, isOptionRequired, requireModulePrefix, isAdvanced, optionArguments) {
19 // Intentionally left empty.
20}
21
22Option::Option(std::string const& moduleName, std::string const& longOptionName, std::string const& shortOptionName, std::string const& optionDescription,
23 bool isOptionRequired, bool requireModulePrefix, bool isAdvanced, std::vector<std::shared_ptr<ArgumentBase>> const& optionArguments)
24 : Option(moduleName, longOptionName, shortOptionName, true, optionDescription, isOptionRequired, requireModulePrefix, isAdvanced, optionArguments) {
25 // Intentionally left empty.
26}
27
29 STORM_LOG_THROW(this->getArgumentCount() == other.getArgumentCount(), storm::exceptions::OptionUnificationException,
30 "Unable to unify two options, because their argument count differs.");
31
32 for (size_t i = 0; i != this->arguments.size(); i++) {
33 ArgumentBase const& firstArgument = this->getArgument(i);
34 ArgumentBase const& secondArgument = other.getArgument(i);
35
36 STORM_LOG_THROW(firstArgument.getType() == secondArgument.getType(), storm::exceptions::OptionUnificationException,
37 "Unable to unify two options, because their arguments are incompatible.");
38
39 switch (firstArgument.getType()) {
43 break;
47 break;
51 break;
55 break;
59 break;
60 }
61 }
62 return true;
63}
64
66 return this->arguments.size();
67}
68
70 STORM_LOG_THROW(argumentIndex < this->getArgumentCount(), storm::exceptions::IllegalArgumentException, "Index of argument is out of bounds.");
71 return *this->arguments.at(argumentIndex);
72}
73
75 STORM_LOG_THROW(argumentIndex < this->getArgumentCount(), storm::exceptions::IllegalArgumentException, "Index of argument is out of bounds.");
76 return *this->arguments.at(argumentIndex);
77}
78
79ArgumentBase const& Option::getArgumentByName(std::string const& argumentName) const {
80 auto argumentIterator = this->argumentNameMap.find(argumentName);
81 STORM_LOG_THROW(argumentIterator != this->argumentNameMap.end(), storm::exceptions::IllegalArgumentException,
82 "Unable to retrieve argument with unknown name '" << argumentName << "'.");
83 return *argumentIterator->second;
84}
85
87 auto argumentIterator = this->argumentNameMap.find(argumentName);
88 STORM_LOG_THROW(argumentIterator != this->argumentNameMap.end(), storm::exceptions::IllegalArgumentException,
89 "Unable to retrieve argument with unknown name '" << argumentName << "'.");
90 return *argumentIterator->second;
91}
92
93std::string const& Option::getLongName() const {
94 return this->longName;
95}
96
98 return this->hasShortName;
99}
100
101std::string const& Option::getShortName() const {
102 return this->shortName;
103}
104
105std::string const& Option::getDescription() const {
106 return this->description;
107}
108
109std::string const& Option::getModuleName() const {
110 return this->moduleName;
111}
112
114 return this->isRequired;
115}
116
118 return this->requireModulePrefix;
119}
120
122 return this->isAdvanced;
123}
124
126 return this->hasBeenSet;
127}
128
130 return this->hasBeenSetWithModulePrefix;
131}
132
133Option::Option(std::string const& moduleName, std::string const& longOptionName, std::string const& shortOptionName, bool hasShortOptionName,
134 std::string const& optionDescription, bool isOptionRequired, bool requireModulePrefix, bool isAdvanced,
135 std::vector<std::shared_ptr<ArgumentBase>> const& optionArguments)
136 : longName(longOptionName),
137 hasShortName(hasShortOptionName),
138 shortName(shortOptionName),
139 description(optionDescription),
140 moduleName(moduleName),
141 isRequired(isOptionRequired),
142 requireModulePrefix(requireModulePrefix),
143 isAdvanced(isAdvanced),
144 hasBeenSet(false),
145 hasBeenSetWithModulePrefix(false),
146 arguments(optionArguments),
147 argumentNameMap() {
148 // First, do some sanity checks.
149 STORM_LOG_THROW(!longName.empty(), storm::exceptions::IllegalArgumentException, "Unable to construct option with empty name.");
150 STORM_LOG_THROW(!moduleName.empty(), storm::exceptions::IllegalArgumentException, "Unable to construct option with empty module name.");
151
153 std::find_if(longName.begin(), longName.end(), [](char c) { return !(std::isalpha(c) || std::isdigit(c) || c == '-' || c == '_'); }) != longName.end();
154 STORM_LOG_THROW(!longNameContainsIllegalCharacter, storm::exceptions::IllegalArgumentException,
155 "Unable to construct option with illegal long name '" << longName << "'.");
156
157 bool shortNameContainsIllegalCharacter = std::find_if(shortName.begin(), shortName.end(), [](char c) {
158 return !(std::isalpha(c) || std::isdigit(c) || c == 'c' || c == '_');
159 }) != shortName.end();
160 STORM_LOG_THROW(!shortNameContainsIllegalCharacter, storm::exceptions::IllegalArgumentException,
161 "Unable to construct option with illegal short name '" << shortName << "'.");
162
163 // Then index all arguments.
164 for (auto const& argument : arguments) {
165 argumentNameMap.emplace(argument->getName(), argument);
166 }
167}
168
169void Option::setHasOptionBeenSet(bool newValue) {
170 this->hasBeenSet = newValue;
171}
172
173void Option::setHasOptionBeenSetWithModulePrefix(bool newValue) {
174 this->hasBeenSetWithModulePrefix = newValue;
175}
176
179 if (!this->getRequiresModulePrefix()) {
180 length += 2;
181 }
182 length += this->getModuleName().length() + 1;
183 length += this->getLongName().length();
184 if (this->getHasShortName()) {
185 length += this->getShortName().length() + 3;
186 }
187
188 if (this->getArgumentCount() > 0) {
189 for (auto const& argument : this->getArguments()) {
190 length += argument->getName().size() + 3;
191 }
192 }
193 return length;
194}
195
196std::vector<std::shared_ptr<ArgumentBase>> const& Option::getArguments() const {
197 return this->arguments;
198}
199
200std::ostream& operator<<(std::ostream& out, Option const& option) {
201 uint_fast64_t width = static_cast<uint_fast64_t>(out.width());
202
204 out << std::setw(0) << "--";
206 if (!option.getRequiresModulePrefix()) {
207 out << "[";
209 }
210 out << option.getModuleName() << ":";
211 charactersPrinted += option.getModuleName().length() + 1;
212 if (!option.getRequiresModulePrefix()) {
213 out << "]";
215 }
216 out << option.getLongName();
217 charactersPrinted += option.getLongName().length();
218 if (option.getHasShortName()) {
219 out << " (" << option.getShortName() << ")";
220 charactersPrinted += option.getShortName().length() + 3;
221 }
222
223 if (option.getArgumentCount() > 0) {
224 for (auto const& argument : option.getArguments()) {
225 out << " <" << argument->getName() << ">";
226 charactersPrinted += argument->getName().size() + 3;
227 }
228 }
229
230 // Now fill the width.
231 for (uint_fast64_t i = charactersPrinted; i < width; ++i) {
232 if (i == charactersPrinted) {
233 out << " ";
234 } else {
235 out << ".";
236 }
237 }
238
239 out << " " << option.getDescription();
240
241 for (auto const& argument : option.getArguments()) {
242 out << " " << *argument;
243 }
244
245 return out;
246}
247} // namespace settings
248} // namespace storm
This class serves as the (untemplated) base class of argument classes.
This class subclasses the argument base to actually implement the pure virtual functions.
Definition Argument.h:32
This class represents one command-line option.
Definition Option.h:27
bool getHasShortName() const
Retrieves whether this option has a short name.
Definition Option.cpp:97
std::string const & getModuleName() const
Retrieves the name of the module to which this option belongs.
Definition Option.cpp:109
std::string const & getDescription() const
Retrieves the description of the option.
Definition Option.cpp:105
uint_fast64_t getArgumentCount() const
Retrieves the argument count this option expects.
Definition Option.cpp:65
ArgumentBase const & getArgumentByName(std::string const &argumentName) const
Returns a reference to the argument with the specified long name.
Definition Option.cpp:79
bool getHasOptionBeenSetWithModulePrefix() const
Retrieves whether the option has been set by including the module prefix.
Definition Option.cpp:129
std::string const & getShortName() const
Retrieves the short name of this option.
Definition Option.cpp:101
std::vector< std::shared_ptr< ArgumentBase > > const & getArguments() const
Retrieves the arguments of the option.
Definition Option.cpp:196
bool getIsAdvanced() const
Retrieves whether the option is only displayed in the advanced help.
Definition Option.cpp:121
ArgumentBase const & getArgument(uint_fast64_t argumentIndex) const
Retrieves the i-th argument of this option.
Definition Option.cpp:69
std::string const & getLongName() const
Retrieves the long name of this option.
Definition Option.cpp:93
uint_fast64_t getPrintLength() const
Retrieves the (print) length of the option.
Definition Option.cpp:177
bool getRequiresModulePrefix() const
Retrieves whether the option requires the module name as a prefix.
Definition Option.cpp:117
Option(std::string const &moduleName, std::string const &longOptionName, std::string const &optionDescription, bool isOptionRequired, bool requireModulePrefix, bool isAdvanced, std::vector< std::shared_ptr< ArgumentBase > > const &optionArguments=std::vector< std::shared_ptr< ArgumentBase > >())
Creates an option with the given parameters.
Definition Option.cpp:16
bool getIsRequired() const
Retrieves whether the option is required.
Definition Option.cpp:113
bool getHasOptionBeenSet() const
Retrieves whether the option has been set.
Definition Option.cpp:125
bool isCompatibleWith(Option const &other)
Checks whether the given option is compatible with the current one.
Definition Option.cpp:28
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
SettingsType const & getModule()
Get module.
std::ostream & operator<<(std::ostream &out, ArgumentBase const &argument)
LabParser.cpp.
Definition cli.cpp:18