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 `->`BodgeMaster-unfinished
parent
f5b0b74f94
commit
82d611b984
|
@ -53,7 +53,7 @@ namespace CLI {
|
|||
}
|
||||
|
||||
// using int here bc that's how main() is defined
|
||||
ArgumentsParser::ArgumentsParser(int argc, char* argv[], std::vector<Flag> flags, std::vector<UnpositionalArgument> unpositionalArguments, std::vector<PositionalArgument> positionalArguments) {
|
||||
ArgumentsParser::ArgumentsParser(int argc, const char* argv[], std::vector<Flag> flags, std::vector<UnpositionalArgument> unpositionalArguments, std::vector<PositionalArgument> positionalArguments) {
|
||||
this->wrongUsage = false;
|
||||
this->wrongUsageMessages = std::vector<std::string>();
|
||||
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<std::string> ArgumentsParser::getUnpositionalArgument(char shortName) {
|
||||
if (!this->argumentsByShortName.contains(shortName)) return ErrorOr<std::string>(true, ErrorCodes::UNKNOWN_KEY, std::string(""));
|
||||
if (this-wrongUsage) {
|
||||
if (this->wrongUsage) {
|
||||
if (this->argumentsByShortName[shortName]->present) return ErrorOr<std::string>(true, ErrorCodes::WRONG_USAGE, this->argumentsByShortName[shortName]->value);
|
||||
else return ErrorOr<std::string>(true, ErrorCodes::NOT_PRESENT, std::string(""));
|
||||
}
|
||||
|
@ -228,7 +229,7 @@ namespace CLI {
|
|||
|
||||
ErrorOr<std::string> ArgumentsParser::getUnpositionalArgument(std::string longName) {
|
||||
if (!this->argumentsByLongName.contains(longName)) return ErrorOr<std::string>(true, ErrorCodes::UNKNOWN_KEY, std::string(""));
|
||||
if (this-wrongUsage) {
|
||||
if (this->wrongUsage) {
|
||||
if (this->argumentsByLongName[longName]->present) return ErrorOr<std::string>(true, ErrorCodes::WRONG_USAGE, this->argumentsByLongName[longName]->value);
|
||||
else return ErrorOr<std::string>(true, ErrorCodes::NOT_PRESENT, std::string(""));
|
||||
}
|
||||
|
|
|
@ -75,7 +75,7 @@ namespace CLI {
|
|||
std::vector<std::string> wrongUsageMessages;
|
||||
|
||||
// using int here bc that's how main() is defined
|
||||
ArgumentsParser(int argc, char* argv[], std::vector<Flag> flags, std::vector<UnpositionalArgument> unpositionalArguments, std::vector<PositionalArgument> positionalArguments);
|
||||
ArgumentsParser(int argc, const char* argv[], std::vector<Flag> flags, std::vector<UnpositionalArgument> unpositionalArguments, std::vector<PositionalArgument> positionalArguments);
|
||||
~ArgumentsParser();
|
||||
|
||||
ErrorOr<bool> getFlag(char shortName);
|
||||
|
|
Loading…
Reference in New Issue