]> granicus.if.org Git - icinga2/blob - lib/cli/clicommand.hpp
Remove unused includes
[icinga2] / lib / cli / clicommand.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef CLICOMMAND_H
21 #define CLICOMMAND_H
22
23 #include "cli/i2-cli.hpp"
24 #include "base/value.hpp"
25 #include "base/utility.hpp"
26 #include "base/type.hpp"
27 #include <vector>
28 #include <boost/program_options.hpp>
29
30 namespace icinga
31 {
32
33 std::vector<String> GetBashCompletionSuggestions(const String& type, const String& word);
34 std::vector<String> GetFieldCompletionSuggestions(const Type::Ptr& type, const String& word);
35
36 enum ImpersonationLevel
37 {
38         ImpersonateNone,
39         ImpersonateRoot,
40         ImpersonateIcinga
41 };
42
43 /**
44  * A CLI command.
45  *
46  * @ingroup base
47  */
48 class CLICommand : public Object
49 {
50 public:
51         DECLARE_PTR_TYPEDEFS(CLICommand);
52
53         typedef std::vector<String>(*ArgumentCompletionCallback)(const String&, const String&);
54
55         virtual String GetDescription() const = 0;
56         virtual String GetShortDescription() const = 0;
57         virtual int GetMinArguments() const;
58         virtual int GetMaxArguments() const;
59         virtual bool IsHidden() const;
60         virtual bool IsDeprecated() const;
61         virtual void InitParameters(boost::program_options::options_description& visibleDesc,
62                 boost::program_options::options_description& hiddenDesc) const;
63         virtual ImpersonationLevel GetImpersonationLevel() const;
64         virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const = 0;
65         virtual std::vector<String> GetArgumentSuggestions(const String& argument, const String& word) const;
66         virtual std::vector<String> GetPositionalSuggestions(const String& word) const;
67
68         static CLICommand::Ptr GetByName(const std::vector<String>& name);
69         static void Register(const std::vector<String>& name, const CLICommand::Ptr& command);
70         static void Unregister(const std::vector<String>& name);
71
72         static bool ParseCommand(int argc, char **argv, boost::program_options::options_description& visibleDesc,
73                 boost::program_options::options_description& hiddenDesc,
74                 boost::program_options::positional_options_description& positionalDesc,
75                 boost::program_options::variables_map& vm, String& cmdname, CLICommand::Ptr& command, bool autocomplete);
76
77         static void ShowCommands(int argc, char **argv,
78                 boost::program_options::options_description *visibleDesc = nullptr,
79                 boost::program_options::options_description *hiddenDesc = nullptr,
80                 ArgumentCompletionCallback globalArgCompletionCallback = nullptr,
81                 bool autocomplete = false, int autoindex = -1);
82
83 private:
84         static boost::mutex& GetRegistryMutex();
85         static std::map<std::vector<String>, CLICommand::Ptr>& GetRegistry();
86 };
87
88 #define REGISTER_CLICOMMAND(name, klass) \
89         INITIALIZE_ONCE([]() { \
90                 std::vector<String> vname = String(name).Split("/"); \
91                 CLICommand::Register(vname, new klass()); \
92         })
93
94 }
95
96 #endif /* CLICOMMAND_H */