From 93167eaf5b5f48fb939b167b10dcebdc26b29739 Mon Sep 17 00:00:00 2001 From: James McClung Date: Wed, 12 Nov 2025 10:45:39 -0500 Subject: [PATCH] params_parser: +_get_inner gives helpful error message --- src/psc_bgk_util/params_parser.hxx | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/psc_bgk_util/params_parser.hxx b/src/psc_bgk_util/params_parser.hxx index 9a7134eab..767ddcb26 100644 --- a/src/psc_bgk_util/params_parser.hxx +++ b/src/psc_bgk_util/params_parser.hxx @@ -50,6 +50,15 @@ public: // return true and display warning iff paramName is present bool warnIfPresent(const std::string paramName, const std::string advice); + +private: + /// @brief Wrapper for retrieving an unparsed value with an enhanced error + /// message. + /// @param paramName the name of the parameter to fetch + /// @throws std::out_of_range (with an error message that specifies + /// `paramName`) if the parameter is not present + /// @return the unparsed parameter value (i.e., as a string) + std::string _get_inner(const std::string paramName); }; // implementations @@ -93,30 +102,38 @@ template <> bool ParsedParams::get(const std::string paramName) { bool b; - std::istringstream(params[paramName]) >> std::boolalpha >> b; + std::istringstream(_get_inner(paramName)) >> std::boolalpha >> b; return b; } template <> double ParsedParams::get(const std::string paramName) { - return std::stod(params[paramName]); + return std::stod(_get_inner(paramName)); } template <> int ParsedParams::get(const std::string paramName) { - return std::stoi(params[paramName]); + return std::stoi(_get_inner(paramName)); } template <> float ParsedParams::get(const std::string paramName) { - return std::stof(params[paramName]); + return std::stof(_get_inner(paramName)); } template <> std::string ParsedParams::get(const std::string paramName) { - return params[paramName]; + return _get_inner(paramName); +} + +std::string ParsedParams::_get_inner(const std::string paramName) +{ + if (params.count(paramName) == 0) { + throw std::out_of_range("missing required input parameter: " + paramName); + } + return params.at(paramName); } \ No newline at end of file