]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/cmdparse.h
import ceph 16.2.7
[ceph.git] / ceph / src / common / cmdparse.h
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
6 #include <vector>
7 #include <stdexcept>
8 #include <ostream>
9 #include <boost/variant.hpp>
10 #include "include/ceph_assert.h" // boost clobbers this
11 #include "include/common_fwd.h"
12 #include "common/Formatter.h"
13 #include "common/BackTrace.h"
14
15 typedef boost::variant<std::string,
16 bool,
17 int64_t,
18 double,
19 std::vector<std::string>,
20 std::vector<int64_t>,
21 std::vector<double>> cmd_vartype;
22 typedef std::map<std::string, cmd_vartype, std::less<>> cmdmap_t;
23
24 namespace TOPNSPC::common {
25 std::string cmddesc_get_prefix(const std::string_view &cmddesc);
26 std::string cmddesc_get_prenautilus_compat(const std::string &cmddesc);
27 void dump_cmd_to_json(ceph::Formatter *f, uint64_t features,
28 const std::string& cmd);
29 void dump_cmd_and_help_to_json(ceph::Formatter *f,
30 uint64_t features,
31 const std::string& secname,
32 const std::string& cmd,
33 const std::string& helptext);
34 void dump_cmddesc_to_json(ceph::Formatter *jf,
35 uint64_t features,
36 const std::string& secname,
37 const std::string& cmdsig,
38 const std::string& helptext,
39 const std::string& module,
40 const std::string& perm,
41 uint64_t flags);
42 bool cmdmap_from_json(const std::vector<std::string>& cmd, cmdmap_t *mapp,
43 std::ostream& ss);
44 void cmdmap_dump(const cmdmap_t &cmdmap, ceph::Formatter *f);
45 void handle_bad_get(CephContext *cct, const std::string& k, const char *name);
46
47 std::string cmd_vartype_stringify(const cmd_vartype& v);
48
49 struct bad_cmd_get : public std::exception {
50 std::string desc;
51 bad_cmd_get(const std::string& f, const cmdmap_t& cmdmap) {
52 desc = "bad or missing field '" + f + "'";
53 }
54 const char *what() const throw() override {
55 return desc.c_str();
56 }
57 };
58
59 bool cmd_getval(const cmdmap_t& cmdmap,
60 const std::string& k, bool& val);
61
62 template <typename T>
63 bool cmd_getval(const cmdmap_t& cmdmap,
64 const std::string& k, T& val)
65 {
66 if (cmdmap.count(k)) {
67 try {
68 val = boost::get<T>(cmdmap.find(k)->second);
69 return true;
70 } catch (boost::bad_get&) {
71 throw bad_cmd_get(k, cmdmap);
72 }
73 }
74 return false;
75 }
76
77 // with default
78
79 template <typename T>
80 bool cmd_getval(
81 const cmdmap_t& cmdmap, const std::string& k,
82 T& val, const T& defval)
83 {
84 if (cmdmap.count(k)) {
85 try {
86 val = boost::get<T>(cmdmap.find(k)->second);
87 return true;
88 } catch (boost::bad_get&) {
89 throw bad_cmd_get(k, cmdmap);
90 }
91 } else {
92 val = defval;
93 return true;
94 }
95 }
96
97 template <typename T>
98 void
99 cmd_putval(CephContext *cct, cmdmap_t& cmdmap, const std::string& k, const T& val)
100 {
101 cmdmap[k] = val;
102 }
103
104 bool validate_cmd(CephContext* cct,
105 const std::string& desc,
106 const cmdmap_t& cmdmap,
107 std::ostream& os);
108 extern int parse_osd_id(const char *s, std::ostream *pss);
109 extern long parse_pos_long(const char *s, std::ostream *pss = NULL);
110
111 }
112 #endif