From 0fffa86ba0460de24241ebd7df72b95306ad5be0 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 9 Sep 2021 11:20:18 +0200 Subject: [PATCH] Util: Deduplicate ArgParser accept value lambdas --- src/util/argparser.cpp | 215 ++++++++++++++++++++--------------------- src/util/argparser.h | 15 ++- 2 files changed, 118 insertions(+), 112 deletions(-) diff --git a/src/util/argparser.cpp b/src/util/argparser.cpp index 0c489a1..586e301 100644 --- a/src/util/argparser.cpp +++ b/src/util/argparser.cpp @@ -4,6 +4,7 @@ #include // numeric_limits #include // stod, stoi, stoul #include +#include #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::max()) { + value = static_cast(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& 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::max()) { - value = static_cast(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& 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::max()) { - value = static_cast(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& 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)); } diff --git a/src/util/argparser.h b/src/util/argparser.h index ff7cc2f..d6860b4 100644 --- a/src/util/argparser.h +++ b/src/util/argparser.h @@ -9,6 +9,8 @@ namespace Util { +using AcceptFunction = std::function; + class ArgParser final { public: ArgParser(); @@ -35,7 +37,7 @@ public: const char* usageString { nullptr }; const char* manString { nullptr }; Required requiresArgument; - std::function 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 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& value); + bool m_errorReporting { true }; bool m_exitOnFirstError { true }; bool m_stopParsingOnFirstNonOption { false };