-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.h
50 lines (38 loc) · 992 Bytes
/
command.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef COMMAND_H_
#define COMMAND_H_
#include <string>
#include <vector>
#include <map>
#include <functional>
#include "atl/status.h"
#include "atl/statusor.h"
#include "worklog.h"
namespace worklog {
struct CommandContext {
std::vector<std::string> args;
worklog::Config config;
};
class Command {
public:
using Action = std::function<int(const CommandContext&)>;
Command(const std::string& name, const std::string& description,
Action action)
: name_(name), description_(description), action_(action) {}
std::string name() const;
std::string description() const;
Action action() const;
private:
const std::string name_;
const std::string description_;
const Action action_;
};
class CommandParser {
public:
void Add(Command cmd);
atl::StatusOr<Command::Action> Parse(const std::vector<std::string>& args);
void PrintHelp() const;
private:
std::map<std::string, Command> commands_;
};
} // namespace worklog
#endif // COMMAND_H_