comparison irccdctl/cli.hpp @ 344:4665fffff6f2

Irccdctl: new cli interface
author David Demelier <markand@malikania.fr>
date Sat, 12 Nov 2016 22:58:48 +0100
parents
children 006452e4a997
comparison
equal deleted inserted replaced
343:3d927ba4af75 344:4665fffff6f2
1 /*
2 * cli.hpp -- command line for irccdctl
3 *
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef IRCCD_CLI_HPP
20 #define IRCCD_CLI_HPP
21
22 #include <string>
23 #include <vector>
24
25 #include <json.hpp>
26
27 namespace irccd {
28
29 class Irccdctl;
30
31 class Cli {
32 protected:
33 std::string m_name;
34 std::string m_summary;
35 std::string m_usage;
36 std::string m_help;
37
38 /**
39 * Check the message result and print an error if any.
40 *
41 * \param result the result
42 * \throw an error if any
43 */
44 void check(const nlohmann::json &result);
45
46 /**
47 * Send a request and wait for the response.
48 *
49 * \param irccdctl the client
50 * \param args the optional arguments
51 */
52 nlohmann::json request(Irccdctl &irccdctl, nlohmann::json args = nullptr);
53
54 /**
55 * Call a command and wait for success/error response.
56 *
57 * \param irccdctl the client
58 * \param args the extra arguments (may be null)
59 */
60 void call(Irccdctl &irccdctl, nlohmann::json args);
61
62 public:
63 inline Cli(std::string name, std::string summary, std::string usage, std::string help) noexcept
64 : m_name(std::move(name))
65 , m_summary(std::move(summary))
66 , m_usage(std::move(usage))
67 , m_help(std::move(help))
68 {
69 }
70
71 virtual ~Cli() = default;
72
73 inline const std::string &name() const noexcept
74 {
75 return m_name;
76 }
77
78 inline const std::string &summary() const noexcept
79 {
80 return m_summary;
81 }
82
83 inline const std::string &usage() const noexcept
84 {
85 return m_usage;
86 }
87
88 inline const std::string &help() const noexcept
89 {
90 return m_help;
91 }
92
93 virtual void exec(Irccdctl &client, const std::vector<std::string> &args) = 0;
94 };
95
96 } // !irccd
97
98 #endif // !IRCCD_CLI_HPP