-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.cc
44 lines (34 loc) · 1.12 KB
/
command.cc
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
#include <iomanip>
#include <map>
#include <string>
#include <iostream>
#include "command.h"
namespace worklog {
std::string Command::name() const { return name_; }
std::string Command::description() const { return description_; }
Command::Action Command::action() const { return action_; }
void CommandParser::Add(Command cmd) {
commands_.emplace(cmd.name(), std::move(cmd));
}
atl::StatusOr<Command::Action> CommandParser::Parse(const std::vector<std::string>& args) {
if (args.size() <= 1) {
return atl::Status(atl::error::INVALID_ARGUMENT, "No command specified");
}
const std::string& name = args[1];
auto entry = commands_.find(name);
if (entry != commands_.end()) {
return entry->second.action();
}
return atl::Status(atl::error::NOT_FOUND, "Command not found");
}
void CommandParser::PrintHelp() const {
std::cout << "Available commands: \n";
for (const auto& entry : commands_) {
const Command& cmd = entry.second;
std::cout << " ";
std::cout << std::setw(20) << std::left << cmd.name();
std::cout << cmd.description() << "\n";
}
std::cout << "\n";
}
} // namespace worklog