scsl  1.1.1
Shimmering Clarity Standard Library
Commander.h
Go to the documentation of this file.
1 
31 #include <cstdint>
32 #include <functional>
33 #include <map>
34 #include <string>
35 #include <vector>
36 
37 
38 #ifndef SCSL_COMMANDER_H
39 #define SCSL_COMMANDER_H
40 
41 namespace scsl {
42 
43 
47 using CommanderFunc = std::function<bool (std::vector<std::string>)>;
48 
49 
54 class Subcommand {
55 public:
57  enum class Status : int8_t {
59  OK = 0,
61  NotEnoughArgs = 1,
63  Failed = 2,
67  };
68 
75  Subcommand(std::string name, size_t argc, CommanderFunc func)
76  : fn(func), requiredArgs(argc), command(name)
77  {}
78 
80  std::string Name() { return this->command; }
81 
86  Status Run(std::vector<std::string> args);
87 private:
88  CommanderFunc fn;
89  size_t requiredArgs;
90  std::string command;
91 };
92 
112 class Commander {
113 public:
116 
118  bool Register(Subcommand scmd);
119 
121  Subcommand::Status Run(std::string command, std::vector<std::string> args);
122 private:
123  std::map<std::string, Subcommand *> cmap;
124 };
125 
126 
127 } // namespace scsl
128 
129 
130 #endif //SCSL_COMMANDER_H
Subcommander manager for programs.
Definition: Commander.h:112
Subcommand::Status Run(std::string command, std::vector< std::string > args)
Try to run a subcommand registered with this Commander.
Commander()
A Commander is initialized empty.
bool Register(Subcommand scmd)
Register adds the subcommand. It will be copied into the Commander.
Subcommands used by Commander.
Definition: Commander.h:54
Status
Status describes the results of running a Subcommand.
Definition: Commander.h:57
@ Failed
The subcommand failed to run correctly.
@ NotEnoughArgs
Not enough arguments were supplied to the subcommand.
@ OK
The subcommand executed correctly.
Subcommand(std::string name, size_t argc, CommanderFunc func)
Definition: Commander.h:75
Status Run(std::vector< std::string > args)
std::string Name()
Name returns the name of this subcommand.
Definition: Commander.h:80
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:43
std::function< bool(std::vector< std::string >)> CommanderFunc
Definition: Commander.h:47