lintel 0.2.0
Loading...
Searching...
No Matches
CommandParser.h
Go to the documentation of this file.
1#ifndef LINTEL_COMMANDPARSER_H
2#define LINTEL_COMMANDPARSER_H
3
4#include <algorithm>
5#include <cstdint>
6#include <map>
7#include <optional>
8#include <set>
9#include <string>
10#include <vector>
11
13
14namespace lintel {
20template<typename ENUM, ENUM undefined> class CommandParser
21{
22private:
23 std::map<std::string, Command> m_commands;
24 std::map<std::string, ENUM> m_enumCommand;
25 static constexpr int32_t m_maxCommandPerLine = 10;
26
27public:
28 CommandParser() = default;
29
31 void addCommand(const Command &command, ENUM value)
32 {
33 m_commands.insert({ command.getCommand(), command });
34 m_enumCommand.insert({ command.getCommand(), value });
35 }
36
39 {
40 m_commands.insert({ command.getCommand(), command });
41 m_enumCommand.insert({ command.getCommand(), value });
42 }
43
50 ENUM parse(const std::string &command, const std::vector<std::string> &flags)
51 {
52 auto foundEnum = m_enumCommand.find(command);
53 auto found = m_commands.find(command);
54 if (foundEnum == m_enumCommand.end() || found == m_commands.end()) { return undefined; }
55 found->second.parse(command, flags);
56 return foundEnum->second;
57 }
58
60 void printHelp(const std::string &command, std::ostream &os = std::cout)
61 {
62 auto found = m_commands.find(command);
63 if (found == m_commands.end()) {
64 os << "ERROR: " << command << " not available\n";
65 return;
66 }
67 found->second.printHelp(os);
68 os << "\n";
69 }
70
72 void printHelp(std::string_view componentName,
73 const std::string_view alias,
74 std::string_view description,
75 std::ostream &os = std::cout)
76 {
77 os << "Help Menu of " << componentName << " [" << alias << "]:\n\n";
78 os << description << "\n\n";
79 for (const auto &item : m_commands) {
80 item.second.printHelp(os);
81 os << "\n";
82 }
83 }
84
86 void printCommandList(std::set<std::string> menuAlias, std::ostream &os = std::cout)
87 {
88 os << "\n\t";
89 int32_t i = 0;
90 for (const auto &item : m_commands) {
91 i++;
92 if (i % m_maxCommandPerLine == 0) { os << "\n"; }
93 os << item.first;
94 if (i % m_maxCommandPerLine > 0) { os << "\t"; }
95 }
96 for (const auto &item : menuAlias) {
97 i++;
98 if (i % m_maxCommandPerLine == 0) { os << "\n"; }
99 os << item;
100 if (i % m_maxCommandPerLine > 0) { os << "\t"; }
101 }
102 os << "\n";
103 }
104
106 std::vector<std::string> allCommandsOf()
107 {
108 std::vector<std::string> temp;
109 std::transform(m_commands.begin(),
110 m_commands.end(),
111 std::back_inserter(temp),
112 [](const std::pair<std::string, Command> &pair) -> std::string { return pair.second.getCommand(); });
113 return temp;
114 }
115};
116
117}// namespace lintel
118#endif// LINTEL_COMMANDPARSER_H
Definition CommandParser.h:21
void printHelp(const std::string &command, std::ostream &os=std::cout)
Definition CommandParser.h:60
void printHelp(std::string_view componentName, const std::string_view alias, std::string_view description, std::ostream &os=std::cout)
Definition CommandParser.h:72
std::vector< std::string > allCommandsOf()
Definition CommandParser.h:106
void printCommandList(std::set< std::string > menuAlias, std::ostream &os=std::cout)
Definition CommandParser.h:86
void addCommand(const Command &command, ENUM value)
Definition CommandParser.h:31
ENUM parse(const std::string &command, const std::vector< std::string > &flags)
Definition CommandParser.h:50
void addCommand(Command &&command, ENUM value)
Definition CommandParser.h:38
Definition Command.h:15
Definition BaseFeature.h:6
constexpr std::string_view type_name()
Definition TypeName.h:9