From 2ab9feb6e917468a8138452cb985d5847b25d8a3 Mon Sep 17 00:00:00 2001 From: ackimixs Date: Mon, 20 May 2024 12:58:53 +0200 Subject: [PATCH] template get option --- .../CLParser/include/Modelec/CLParser.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/components/CLParser/include/Modelec/CLParser.h b/components/CLParser/include/Modelec/CLParser.h index 2483605..49705a0 100644 --- a/components/CLParser/include/Modelec/CLParser.h +++ b/components/CLParser/include/Modelec/CLParser.h @@ -19,7 +19,24 @@ public: return defaultValue; } - return static_cast(std::stod(_options.at(option))); + try { + return static_cast(std::stod(_options.at(option))); + } catch (std::exception& e) { + return defaultValue; + } + } + + template + [[nodiscard]] std::optional getOption(const std::string& option) const { + if (!hasOption(option)) { + return std::nullopt; + } + + try { + return static_cast(std::stod(_options.at(option))); + } catch (std::exception& e) { + return std::nullopt; + } } [[nodiscard]] std::optional getOption(const std::string& option) const;