From: Sage Weil Date: Mon, 7 Jun 2021 14:02:52 +0000 (-0400) Subject: Merge PR #41509 into master X-Git-Tag: v17.1.0~1726 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b18427da4bb3f8094fd8e0cefbf24d0eb62f7c78;p=ceph.git Merge PR #41509 into master * refs/pull/41509/head: common/cmdparse: fix CephBool validation for tell commands mgr/nfs: fix 'nfs export create' argument order common/cmdparse: emit proper json mon/MonCommands: add -- seperator to example qa/tasks/cephfs/test_nfs: fix export create test mgr: make mgr commands compat with pre-quincy mon doc/_ext/ceph_commands: handle non-positional args in docs mgr: fix reweight-by-utilization cephbool flag mon/MonCommands: convert some CephChoices to CephBool mgr/k8sevents: fix help strings pybind/mgr/mgr_module: fix help desc formatting mgr/orchestrator: clean up 'orch {daemon add,apply} rgw' args mgr/orchestrator: add end_positional to a few methods mgr/orchestrator: reformat a few methods pybind/ceph_argparse: stop parsing when we run out of positional args pybind/ceph_argparse: remove dead code pybind/mgr/mgr_module: infer non-positional args pybind/mgr/mgr_module: add separator for non-positional args command/cmdparse: use -- to separate positional from non-positional args pybind/ceph_argparse: adjust help text for non-positional args pybind/ceph_argparse: track a 'positional' property on cli args Reviewed-by: Kefu Chai --- b18427da4bb3f8094fd8e0cefbf24d0eb62f7c78 diff --cc src/common/cmdparse.cc index 6817f6454115f,9d28a5c57dbf6..d02a2b99bbd11 --- a/src/common/cmdparse.cc +++ b/src/common/cmdparse.cc @@@ -645,30 -691,47 +691,49 @@@ bool cmd_getval(const cmdmap_t& cmdmap * 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(found->second); + return true; + } catch (boost::bad_get&) { try { - val = boost::get(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(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(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; + } + } + } diff --cc src/common/cmdparse.h index 0dec47773a398,4d5160d3333b2..f6edf0cb88dfa --- a/src/common/cmdparse.h +++ b/src/common/cmdparse.h @@@ -60,50 -57,44 +60,54 @@@ struct bad_cmd_get : public std::except }; 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 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(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(found->second); + return true; + } catch (boost::bad_get&) { + throw bad_cmd_get(k, cmdmap); } - return false; } -// with default - template -bool cmd_getval( - const cmdmap_t& cmdmap, const std::string& k, - T& val, const T& defval) +std::optional cmd_getval(const cmdmap_t& cmdmap, + std::string_view k) { - if (cmdmap.count(k)) { - try { - val = boost::get(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 +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(cmdmap.find(k)->second); + } catch (boost::bad_get&) { + throw bad_cmd_get(k, cmdmap); } }