1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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. *
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. *
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 ******************************************************************************/
23 #include "cli/i2-cli.hpp"
24 #include "base/value.hpp"
25 #include "base/utility.hpp"
26 #include "base/type.hpp"
28 #include <boost/program_options.hpp>
33 std::vector<String> GetBashCompletionSuggestions(const String& type, const String& word);
34 std::vector<String> GetFieldCompletionSuggestions(const Type::Ptr& type, const String& word);
36 enum ImpersonationLevel
48 class CLICommand : public Object
51 DECLARE_PTR_TYPEDEFS(CLICommand);
53 typedef std::vector<String>(*ArgumentCompletionCallback)(const String&, const String&);
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;
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);
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);
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);
84 static boost::mutex& GetRegistryMutex();
85 static std::map<std::vector<String>, CLICommand::Ptr>& GetRegistry();
88 #define REGISTER_CLICOMMAND(name, klass) \
89 INITIALIZE_ONCE([]() { \
90 std::vector<String> vname = String(name).Split("/"); \
91 CLICommand::Register(vname, new klass()); \
96 #endif /* CLICOMMAND_H */