Browse Source

Util: Deduplicate ArgParser accept value lambdas

master
Riyyi 3 years ago
parent
commit
0fffa86ba0
  1. 215
      src/util/argparser.cpp
  2. 15
      src/util/argparser.h

215
src/util/argparser.cpp

@ -4,6 +4,7 @@
#include <limits> // numeric_limits
#include <string> // stod, stoi, stoul
#include <string_view>
#include <vector>
#include "util/argparser.h"
@ -286,6 +287,94 @@ bool ArgParser::parse(int argc, const char* argv[])
// -----------------------------------------
AcceptFunction ArgParser::getAcceptFunction(bool& value)
{
return [&value](const char*) -> bool {
value = true;
return true;
};
}
AcceptFunction ArgParser::getAcceptFunction(const char*& value)
{
return [&value](const char* v) -> bool {
value = v;
return true;
};
}
AcceptFunction ArgParser::getAcceptFunction(std::string& value)
{
return [&value](const char* v) -> bool {
value = v;
return true;
};
}
AcceptFunction ArgParser::getAcceptFunction(std::string_view& value)
{
return [&value](const char* v) -> bool {
value = v;
return true;
};
}
AcceptFunction ArgParser::getAcceptFunction(int& value)
{
return [&value](const char* v) -> bool {
try {
value = std::stoi(v);
return true;
}
catch (...) {
return false;
}
};
}
AcceptFunction ArgParser::getAcceptFunction(unsigned int& value)
{
return [&value](const char* v) -> bool {
unsigned long convert = 0;
try {
convert = std::stoul(v);
}
catch (...) {
return false;
}
if (convert <= std::numeric_limits<unsigned int>::max()) {
value = static_cast<unsigned int>(convert);
return true;
}
return false;
};
}
AcceptFunction ArgParser::getAcceptFunction(double& value)
{
return [&value](const char* v) -> bool {
try {
value = std::stod(v);
return true;
}
catch (...) {
return false;
}
};
}
AcceptFunction ArgParser::getAcceptFunction(std::vector<std::string>& value)
{
return [&value](const char* v) -> bool {
value.push_back(v);
return true;
};
}
// -----------------------------------------
void ArgParser::addOption(Option&& option)
{
m_options.push_back(option);
@ -300,10 +389,7 @@ void ArgParser::addOption(bool& value, char shortName, const char* longName, con
usageString,
manString,
Required::No,
[&value](const char*) -> bool {
value = true;
return true;
}
getAcceptFunction(value),
};
addOption(std::move(option));
}
@ -317,10 +403,7 @@ void ArgParser::addOption(const char*& value, char shortName, const char* longNa
usageString,
manString,
requiresArgument,
[&value](const char* a) -> bool {
value = a;
return true;
}
getAcceptFunction(value),
};
addOption(std::move(option));
}
@ -334,10 +417,7 @@ void ArgParser::addOption(std::string& value, char shortName, const char* longNa
usageString,
manString,
requiresArgument,
[&value](const char* a) -> bool {
value = a;
return true;
}
getAcceptFunction(value),
};
addOption(std::move(option));
}
@ -351,10 +431,7 @@ void ArgParser::addOption(std::string_view& value, char shortName, const char* l
usageString,
manString,
requiresArgument,
[&value](const char* a) -> bool {
value = a;
return true;
}
getAcceptFunction(value),
};
addOption(std::move(option));
}
@ -368,15 +445,7 @@ void ArgParser::addOption(int& value, char shortName, const char* longName, cons
usageString,
manString,
requiresArgument,
[&value](const char* a) -> bool {
try {
value = std::stoi(a);
return true;
}
catch (...) {
return false;
}
}
getAcceptFunction(value),
};
addOption(std::move(option));
}
@ -390,22 +459,7 @@ void ArgParser::addOption(unsigned int& value, char shortName, const char* longN
usageString,
manString,
requiresArgument,
[&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;
}
getAcceptFunction(value),
};
addOption(std::move(option));
}
@ -419,15 +473,7 @@ void ArgParser::addOption(double& value, char shortName, const char* longName, c
usageString,
manString,
requiresArgument,
[&value](const char* a) -> bool {
try {
value = std::stod(a);
return true;
}
catch (...) {
return false;
}
}
getAcceptFunction(value),
};
addOption(std::move(option));
}
@ -441,10 +487,7 @@ void ArgParser::addOption(std::vector<std::string>& values, char shortName, cons
usageString,
manString,
requiresArgument,
[&values](const char* a) -> bool {
values.push_back(a);
return true;
}
getAcceptFunction(values),
};
addOption(std::move(option));
}
@ -466,10 +509,7 @@ void ArgParser::addArgument(bool& value, const char* name, const char* usageStri
minValues,
1,
0,
[&value](const char*) -> bool {
value = true;
return true;
}
getAcceptFunction(value),
};
addArgument(std::move(argument));
}
@ -484,10 +524,7 @@ void ArgParser::addArgument(const char*& value, const char* name, const char* us
minValues,
1,
0,
[&value](const char* a) -> bool {
value = a;
return true;
}
getAcceptFunction(value),
};
addArgument(std::move(argument));
}
@ -502,10 +539,7 @@ void ArgParser::addArgument(std::string& value, const char* name, const char* us
minValues,
1,
0,
[&value](const char* a) -> bool {
value = a;
return true;
}
getAcceptFunction(value),
};
addArgument(std::move(argument));
}
@ -520,10 +554,7 @@ void ArgParser::addArgument(std::string_view& value, const char* name, const cha
minValues,
1,
0,
[&value](const char* a) -> bool {
value = a;
return true;
}
getAcceptFunction(value),
};
addArgument(std::move(argument));
}
@ -538,20 +569,11 @@ void ArgParser::addArgument(int& value, const char* name, const char* usageStrin
minValues,
1,
0,
[&value](const char* a) -> bool {
try {
value = std::stoi(a);
return true;
}
catch (...) {
return false;
}
}
getAcceptFunction(value),
};
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;
@ -562,27 +584,11 @@ void ArgParser::addArgument(unsigned int& value, const char* name, const char* u
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;
}
getAcceptFunction(value),
};
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;
@ -593,15 +599,7 @@ void ArgParser::addArgument(double& value, const char* name, const char* usageSt
minValues,
1,
0,
[&value](const char* a) -> bool {
try {
value = std::stod(a);
return true;
}
catch (...) {
return false;
}
}
getAcceptFunction(value),
};
addArgument(std::move(argument));
}
@ -616,10 +614,7 @@ void ArgParser::addArgument(std::vector<std::string>& values, const char* name,
minValues,
values.max_size(),
0,
[&values](const char* a) -> bool {
values.push_back(a);
return true;
}
getAcceptFunction(values),
};
addArgument(std::move(argument));
}

15
src/util/argparser.h

@ -9,6 +9,8 @@
namespace Util {
using AcceptFunction = std::function<bool(const char*)>;
class ArgParser final {
public:
ArgParser();
@ -35,7 +37,7 @@ public:
const char* usageString { nullptr };
const char* manString { nullptr };
Required requiresArgument;
std::function<bool(const char*)> acceptValue;
AcceptFunction acceptValue;
Error error = Error::None;
};
@ -47,7 +49,7 @@ public:
size_t minValues { 0 };
size_t maxValues { 1 };
size_t addedValues { 0 };
std::function<bool(const char*)> acceptValue;
AcceptFunction acceptValue;
};
bool parse(int argc, const char* argv[]);
@ -83,6 +85,15 @@ private:
bool parseLongOption(std::string_view option, std::string_view next);
bool parseArgument(std::string_view argument);
AcceptFunction getAcceptFunction(bool& value);
AcceptFunction getAcceptFunction(const char*& value);
AcceptFunction getAcceptFunction(std::string& value);
AcceptFunction getAcceptFunction(std::string_view& value);
AcceptFunction getAcceptFunction(int& value);
AcceptFunction getAcceptFunction(unsigned int& value);
AcceptFunction getAcceptFunction(double& value);
AcceptFunction getAcceptFunction(std::vector<std::string>& value);
bool m_errorReporting { true };
bool m_exitOnFirstError { true };
bool m_stopParsingOnFirstNonOption { false };

Loading…
Cancel
Save