10 logger.
write(
"List of parameters:");
18 for (
int i = 1; i < argc; ++i) {
19 const std::string name = argv[i];
21 std::find_if(descs.
begin(), descs.
end(), [&name](
ArgDesc& desc) { return desc.matches(name); });
22 if (iter != descs.
end()) {
24 parseValuelessArg(*iter);
27 throw ArgError(
"Missing parameter value: " + name);
29 parseValueArg(*iter, argv[i + 1]);
33 throw ArgError(
"Unknown parameter: " + name);
53 std::string line =
"-" + arg.shortName +
", --" + arg.longName +
" ";
58 line += std::string(
max(1, 35 -
int(line.size())),
' ');
61 line +=
replaceAll(desc,
"\n",
"\n" + std::string(35,
' '));
66 void ArgParser::parseValuelessArg(
const ArgDesc& desc) {
77 void ArgParser::parseValueArg(
const ArgDesc& desc,
const std::string& value) {
90 insertArg<std::string>(desc.
shortName, value);
101 template <
typename TValue>
102 void ArgParser::insertArg(
const std::string& name,
const std::string& textValue) {
105 throw ArgError(
"Cannot parse value of parameter " + name);
107 params.insert(name, value.
value());
110 void ArgParser::throwIfUnknownArg(
const std::string& name)
const {
111 auto iter = std::find_if(descs.
begin(), descs.
end(), [&name](
const ArgDesc& desc) {
112 return desc.shortName == name;
114 if (iter == descs.
end()) {
115 throw ArgError(
"Unknown argument " + name);
INLINE std::string toString(const ArgEnum type)
@ NONE
No value after the argument.
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
constexpr INLINE T max(const T &f1, const T &f2)
#define INLINE
Macros for conditional compilation based on selected compiler.
#define NAMESPACE_SPH_END
std::string setLineBreak(const std::string &s, const Size lineWidth)
Inserts to string so that no line is longer than given limit.
std::string replaceAll(const std::string &source, const std::string &old, const std::string &s)
Replaces all occurences of string with a new string.
Exception thrown if the arguments are invalid.
void parse(const int argc, char **argv)
Parses the input arguments and stored the parsed values.
void printHelp(ILogger &logger)
Prints the help information into given logger.
ArgParser(ArrayView< const ArgDesc > args)
Creates a parser, given a set of parameter descriptors.
Object providing safe access to continuous memory of data.
INLINE Iterator< StorageType > begin()
INLINE Iterator< StorageType > end()
INLINE Iterator< StorageType > end() noexcept
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
INLINE Iterator< StorageType > begin() noexcept
void pushAll(const TIter first, const TIter last)
Exception thrown when used passes -h or –help parameter.
Interface providing generic (text, human readable) output of the program.
void write(TArgs &&... args)
Creates and logs a message by concatenating arguments.
Wrapper of type value of which may or may not be present.
INLINE Type & value()
Returns the reference to the stored value.
Logger writing messages to string stream.
std::string toString() const
Returns all written messages as a string. Messages are not erased from the logger by this.
Descriptor of a command-line argument.
std::string shortName
Short name, prefixed by single dash (e.g. -h)
Function< void()> callback
Generic callback executed when the parameter is parsed.
ArgEnum type
Type of the parameter.