Skip to content

Commit 93167ea

Browse files
committed
params_parser: +_get_inner
gives helpful error message
1 parent c497c98 commit 93167ea

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

src/psc_bgk_util/params_parser.hxx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public:
5050

5151
// return true and display warning iff paramName is present
5252
bool warnIfPresent(const std::string paramName, const std::string advice);
53+
54+
private:
55+
/// @brief Wrapper for retrieving an unparsed value with an enhanced error
56+
/// message.
57+
/// @param paramName the name of the parameter to fetch
58+
/// @throws std::out_of_range (with an error message that specifies
59+
/// `paramName`) if the parameter is not present
60+
/// @return the unparsed parameter value (i.e., as a string)
61+
std::string _get_inner(const std::string paramName);
5362
};
5463

5564
// implementations
@@ -93,30 +102,38 @@ template <>
93102
bool ParsedParams::get<bool>(const std::string paramName)
94103
{
95104
bool b;
96-
std::istringstream(params[paramName]) >> std::boolalpha >> b;
105+
std::istringstream(_get_inner(paramName)) >> std::boolalpha >> b;
97106
return b;
98107
}
99108

100109
template <>
101110
double ParsedParams::get<double>(const std::string paramName)
102111
{
103-
return std::stod(params[paramName]);
112+
return std::stod(_get_inner(paramName));
104113
}
105114

106115
template <>
107116
int ParsedParams::get<int>(const std::string paramName)
108117
{
109-
return std::stoi(params[paramName]);
118+
return std::stoi(_get_inner(paramName));
110119
}
111120

112121
template <>
113122
float ParsedParams::get<float>(const std::string paramName)
114123
{
115-
return std::stof(params[paramName]);
124+
return std::stof(_get_inner(paramName));
116125
}
117126

118127
template <>
119128
std::string ParsedParams::get<std::string>(const std::string paramName)
120129
{
121-
return params[paramName];
130+
return _get_inner(paramName);
131+
}
132+
133+
std::string ParsedParams::_get_inner(const std::string paramName)
134+
{
135+
if (params.count(paramName) == 0) {
136+
throw std::out_of_range("missing required input parameter: " + paramName);
137+
}
138+
return params.at(paramName);
122139
}

0 commit comments

Comments
 (0)