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

15
src/util/argparser.h

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

Loading…
Cancel
Save