From 82d611b9845e96222f879f10aceec9d920a28597 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Fri, 15 Jul 2022 09:45:55 +0200 Subject: [PATCH] lib/cli: various fixes - argumentWaitingForValue was never reset after the value had been retrieved - made it so the ArgumentsParser constructor can accept const char** - correct usage of substr() and actually use the correct position in the first place - fix a typo where I tried to dereference `this` with `-` instead of `->` --- src/lib/cli.cpp | 17 +++++++++-------- src/lib/cli.h++ | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/lib/cli.cpp b/src/lib/cli.cpp index a09b4c1..514211d 100644 --- a/src/lib/cli.cpp +++ b/src/lib/cli.cpp @@ -53,7 +53,7 @@ namespace CLI { } // using int here bc that's how main() is defined - ArgumentsParser::ArgumentsParser(int argc, char* argv[], std::vector flags, std::vector unpositionalArguments, std::vector positionalArguments) { + ArgumentsParser::ArgumentsParser(int argc, const char* argv[], std::vector flags, std::vector unpositionalArguments, std::vector positionalArguments) { this->wrongUsage = false; this->wrongUsageMessages = std::vector(); this->programName = std::string(argv[0]); @@ -99,7 +99,7 @@ namespace CLI { if (position==std::string::npos) { // no =value //is argument or flag? - std::string argumentName = argument.substr(2,argument.length()); + std::string argumentName = argument.substr(2,argument.length()-2); if (flagsByLongName.contains(argumentName)) { // flag flagsByLongName[argumentName]->present = true; @@ -113,8 +113,8 @@ namespace CLI { } } else { // has =value - std::string value = argument.substr(position, argument.length()); - std::string argumentName = argument.substr(2, position); + std::string value = argument.substr(position+1, argument.length()-position-1); + std::string argumentName = argument.substr(2, position-2); if (argumentsByLongName.contains(argumentName)) { argumentsByLongName[argumentName]->present = true; argumentsByLongName[argumentName]->value = value; @@ -140,12 +140,12 @@ namespace CLI { argumentWaitingForValue = argumentsByShortName[argument[i]]; } else { //assume the rest of the argv is a concatenated argument value - argumentsByShortName[argument[i]]->value = argument.substr(i+1, argument.length()); + argumentsByShortName[argument[i]]->value = argument.substr(i+1, argument.length()-i-1); break; } } else { this->wrongUsage = true; - this->wrongUsageMessages.push_back(std::string("Unknown argument or flag(s): ")+argument.substr(i, argument.length())); + this->wrongUsageMessages.push_back(std::string("Unknown argument or flag(s): ")+argument.substr(i, argument.length()-i)); // err on the side of caution to ensure that // no unwanted options get activated on programs // that deal gracefully with unrecognized command @@ -169,6 +169,7 @@ namespace CLI { } else { // value for unpositional argument argumentWaitingForValue->value = argument; + argumentWaitingForValue = nullptr; } } } @@ -217,7 +218,7 @@ namespace CLI { ErrorOr ArgumentsParser::getUnpositionalArgument(char shortName) { if (!this->argumentsByShortName.contains(shortName)) return ErrorOr(true, ErrorCodes::UNKNOWN_KEY, std::string("")); - if (this-wrongUsage) { + if (this->wrongUsage) { if (this->argumentsByShortName[shortName]->present) return ErrorOr(true, ErrorCodes::WRONG_USAGE, this->argumentsByShortName[shortName]->value); else return ErrorOr(true, ErrorCodes::NOT_PRESENT, std::string("")); } @@ -228,7 +229,7 @@ namespace CLI { ErrorOr ArgumentsParser::getUnpositionalArgument(std::string longName) { if (!this->argumentsByLongName.contains(longName)) return ErrorOr(true, ErrorCodes::UNKNOWN_KEY, std::string("")); - if (this-wrongUsage) { + if (this->wrongUsage) { if (this->argumentsByLongName[longName]->present) return ErrorOr(true, ErrorCodes::WRONG_USAGE, this->argumentsByLongName[longName]->value); else return ErrorOr(true, ErrorCodes::NOT_PRESENT, std::string("")); } diff --git a/src/lib/cli.h++ b/src/lib/cli.h++ index 9307483..bb4e8d3 100644 --- a/src/lib/cli.h++ +++ b/src/lib/cli.h++ @@ -75,7 +75,7 @@ namespace CLI { std::vector wrongUsageMessages; // using int here bc that's how main() is defined - ArgumentsParser(int argc, char* argv[], std::vector flags, std::vector unpositionalArguments, std::vector positionalArguments); + ArgumentsParser(int argc, const char* argv[], std::vector flags, std::vector unpositionalArguments, std::vector positionalArguments); ~ArgumentsParser(); ErrorOr getFlag(char shortName);