scsl 0.1.1
Shimmering Clarity Standard Library
Commander.h
Go to the documentation of this file.
1
15
16#include <map>
17#include <functional>
18#include <string>
19#include <vector>
20
21
22#ifndef SCSL_COMMANDER_H
23#define SCSL_COMMANDER_H
24
25namespace scsl {
26
27
31typedef std::function<bool(int, char **)> CommanderFunc;
32
33
37public:
39 enum class Status : uint8_t {
41 OK = 0,
43 NotEnoughArgs = 1,
45 Failed = 2,
48 CommandNotRegistered = 3,
49 };
50
57 Subcommand(std::string name, int argc, CommanderFunc func)
58 : fn(func), args(argc), command(name)
59 {}
60
62 std::string Name() { return this->command; }
63
69 Status Run(int argc, char **argv);
70private:
72 int args;
73 std::string command;
74};
75
93class Commander {
94public:
96 Commander();
97
99 bool Register(Subcommand scmd);
100
102 Subcommand::Status Run(std::string command, int argc, char **argv);
103private:
104 std::map<std::string, Subcommand *> cmap;
105};
106
107} // scsl
108
109
110#endif //SCSL_COMMANDER_H
Definition: Commander.h:93
Commander()
A Commander is initialized empty.
Definition: Commander.cc:29
Subcommand::Status Run(std::string command, int argc, char **argv)
Try to run a subcommand registered with this Commander.
Definition: Commander.cc:49
bool Register(Subcommand scmd)
Register adds the subcommand. It will be copied into the Commander.
Definition: Commander.cc:36
Definition: Commander.h:36
std::string Name()
Name returns the name of this subcommand.
Definition: Commander.h:62
Status Run(int argc, char **argv)
Definition: Commander.cc:14
Subcommand(std::string name, int argc, CommanderFunc func)
Definition: Commander.h:57
Status
Status describes the results of running a Subcommand.
Definition: Commander.h:39
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:37
std::function< bool(int, char **)> CommanderFunc
Definition: Commander.h:31