Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd / Shell.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_RBD_SHELL_H
5 #define CEPH_RBD_SHELL_H
6
7 #include "include/int_types.h"
8 #include <set>
9 #include <string>
10 #include <vector>
11 #include <boost/program_options.hpp>
12
13 namespace rbd {
14
15 class Shell {
16 public:
17   typedef std::vector<const char *> Arguments;
18   typedef std::vector<std::string> CommandSpec;
19
20   struct Action {
21     typedef void (*GetArguments)(boost::program_options::options_description *,
22                                  boost::program_options::options_description *);
23     typedef int (*Execute)(const boost::program_options::variables_map &);
24
25     CommandSpec command_spec;
26     CommandSpec alias_command_spec;
27     const std::string description;
28     const std::string help;
29     GetArguments get_arguments;
30     Execute execute;
31     bool visible;
32
33     template <typename Args, typename Execute>
34     Action(const std::initializer_list<std::string> &command_spec,
35            const std::initializer_list<std::string> &alias_command_spec,
36            const std::string &description, const std::string &help,
37            Args args, Execute execute, bool visible = true)
38         : command_spec(command_spec), alias_command_spec(alias_command_spec),
39           description(description), help(help), get_arguments(args),
40           execute(execute), visible(visible) {
41       Shell::get_actions().push_back(this);
42     }
43
44   };
45
46   struct SwitchArguments {
47     SwitchArguments(const std::initializer_list<std::string> &arguments) {
48       Shell::get_switch_arguments().insert(arguments.begin(), arguments.end());
49     }
50   };
51
52   int execute(const Arguments &argument);
53
54 private:
55   static std::vector<Action *>& get_actions();
56   static std::set<std::string>& get_switch_arguments();
57
58   void get_command_spec(const std::vector<std::string> &arguments,
59                         std::vector<std::string> *command_spec);
60   Action *find_action(const CommandSpec &command_spec,
61                       CommandSpec **matching_spec, bool *is_alias);
62
63   void get_global_options(boost::program_options::options_description *opts);
64
65   void print_help();
66   void print_action_help(Action *action, bool is_alias);
67   void print_unknown_action(const CommandSpec &command_spec);
68
69   void print_bash_completion(const CommandSpec &command_spec);
70   void print_bash_completion_options(
71     const boost::program_options::options_description &ops);
72 };
73
74 } // namespace rbd
75
76 #endif // CEPH_RBD_SHELL_H