-
Notifications
You must be signed in to change notification settings - Fork 0
/
JCLI.cpp
89 lines (77 loc) · 3.03 KB
/
JCLI.cpp
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
#include <iostream>
#include "JSimple.hpp"
#include "Command.hpp"
#include "CommandList.hpp"
int main(int argc, char* argv[])
{
CommandsList CommandManager;
Print("Ji9sw's Command Line Interface\n");
Print("Type 'Help' for a list of commands\n");
if (argv[1])
{
if (CommandManager.GetCommandByName(argv[1]))
{
string Parameters;
for (int i = 2; i < argc; i++) {
Parameters = Parameters + argv[i] + " ";
}
Print(Parameters);
if (CommandManager.GetCommandByName(argv[1])->ParameterCount > 0 && Parameters == "")
{
Print("Error: This command requires " + std::to_string(CommandManager.GetCommandByName(argv[1])->ParameterCount) + " Parameters\n");
goto TopOfLoop;
}
CommandManager.GetCommandByName(argv[1])->Execute(Parameters);
Print("\n");
}
else {
Print("Error: This command does not exist\n");
}
}
while (true)
{
TopOfLoop:
string CommandInput = CreateInput("");
string Command;
string Parameters;
size_t spacePos = CommandInput.find(' ');
if (spacePos != std::string::npos) {
// Seperate the command and parameters
Command = CommandInput.substr(0, spacePos);
Parameters = CommandInput.substr(spacePos + 1);
}
else {
// No space found, the entire input is the command
Command = CommandInput;
}
if (isdigit(Command[0])) {
int commandNumber = stoi(Command);
if (commandNumber >= 0 && commandNumber < CommandManager.GetCommandCount()) {
Command = CommandManager.GetCommandByNumber(commandNumber)->Name;
if (CommandManager.GetCommandByName(Command)->ParameterCount > 0 && Parameters == "")
{
Print("Error: This command requires " + std::to_string(CommandManager.GetCommandByName(Command)->ParameterCount) + " Parameters\n");
goto TopOfLoop;
}
CommandManager.GetCommandByName(Command)->Execute(Parameters);
}
else {
Print("Error: Invalid command number\n");
continue;
}
}
else if (CommandManager.GetCommandByName(Command) != nullptr) {
if (CommandManager.GetCommandByName(Command)->ParameterCount > 0 && Parameters == "")
{
Print("Error: This command requires " + std::to_string(CommandManager.GetCommandByName(Command)->ParameterCount) + " Parameters\n");
goto TopOfLoop;
}
CommandManager.GetCommandByName(Command)->Execute(Parameters);
Print("\n");
}
else {
Print("Error: This command does not exist\n");
goto TopOfLoop;
}
}
}