Browse Source

Util: Add more ArgParser argument types

master
Riyyi 3 years ago
parent
commit
9cecb8602f
  1. 132
      src/util/argparser.cpp
  2. 6
      src/util/argparser.h

132
src/util/argparser.cpp

@ -456,6 +456,42 @@ void ArgParser::addArgument(Argument&& argument)
m_arguments.push_back(argument);
}
void ArgParser::addArgument(bool& value, const char* name, const char* usageString, const char* manString, Required required)
{
size_t minValues = required == Required::Yes ? 1 : 0;
Argument argument {
name,
usageString,
manString,
minValues,
1,
0,
[&value](const char*) -> bool {
value = true;
return true;
}
};
addArgument(std::move(argument));
}
void ArgParser::addArgument(const char*& value, const char* name, const char* usageString, const char* manString, Required required)
{
size_t minValues = required == Required::Yes ? 1 : 0;
Argument argument {
name,
usageString,
manString,
minValues,
1,
0,
[&value](const char* a) -> bool {
value = a;
return true;
}
};
addArgument(std::move(argument));
}
void ArgParser::addArgument(std::string& value, const char* name, const char* usageString, const char* manString, Required required)
{
size_t minValues = required == Required::Yes ? 1 : 0;
@ -474,6 +510,102 @@ void ArgParser::addArgument(std::string& value, const char* name, const char* us
addArgument(std::move(argument));
}
void ArgParser::addArgument(std::string_view& value, const char* name, const char* usageString, const char* manString, Required required)
{
size_t minValues = required == Required::Yes ? 1 : 0;
Argument argument {
name,
usageString,
manString,
minValues,
1,
0,
[&value](const char* a) -> bool {
value = a;
return true;
}
};
addArgument(std::move(argument));
}
void ArgParser::addArgument(int& value, const char* name, const char* usageString, const char* manString, Required required)
{
size_t minValues = required == Required::Yes ? 1 : 0;
Argument argument {
name,
usageString,
manString,
minValues,
1,
0,
[&value](const char* a) -> bool {
try {
value = std::stoi(a);
return true;
}
catch (...) {
return false;
}
}
};
addArgument(std::move(argument));
}
void ArgParser::addArgument(unsigned int& value, const char* name, const char* usageString, const char* manString, Required required)
{
size_t minValues = required == Required::Yes ? 1 : 0;
Argument argument {
name,
usageString,
manString,
minValues,
1,
0,
[&value](const char* a) -> bool {
unsigned long convert = 0;
try {
convert = std::stoul(a);
}
catch (...) {
return false;
}
if (convert <= std::numeric_limits<unsigned int>::max()) {
value = static_cast<unsigned int>(convert);
return true;
}
return false;
}
};
addArgument(std::move(argument));
}
void ArgParser::addArgument(double& value, const char* name, const char* usageString, const char* manString, Required required)
{
size_t minValues = required == Required::Yes ? 1 : 0;
Argument argument {
name,
usageString,
manString,
minValues,
1,
0,
[&value](const char* a) -> bool {
try {
value = std::stod(a);
return true;
}
catch (...) {
return false;
}
}
};
addArgument(std::move(argument));
}
void ArgParser::addArgument(std::vector<std::string>& values, const char* name, const char* usageString, const char* manString, Required required)
{
size_t minValues = required == Required::Yes ? 1 : 0;

6
src/util/argparser.h

@ -63,7 +63,13 @@ public:
void addOption(std::vector<std::string>& values, char shortName, const char* longName, const char* usageString, const char* manString, const char* argumentName = "", Required requiresArgument = Required::No);
void addArgument(Argument&& argument);
void addArgument(bool& value, const char* name, const char* usageString, const char* manString, Required required = Required::No);
void addArgument(const char*& value, const char* name, const char* usageString, const char* manString, Required required = Required::No);
void addArgument(std::string& value, const char* name, const char* usageString, const char* manString, Required required = Required::No);
void addArgument(std::string_view& value, const char* name, const char* usageString, const char* manString, Required required = Required::No);
void addArgument(int& value, const char* name, const char* usageString, const char* manString, Required required = Required::No);
void addArgument(unsigned int& value, const char* name, const char* usageString, const char* manString, Required required = Required::No);
void addArgument(double& value, const char* name, const char* usageString, const char* manString, Required required = Required::No);
void addArgument(std::vector<std::string>& values, const char* name, const char* usageString, const char* manString, Required required = Required::No);
void setErrorReporting(bool state) { m_errorReporting = state; }

Loading…
Cancel
Save