* so earlier clients are sent a CephChoices argdesc instead, and will
* send us a "--foo-bar" value string for boolean arguments.
*/
- if (cmdmap.count(k)) {
+ auto found = cmdmap.find(k);
+ if (found == cmdmap.end()) {
+ return false;
+ }
+ try {
+ val = boost::get<bool>(found->second);
+ return true;
+ } catch (boost::bad_get&) {
try {
- val = boost::get<bool>(cmdmap.find(k)->second);
- return true;
- } catch (boost::bad_get&) {
- try {
- std::string expected = "--" + k;
- std::replace(expected.begin(), expected.end(), '_', '-');
-
- std::string v_str = boost::get<std::string>(cmdmap.find(k)->second);
- if (v_str == expected) {
- val = true;
- return true;
- } else {
- throw bad_cmd_get(k, cmdmap);
- }
- } catch (boost::bad_get&) {
- throw bad_cmd_get(k, cmdmap);
+ std::string expected{"--"};
+ expected += k;
+ std::replace(expected.begin(), expected.end(), '_', '-');
+
+ std::string v_str = boost::get<std::string>(found->second);
+ if (v_str == expected) {
+ val = true;
+ return true;
+ } else {
+ throw bad_cmd_get(k, cmdmap);
}
+ } catch (boost::bad_get&) {
+ throw bad_cmd_get(k, cmdmap);
}
}
- return false;
}
+ bool cmd_getval_compat_cephbool(
+ const cmdmap_t& cmdmap,
+ const std::string& k, bool& val)
+ {
+ try {
+ return cmd_getval(cmdmap, k, val);
+ } catch (bad_cmd_get& e) {
+ // try as legacy/compat CephChoices
+ std::string t;
+ if (!cmd_getval(cmdmap, k, t)) {
+ return false;
+ }
+ std::string expected = "--"s + k;
+ std::replace(expected.begin(), expected.end(), '_', '-');
+ val = (t == expected);
+ return true;
+ }
+ }
+
}
};
bool cmd_getval(const cmdmap_t& cmdmap,
- const std::string& k, bool& val);
+ std::string_view k, bool& val);
+ bool cmd_getval_compat_cephbool(
+ const cmdmap_t& cmdmap,
+ const std::string& k, bool& val);
+
template <typename T>
bool cmd_getval(const cmdmap_t& cmdmap,
- const std::string& k, T& val)
+ std::string_view k, T& val)
{
- if (cmdmap.count(k)) {
- try {
- val = boost::get<T>(cmdmap.find(k)->second);
- return true;
- } catch (boost::bad_get&) {
- throw bad_cmd_get(k, cmdmap);
- }
+ auto found = cmdmap.find(k);
+ if (found == cmdmap.end()) {
+ return false;
+ }
+ try {
+ val = boost::get<T>(found->second);
+ return true;
+ } catch (boost::bad_get&) {
+ throw bad_cmd_get(k, cmdmap);
}
- return false;
}
-// with default
-
template <typename T>
-bool cmd_getval(
- const cmdmap_t& cmdmap, const std::string& k,
- T& val, const T& defval)
+std::optional<T> cmd_getval(const cmdmap_t& cmdmap,
+ std::string_view k)
{
- if (cmdmap.count(k)) {
- try {
- val = boost::get<T>(cmdmap.find(k)->second);
- return true;
- } catch (boost::bad_get&) {
- throw bad_cmd_get(k, cmdmap);
- }
+ T ret;
+ if (const bool found = cmd_getval(cmdmap, k, ret); found) {
+ return std::make_optional(std::move(ret));
} else {
- val = defval;
- return true;
+ return std::nullopt;
+ }
+}
+
+// with default
+
+template <typename T, typename V>
+T cmd_getval_or(const cmdmap_t& cmdmap, std::string_view k,
+ const V& defval)
+{
+ auto found = cmdmap.find(k);
+ if (found == cmdmap.end()) {
+ return T(defval);
+ }
+ try {
+ return boost::get<T>(cmdmap.find(k)->second);
+ } catch (boost::bad_get&) {
+ throw bad_cmd_get(k, cmdmap);
}
}