]>
git.proxmox.com Git - ceph.git/blob - 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
10 #include <boost/variant.hpp>
11 #include "include/ceph_assert.h" // boost clobbers this
12 #include "include/common_fwd.h"
13 #include "common/Formatter.h"
14 #include "common/BackTrace.h"
16 typedef boost::variant
<std::string
,
20 std::vector
<std::string
>,
22 std::vector
<double>> cmd_vartype
;
23 typedef std::map
<std::string
, cmd_vartype
, std::less
<>> cmdmap_t
;
25 namespace TOPNSPC::common
{
26 std::string
cmddesc_get_prefix(const std::string_view
&cmddesc
);
27 std::string
cmddesc_get_prenautilus_compat(const std::string
&cmddesc
);
28 void dump_cmd_to_json(ceph::Formatter
*f
, uint64_t features
,
29 const std::string
& cmd
);
30 void dump_cmd_and_help_to_json(ceph::Formatter
*f
,
32 const std::string
& secname
,
33 const std::string
& cmd
,
34 const std::string
& helptext
);
35 void dump_cmddesc_to_json(ceph::Formatter
*jf
,
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
,
43 bool cmdmap_from_json(const std::vector
<std::string
>& cmd
, cmdmap_t
*mapp
,
45 void cmdmap_dump(const cmdmap_t
&cmdmap
, ceph::Formatter
*f
);
46 void handle_bad_get(CephContext
*cct
, const std::string
& k
, const char *name
);
48 std::string
cmd_vartype_stringify(const cmd_vartype
& v
);
50 struct bad_cmd_get
: public std::exception
{
52 bad_cmd_get(std::string_view f
, const cmdmap_t
& cmdmap
) {
53 desc
+= "bad or missing field '";
57 const char *what() const throw() override
{
62 bool cmd_getval(const cmdmap_t
& cmdmap
,
63 std::string_view k
, bool& val
);
65 bool cmd_getval_compat_cephbool(
66 const cmdmap_t
& cmdmap
,
67 const std::string
& k
, bool& val
);
70 bool cmd_getval(const cmdmap_t
& cmdmap
,
71 std::string_view k
, T
& val
)
73 auto found
= cmdmap
.find(k
);
74 if (found
== cmdmap
.end()) {
78 val
= boost::get
<T
>(found
->second
);
80 } catch (boost::bad_get
&) {
81 throw bad_cmd_get(k
, cmdmap
);
86 std::optional
<T
> cmd_getval(const cmdmap_t
& cmdmap
,
90 if (const bool found
= cmd_getval(cmdmap
, k
, ret
); found
) {
91 return std::make_optional(std::move(ret
));
99 template <typename T
, typename V
>
100 T
cmd_getval_or(const cmdmap_t
& cmdmap
, std::string_view k
,
103 auto found
= cmdmap
.find(k
);
104 if (found
== cmdmap
.end()) {
108 return boost::get
<T
>(cmdmap
.find(k
)->second
);
109 } catch (boost::bad_get
&) {
110 throw bad_cmd_get(k
, cmdmap
);
114 template <typename T
>
116 cmd_putval(CephContext
*cct
, cmdmap_t
& cmdmap
, std::string_view k
, const T
& val
)
118 cmdmap
.insert_or_assign(std::string
{k
}, val
);
121 bool validate_cmd(CephContext
* cct
,
122 const std::string
& desc
,
123 const cmdmap_t
& cmdmap
,
125 extern int parse_osd_id(const char *s
, std::ostream
*pss
);
126 extern long parse_pos_long(const char *s
, std::ostream
*pss
= NULL
);