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);