From b786384f24354320d56ce5ad5a816ab3660fc9c8 Mon Sep 17 00:00:00 2001 From: ackimixs Date: Mon, 20 May 2024 12:50:11 +0200 Subject: [PATCH] pi | template get option --- components/CLParser/include/Modelec/CLParser.h | 9 +++++++++ components/Utils/include/Modelec/Utils.h | 2 ++ example/main.cpp | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/components/CLParser/include/Modelec/CLParser.h b/components/CLParser/include/Modelec/CLParser.h index 4cee155..2483605 100644 --- a/components/CLParser/include/Modelec/CLParser.h +++ b/components/CLParser/include/Modelec/CLParser.h @@ -13,6 +13,15 @@ public: [[nodiscard]] bool hasOption(const std::string& option) const; + template + [[nodiscard]] T getOption(const std::string& option, T defaultValue) const { + if (!hasOption(option)) { + return defaultValue; + } + + return static_cast(std::stod(_options.at(option))); + } + [[nodiscard]] std::optional getOption(const std::string& option) const; [[nodiscard]] std::string getOption(const std::string& option, const std::string& defaultValue) const; diff --git a/components/Utils/include/Modelec/Utils.h b/components/Utils/include/Modelec/Utils.h index 06b9ce3..3d395c6 100644 --- a/components/Utils/include/Modelec/Utils.h +++ b/components/Utils/include/Modelec/Utils.h @@ -4,6 +4,8 @@ #include namespace Modelec { + #define PI 3.14159265358979323846 + bool startWith(const std::string& str, const std::string& start); bool endsWith(const std::string& str, const std::string& end); diff --git a/example/main.cpp b/example/main.cpp index 444ca05..e7126cf 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -7,7 +7,7 @@ int main(int argc, char* argv[]) { CLParser parser(argc, argv); - int port = std::stoi(parser.getOption("port", "8080")); + int port = parser.getOption("port", 8080); bool loggerMode = parser.hasOption("logger");