]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/cmdparse.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / common / cmdparse.h
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3#ifndef CEPH_COMMON_CMDPARSE_H
4#define CEPH_COMMON_CMDPARSE_H
5
7c673cae
FG
6#include <vector>
7#include <stdexcept>
20effc67 8#include <optional>
31f18b77
FG
9#include <ostream>
10#include <boost/variant.hpp>
11fdf7f2 11#include "include/ceph_assert.h" // boost clobbers this
9f95a23c 12#include "include/common_fwd.h"
7c673cae
FG
13#include "common/Formatter.h"
14#include "common/BackTrace.h"
15
7c673cae
FG
16typedef boost::variant<std::string,
17 bool,
18 int64_t,
19 double,
20 std::vector<std::string>,
c07f9fc5
FG
21 std::vector<int64_t>,
22 std::vector<double>> cmd_vartype;
11fdf7f2 23typedef std::map<std::string, cmd_vartype, std::less<>> cmdmap_t;
7c673cae 24
9f95a23c
TL
25namespace TOPNSPC::common {
26std::string cmddesc_get_prefix(const std::string_view &cmddesc);
11fdf7f2
TL
27std::string cmddesc_get_prenautilus_compat(const std::string &cmddesc);
28void dump_cmd_to_json(ceph::Formatter *f, uint64_t features,
29 const std::string& cmd);
7c673cae 30void dump_cmd_and_help_to_json(ceph::Formatter *f,
11fdf7f2 31 uint64_t features,
7c673cae
FG
32 const std::string& secname,
33 const std::string& cmd,
34 const std::string& helptext);
35void dump_cmddesc_to_json(ceph::Formatter *jf,
11fdf7f2 36 uint64_t features,
7c673cae
FG
37 const std::string& secname,
38 const std::string& cmdsig,
39 const std::string& helptext,
40 const std::string& module,
41 const std::string& perm,
7c673cae 42 uint64_t flags);
9f95a23c 43bool cmdmap_from_json(const std::vector<std::string>& cmd, cmdmap_t *mapp,
f67539c2 44 std::ostream& ss);
7c673cae 45void cmdmap_dump(const cmdmap_t &cmdmap, ceph::Formatter *f);
31f18b77 46void handle_bad_get(CephContext *cct, const std::string& k, const char *name);
7c673cae
FG
47
48std::string cmd_vartype_stringify(const cmd_vartype& v);
49
f64942e4
AA
50struct bad_cmd_get : public std::exception {
51 std::string desc;
20effc67
TL
52 bad_cmd_get(std::string_view f, const cmdmap_t& cmdmap) {
53 desc += "bad or missing field '";
54 desc += f;
55 desc += "'";
f64942e4
AA
56 }
57 const char *what() const throw() override {
58 return desc.c_str();
59 }
60};
61
9f95a23c 62bool cmd_getval(const cmdmap_t& cmdmap,
20effc67
TL
63 std::string_view k, bool& val);
64
65bool cmd_getval_compat_cephbool(
66 const cmdmap_t& cmdmap,
67 const std::string& k, bool& val);
7c673cae 68
f64942e4 69template <typename T>
9f95a23c 70bool cmd_getval(const cmdmap_t& cmdmap,
20effc67 71 std::string_view k, T& val)
f64942e4 72{
20effc67
TL
73 auto found = cmdmap.find(k);
74 if (found == cmdmap.end()) {
75 return false;
76 }
77 try {
78 val = boost::get<T>(found->second);
79 return true;
80 } catch (boost::bad_get&) {
81 throw bad_cmd_get(k, cmdmap);
f64942e4 82 }
f64942e4
AA
83}
84
7c673cae 85template <typename T>
20effc67
TL
86std::optional<T> cmd_getval(const cmdmap_t& cmdmap,
87 std::string_view k)
f64942e4 88{
20effc67
TL
89 T ret;
90 if (const bool found = cmd_getval(cmdmap, k, ret); found) {
91 return std::make_optional(std::move(ret));
f64942e4 92 } else {
20effc67
TL
93 return std::nullopt;
94 }
95}
96
97// with default
98
99template <typename T, typename V>
100T cmd_getval_or(const cmdmap_t& cmdmap, std::string_view k,
101 const V& defval)
102{
103 auto found = cmdmap.find(k);
104 if (found == cmdmap.end()) {
105 return T(defval);
106 }
107 try {
108 return boost::get<T>(cmdmap.find(k)->second);
109 } catch (boost::bad_get&) {
110 throw bad_cmd_get(k, cmdmap);
f64942e4
AA
111 }
112}
113
7c673cae
FG
114template <typename T>
115void
20effc67 116cmd_putval(CephContext *cct, cmdmap_t& cmdmap, std::string_view k, const T& val)
7c673cae 117{
20effc67 118 cmdmap.insert_or_assign(std::string{k}, val);
7c673cae 119}
31f18b77 120
11fdf7f2
TL
121bool validate_cmd(CephContext* cct,
122 const std::string& desc,
123 const cmdmap_t& cmdmap,
124 std::ostream& os);
31f18b77
FG
125extern int parse_osd_id(const char *s, std::ostream *pss);
126extern long parse_pos_long(const char *s, std::ostream *pss = NULL);
127
9f95a23c 128}
7c673cae 129#endif