-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasyCLI.hpp
331 lines (304 loc) · 12.7 KB
/
EasyCLI.hpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* @file EasyCLI.hpp
* @author Elias Bort (zetelias@proton.me)
* @brief A simple header-only library to make it easy to create a CLI by exposing a simple API with 2 structs and 1 class.
* @remark This is a header-only library, so you don't need to compile anything. Just include this header file in your project and you're good to go!
* @version 0.2.1
* @date 2023-12-29
*
* @copyright GPLv3
*
*/
#pragma once
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#define BINDFN(fn) static_cast<CommandOutput (*)(const CommandArguments &)>(fn)
#define COMMAND_FUNCTION(name) CommandOutput name(const CommandArguments &args)
/**
* @brief A struct that contains the output of a command
* @remark You, the user, needs to use this when you write your own commands
* as CommandOutput is returned by your command function
*
*/
struct CommandOutput {
std::string out;
bool success;
};
/**
* @brief A struct that contains the parsed arguments of a command
* @remark You, the user, needs to use this when you write your own commands
* as const CommandArguments &args is passed to your command function
*/
struct CommandArguments {
std::string command;
std::vector<std::string> arguments;
std::vector<std::string> flags;
/**
* @brief Returns true if the flags vector contains the specified flag
*
* @param flag The flag to check for
* @return true if the flags vector contains the specified flag
* @return false if the flags vector does not contain the specified flag
* @remark This is a convenience function that is equivalent to `std::find(arguments.begin(), arguments.end(), argument) != arguments.end();`
* @remark The time complexity of this function is O(n), where n is the number of flags
*/
bool flags_contains(const std::string &flag) const {
return std::find(flags.begin(), flags.end(), flag) != flags.end();
}
/**
* @brief Returns true if the arguments vector contains the specified flag
*
* @param flag The flag to check for
* @return true if the arguments vector contains the specified flag
* @return false if the arguments vector does not contain the specified flag
* @remark This is a convenience function that is equivalent to `std::find(arguments.begin(), arguments.end(), argument) != arguments.end();`
* @remark The time complexity of this function is O(n), where n is the number of arguments
*/
bool arguments_contains(const std::string &argument) const {
return std::find(arguments.begin(), arguments.end(), argument) != arguments.end();
}
};
/**
* @brief Parses a string into a CommandArguments struct
* For an input value of "echo hello world -oneline"
* The CommandArguments struct would look like this:
* {
* command: "echo",
* arguments: ["hello", "world"],
* flags: ["oneline"]
* }
*
* @remark You can use this directly, but it's simpler to use the variations of Execute{...} instead
* @remark This uses Bash-like parsing, so "echo hello world" and "echo hello world" are equivalent
* @param input The string to parse into a CommandArguments struct
* @return CommandArguments The parsed CommandArguments struct
*/
inline CommandArguments ParseArgs(const std::string &input) {
CommandArguments args;
std::istringstream iss(input);
std::string token;
// Extract the command
iss >> args.command;
// Extract arguments and flags
while (iss >> token) {
if (token[0] == '-') {
// Token is a flag
args.flags.push_back(token.substr(1)); // Remove '-' and store the flag
} else {
// Token is an argument
args.arguments.push_back(token);
}
}
return args;
}
using CommandFunction = std::function<CommandOutput(const CommandArguments &)>;
using CommandMap = std::unordered_map<std::string, CommandFunction>;
/**
* @brief A class that makes it easy to create a CLI
* @remark This class is not thread-safe (yet) but it should be fine for most use cases
*/
class EasyCLI {
public:
EasyCLI() {
}
EasyCLI(const CommandMap &commands) : commands(commands) {
}
/**
* @brief Adds a command to the list of commands
*
* @param name the name of the command to add. it should not contain spaces or the command will be unreachable by user input
* @param fn the callback function to call when the command is executed. Must be of type `std::function<CommandOutput(const CommandArguments &)>`
*/
void RegisterCommand(const std::string &name, CommandFunction fn) {
commands[name] = fn;
}
/**
* @brief Adds a command to the list of commands
*
* @param name the name of the command to add. it should not contain spaces or the command will be unreachable by user input
* @param fn the callback function to call when the command is executed. Must be of type `CommandOutput (*)(const CommandArguments &)`
*/
void RegisterCommand(const std::string &name, CommandOutput (*fn)(const CommandArguments &)) {
commands[name] = fn;
}
/**
* @brief Adds a command to the list of commands
*
* @tparam T the type of the callback function to call when the command is executed. Must be of type `CommandOutput (*)(const CommandArguments &)`
* @param name the name of the command to add. it should not contain spaces or the command will be unreachable by user input
* @param c the callback function to call when the command is executed. Must be of type `CommandOutput (*)(const CommandArguments &)`
*/
template <typename T> void RegisterCommand(const std::string &name, T c) {
commands[name] = BINDFN(c);
}
/**
* @brief Executes a command from user input and returns the output
*
* @remark This is the most basic way to execute a command, but it's not the most convenient.
* @param input The user input to parse and execute
* @return CommandOutput The output of the command or an error output if the command failed.
*/
CommandOutput Execute(const std::string &input) {
CommandArguments args = ParseArgs(input);
if (commands.find(args.command) != commands.end()) {
CommandOutput out = commands[args.command](args);
return out;
}
const std::string error = "Unknown command: \"" + args.command + "\"";
return CommandOutput{error, false};
}
/**
* @brief Executes a command from user input and sends error output to a stream (std::cerr by default)
*
* @param input The user input to parse and execute
* @param error_stream The stream to send error output to. std::cerr by default
* @return CommandOutput The output of the command or an error output if the command failed.
*/
CommandOutput ExecuteWithErrStream(const std::string &input, std::ostream &error_stream = std::cerr) {
CommandArguments args = ParseArgs(input);
if (commands.find(args.command) != commands.end()) {
CommandOutput out = commands[args.command](args);
if (out.success) {
return out;
} else {
error_stream << out.out;
}
}
const std::string error = "Unknown command: \"" + args.command + "\"";
error_stream << error;
return CommandOutput{error, false};
}
/**
* @brief Executes a command from user input and sends output to a stream (std::cout by default) and error output to another stream (std::cerr by default)
*
* @param input The user input to parse and execute
* @param output_stream The stream to send output to. std::cout by default
* @param error_stream The stream to send error output to. std::cerr by default
* @return CommandOutput the output of the command or an error output if the command failed.
*/
CommandOutput ExecuteIntoStream(const std::string &input, std::ostream &output_stream = std::cout, std::ostream &error_stream = std::cerr) {
CommandArguments args = ParseArgs(input);
if (commands.find(args.command) != commands.end()) {
CommandOutput out = commands[args.command](args);
if (out.success) {
output_stream << out.out << std::endl;
return out;
} else {
error_stream << out.out << std::endl;
}
}
const std::string error = "Unknown command: \"" + args.command + "\"";
return CommandOutput{error, false};
}
/**
* @brief Execute a command without returning the output
*
* @param input The user input to parse and execute
*/
void ExecuteVoid(const std::string &input) {
CommandArguments args = ParseArgs(input);
if (commands.find(args.command) != commands.end()) {
commands[args.command](args);
}
}
/**
* @brief Execute a command and modifies a string that is passed by reference with the output of the command
*
* @param input The user input to parse and execute
* @param output The string to modify with the output of the command
*/
void ExecuteVoidIntoString(const std::string &input, std::string &output) {
CommandArguments args = ParseArgs(input);
if (commands.find(args.command) != commands.end()) {
CommandOutput out = commands[args.command](args);
output = out.out;
}
}
/**
* @brief Executes a command from user input and sends error output to a stream (std::cerr by default)
*
* @param input The user input to parse and execute
* @param error_stream The stream to send error output to. std::cerr by default
*/
void ExecuteVoidWithErrStream(const std::string &input, std::ostream &error_stream = std::cerr) {
CommandArguments args = ParseArgs(input);
if (commands.find(args.command) != commands.end()) {
CommandOutput out = commands[args.command](args);
if (!out.success) {
error_stream << out.out << std::endl;
}
}
const std::string error = "Unknown command: \"" + args.command + "\"";
error_stream << error;
}
/**
* @brief Executes a command from user input and sends output to a stream (std::cout by default) and error output to another stream (std::cerr by default)
*
* @param input The user input to parse and execute
* @param output_stream The stream to send output to. std::cout by default
* @param error_stream The stream to send error output to. std::cerr by default
*/
void ExecuteVoidIntoStream(const std::string &input, std::ostream &output_stream = std::cout, std::ostream &error_stream = std::cerr) {
CommandArguments args = ParseArgs(input);
if (commands.find(args.command) != commands.end()) {
CommandOutput output = commands[args.command](args);
if (output.success) {
output_stream << output.out << std::endl;
} else {
error_stream << output.out << std::endl;
}
}
}
/**
* @brief Gets a list of all the commands registered as a vector of strings
*
* @return A vector of strings containing all the commands
*/
std::vector<std::string> GetCommandsString() const {
std::vector<std::string> command_list;
for (const auto &command : commands) {
command_list.push_back(command.first);
}
return command_list;
}
/**
* @brief gets a list of all the commands registered as a vector of CommandFunction
*
* @return a vector of CommandFunction containing all the commands
*/
std::vector<CommandFunction> GetCommands() const {
std::vector<CommandFunction> command_list;
for (const auto &command : commands) {
command_list.push_back(command.second);
}
return command_list;
}
/**
* @brief Sugar for ExecuteVoidIntoStream that takes argv as input and not a string
*
* @param argv the argv array from main()
*/
void Run(char **argv, bool cout_output = true, bool cerr_output = true) {
if (argv[1] != nullptr) {
std::string input = "";
argv++; // Skip the first argument
while (*argv != nullptr) {
input += *argv;
input += " ";
argv++;
}
ExecuteVoidIntoStream(input);
} else {
std::cerr << "Available commands:" << std::endl;
for (const auto &command : GetCommandsString()) {
std::cerr << "\t- " << command << std::endl;
}
}
}
protected:
CommandMap commands;
};