scsl  0.2.4
Shimmering Clarity Standard Library
Commander.h
Go to the documentation of this file.
1 
31 #include <functional>
32 #include <map>
33 #include <string>
34 #include <vector>
35 
36 
37 #ifndef SCSL_COMMANDER_H
38 #define SCSL_COMMANDER_H
39 
40 namespace scsl {
41 
42 
46 using CommanderFunc = std::function<bool (int, char **)>;
47 
48 
51 class Subcommand {
52 public:
54  enum class Status : uint8_t {
56  OK = 0,
58  NotEnoughArgs = 1,
60  Failed = 2,
64  };
65 
72  Subcommand(std::string name, int argc, CommanderFunc func)
73  : fn(func), args(argc), command(name)
74  {}
75 
77  std::string Name() { return this->command; }
78 
84  Status Run(int argc, char **argv);
85 private:
86  CommanderFunc fn;
87  int args;
88  std::string command;
89 };
90 
108 class Commander {
109 public:
111  Commander();
112 
114  bool Register(Subcommand scmd);
115 
117  Subcommand::Status Run(std::string command, int argc, char **argv);
118 private:
119  std::map<std::string, Subcommand *> cmap;
120 };
121 
122 
123 } // namespace scsl
124 
125 
126 #endif //SCSL_COMMANDER_H
Definition: Commander.h:108
Commander()
A Commander is initialized empty.
Definition: Commander.cc:47
Subcommand::Status Run(std::string command, int argc, char **argv)
Try to run a subcommand registered with this Commander.
Definition: Commander.cc:67
bool Register(Subcommand scmd)
Register adds the subcommand. It will be copied into the Commander.
Definition: Commander.cc:54
Definition: Commander.h:51
std::string Name()
Name returns the name of this subcommand.
Definition: Commander.h:77
Status Run(int argc, char **argv)
Definition: Commander.cc:32
Subcommand(std::string name, int argc, CommanderFunc func)
Definition: Commander.h:72
Status
Status describes the results of running a Subcommand.
Definition: Commander.h:54
@ Failed
The subcommand failed to run correctly.
@ NotEnoughArgs
Not enough arguments were supplied to the subcommand.
@ OK
The subcommand executed correctly.
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:43
std::function< bool(int, char **)> CommanderFunc
Definition: Commander.h:46