Parsing errors to to status in b_manage

This commit is contained in:
2024-03-15 19:28:37 +01:00
parent fe24aa0b3e
commit 3691363b8e
4 changed files with 37 additions and 29 deletions

View File

@@ -6,11 +6,8 @@
#include "common/Utils.h" #include "common/Utils.h"
namespace platform { namespace platform {
void CommandParser::messageError(const std::string& message)
{ std::tuple<char, int, bool> CommandParser::parse(const std::string& color, const std::vector<std::tuple<std::string, char, bool>>& options, const char defaultCommand, const int minIndex, const int maxIndex)
std::cout << Colors::RED() << message << Colors::RESET() << std::endl;
}
std::pair<char, int> CommandParser::parse(const std::string& color, const std::vector<std::tuple<std::string, char, bool>>& options, const char defaultCommand, const int minIndex, const int maxIndex)
{ {
bool finished = false; bool finished = false;
while (!finished) { while (!finished) {
@@ -29,16 +26,17 @@ namespace platform {
oss << "): "; oss << "): ";
std::cout << oss.str(); std::cout << oss.str();
getline(std::cin, line); getline(std::cin, line);
std::cout << Colors::RESET();
line = trim(line); line = trim(line);
if (line.size() == 0) if (line.size() == 0) {
continue; errorMessage = "No command";
return { defaultCommand, 0, true };
}
if (all_of(line.begin(), line.end(), ::isdigit)) { if (all_of(line.begin(), line.end(), ::isdigit)) {
command = defaultCommand; command = defaultCommand;
index = stoi(line); index = stoi(line);
if (index > maxIndex || index < minIndex) { if (index > maxIndex || index < minIndex) {
messageError("Index out of range"); errorMessage = "Index out of range";
continue; return { command, index, true };
} }
finished = true; finished = true;
break; break;
@@ -53,24 +51,24 @@ namespace platform {
if (std::get<bool>(option)) { if (std::get<bool>(option)) {
// The option requires a value // The option requires a value
if (line.size() == 0) { if (line.size() == 0) {
messageError("Option " + std::get<std::string>(option) + " requires a value"); errorMessage = "Option " + std::get<std::string>(option) + " requires a value";
break; return { command, index, true };
} }
try { try {
index = stoi(line); index = stoi(line);
if (index > maxIndex || index < 0) { if (index > maxIndex || index < 0) {
messageError("Index out of range"); errorMessage = "Index out of range";
break; return { command, index, true };
} }
} }
catch (const std::invalid_argument& ia) { catch (const std::invalid_argument& ia) {
messageError("Invalid value: " + line); errorMessage = "Invalid value: " + line;
break; return { command, index, true };
} }
} else { } else {
if (line.size() > 0) { if (line.size() > 0) {
messageError("option " + std::get<std::string>(option) + " doesn't accept values"); errorMessage = "option " + std::get<std::string>(option) + " doesn't accept values";
break; return { command, index, true };
} }
} }
command = std::get<char>(option); command = std::get<char>(option);
@@ -79,9 +77,10 @@ namespace platform {
} }
} }
if (!found) { if (!found) {
messageError("I don't know " + line); errorMessage = "I don't know " + line;
return { command, index, true };
} }
} }
return { command, index }; return { command, index, false };
} }
} /* namespace platform */ } /* namespace platform */

View File

@@ -8,11 +8,12 @@ namespace platform {
class CommandParser { class CommandParser {
public: public:
CommandParser() = default; CommandParser() = default;
std::pair<char, int> parse(const std::string& color, const std::vector<std::tuple<std::string, char, bool>>& options, const char defaultCommand, const int minIndex, const int maxIndex); std::tuple<char, int, bool> parse(const std::string& color, const std::vector<std::tuple<std::string, char, bool>>& options, const char defaultCommand, const int minIndex, const int maxIndex);
char getCommand() const { return command; }; char getCommand() const { return command; };
int getIndex() const { return index; }; int getIndex() const { return index; };
std::string getErrorMessage() const { return errorMessage; };
private: private:
void messageError(const std::string& message); std::string errorMessage;
char command; char command;
int index; int index;
}; };

View File

@@ -224,13 +224,21 @@ namespace platform {
}; };
auto parser = CommandParser(); auto parser = CommandParser();
while (!finished) { while (!finished) {
if (indexList) { bool parserError = true; // force the first iteration
auto [min_index, max_index] = paginator.getOffset(page); while (parserError) {
std::tie(option, index) = parser.parse(Colors::IGREEN(), mainOptions, 'r', min_index, max_index); if (indexList) {
} else { auto [min_index, max_index] = paginator.getOffset(page);
std::tie(option, subIndex) = parser.parse(Colors::IBLUE(), listOptions, 'r', 0, results.at(index).getJson()["results"].size() - 1); std::tie(option, index, parserError) = parser.parse(Colors::IGREEN(), mainOptions, 'r', min_index, max_index);
} else {
std::tie(option, subIndex, parserError) = parser.parse(Colors::IBLUE(), listOptions, 'r', 0, results.at(index).getJson()["results"].size() - 1);
}
if (parserError) {
if (indexList)
list(parser.getErrorMessage(), Colors::RED(), index_A, index_B);
else
showIndex(index, subIndex);
}
} }
switch (option) { switch (option) {
case 'p': case 'p':
if (paginator.valid(index)) { if (paginator.valid(index)) {

View File

@@ -58,7 +58,7 @@ int main(int argc, char** argv)
auto partial = program.get<bool>("partial"); auto partial = program.get<bool>("partial");
auto compare = program.get<bool>("compare"); auto compare = program.get<bool>("compare");
if (number == 0) { if (number == 0) {
number = std::max(0, numRows() - 5); // 5 is the number of lines used by the menu & header number = std::max(0, numRows() - 6); // 6 is the number of lines used by the menu & header
} }
if (complete) if (complete)
partial = false; partial = false;