Parsing errors to to status in b_manage
This commit is contained in:
@@ -6,11 +6,8 @@
|
||||
#include "common/Utils.h"
|
||||
|
||||
namespace platform {
|
||||
void CommandParser::messageError(const std::string& message)
|
||||
{
|
||||
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)
|
||||
|
||||
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)
|
||||
{
|
||||
bool finished = false;
|
||||
while (!finished) {
|
||||
@@ -29,16 +26,17 @@ namespace platform {
|
||||
oss << "): ";
|
||||
std::cout << oss.str();
|
||||
getline(std::cin, line);
|
||||
std::cout << Colors::RESET();
|
||||
line = trim(line);
|
||||
if (line.size() == 0)
|
||||
continue;
|
||||
if (line.size() == 0) {
|
||||
errorMessage = "No command";
|
||||
return { defaultCommand, 0, true };
|
||||
}
|
||||
if (all_of(line.begin(), line.end(), ::isdigit)) {
|
||||
command = defaultCommand;
|
||||
index = stoi(line);
|
||||
if (index > maxIndex || index < minIndex) {
|
||||
messageError("Index out of range");
|
||||
continue;
|
||||
errorMessage = "Index out of range";
|
||||
return { command, index, true };
|
||||
}
|
||||
finished = true;
|
||||
break;
|
||||
@@ -53,24 +51,24 @@ namespace platform {
|
||||
if (std::get<bool>(option)) {
|
||||
// The option requires a value
|
||||
if (line.size() == 0) {
|
||||
messageError("Option " + std::get<std::string>(option) + " requires a value");
|
||||
break;
|
||||
errorMessage = "Option " + std::get<std::string>(option) + " requires a value";
|
||||
return { command, index, true };
|
||||
}
|
||||
try {
|
||||
index = stoi(line);
|
||||
if (index > maxIndex || index < 0) {
|
||||
messageError("Index out of range");
|
||||
break;
|
||||
errorMessage = "Index out of range";
|
||||
return { command, index, true };
|
||||
}
|
||||
}
|
||||
catch (const std::invalid_argument& ia) {
|
||||
messageError("Invalid value: " + line);
|
||||
break;
|
||||
errorMessage = "Invalid value: " + line;
|
||||
return { command, index, true };
|
||||
}
|
||||
} else {
|
||||
if (line.size() > 0) {
|
||||
messageError("option " + std::get<std::string>(option) + " doesn't accept values");
|
||||
break;
|
||||
errorMessage = "option " + std::get<std::string>(option) + " doesn't accept values";
|
||||
return { command, index, true };
|
||||
}
|
||||
}
|
||||
command = std::get<char>(option);
|
||||
@@ -79,9 +77,10 @@ namespace platform {
|
||||
}
|
||||
}
|
||||
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 */
|
@@ -8,11 +8,12 @@ namespace platform {
|
||||
class CommandParser {
|
||||
public:
|
||||
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; };
|
||||
int getIndex() const { return index; };
|
||||
std::string getErrorMessage() const { return errorMessage; };
|
||||
private:
|
||||
void messageError(const std::string& message);
|
||||
std::string errorMessage;
|
||||
char command;
|
||||
int index;
|
||||
};
|
||||
|
@@ -224,13 +224,21 @@ namespace platform {
|
||||
};
|
||||
auto parser = CommandParser();
|
||||
while (!finished) {
|
||||
if (indexList) {
|
||||
auto [min_index, max_index] = paginator.getOffset(page);
|
||||
std::tie(option, index) = parser.parse(Colors::IGREEN(), mainOptions, 'r', min_index, max_index);
|
||||
} else {
|
||||
std::tie(option, subIndex) = parser.parse(Colors::IBLUE(), listOptions, 'r', 0, results.at(index).getJson()["results"].size() - 1);
|
||||
bool parserError = true; // force the first iteration
|
||||
while (parserError) {
|
||||
if (indexList) {
|
||||
auto [min_index, max_index] = paginator.getOffset(page);
|
||||
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) {
|
||||
case 'p':
|
||||
if (paginator.valid(index)) {
|
||||
|
@@ -58,7 +58,7 @@ int main(int argc, char** argv)
|
||||
auto partial = program.get<bool>("partial");
|
||||
auto compare = program.get<bool>("compare");
|
||||
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)
|
||||
partial = false;
|
||||
|
Reference in New Issue
Block a user