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
|
// 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->wrongUsage = false;
|
||||||
this->wrongUsageMessages = std::vector<std::string>();
|
this->wrongUsageMessages = std::vector<std::string>();
|
||||||
this->programName = std::string(argv[0]);
|
this->programName = std::string(argv[0]);
|
||||||
|
@ -99,7 +99,7 @@ namespace CLI {
|
||||||
if (position==std::string::npos) {
|
if (position==std::string::npos) {
|
||||||
// no =value
|
// no =value
|
||||||
//is argument or flag?
|
//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)) {
|
if (flagsByLongName.contains(argumentName)) {
|
||||||
// flag
|
// flag
|
||||||
flagsByLongName[argumentName]->present = true;
|
flagsByLongName[argumentName]->present = true;
|
||||||
|
@ -113,8 +113,8 @@ namespace CLI {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// has =value
|
// has =value
|
||||||
std::string value = argument.substr(position, argument.length());
|
std::string value = argument.substr(position+1, argument.length()-position-1);
|
||||||
std::string argumentName = argument.substr(2, position);
|
std::string argumentName = argument.substr(2, position-2);
|
||||||
if (argumentsByLongName.contains(argumentName)) {
|
if (argumentsByLongName.contains(argumentName)) {
|
||||||
argumentsByLongName[argumentName]->present = true;
|
argumentsByLongName[argumentName]->present = true;
|
||||||
argumentsByLongName[argumentName]->value = value;
|
argumentsByLongName[argumentName]->value = value;
|
||||||
|
@ -140,12 +140,12 @@ namespace CLI {
|
||||||
argumentWaitingForValue = argumentsByShortName[argument[i]];
|
argumentWaitingForValue = argumentsByShortName[argument[i]];
|
||||||
} else {
|
} else {
|
||||||
//assume the rest of the argv is a concatenated argument value
|
//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;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this->wrongUsage = true;
|
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
|
// err on the side of caution to ensure that
|
||||||
// no unwanted options get activated on programs
|
// no unwanted options get activated on programs
|
||||||
// that deal gracefully with unrecognized command
|
// that deal gracefully with unrecognized command
|
||||||
|
@ -169,6 +169,7 @@ namespace CLI {
|
||||||
} else {
|
} else {
|
||||||
// value for unpositional argument
|
// value for unpositional argument
|
||||||
argumentWaitingForValue->value = argument;
|
argumentWaitingForValue->value = argument;
|
||||||
|
argumentWaitingForValue = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -217,7 +218,7 @@ namespace CLI {
|
||||||
|
|
||||||
ErrorOr<std::string> ArgumentsParser::getUnpositionalArgument(char shortName) {
|
ErrorOr<std::string> ArgumentsParser::getUnpositionalArgument(char shortName) {
|
||||||
if (!this->argumentsByShortName.contains(shortName)) return ErrorOr<std::string>(true, ErrorCodes::UNKNOWN_KEY, std::string(""));
|
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);
|
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(""));
|
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) {
|
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->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);
|
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(""));
|
else return ErrorOr<std::string>(true, ErrorCodes::NOT_PRESENT, std::string(""));
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@ namespace CLI {
|
||||||
std::vector<std::string> wrongUsageMessages;
|
std::vector<std::string> wrongUsageMessages;
|
||||||
|
|
||||||
// using int here bc that's how main() is defined
|
// 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();
|
~ArgumentsParser();
|
||||||
|
|
||||||
ErrorOr<bool> getFlag(char shortName);
|
ErrorOr<bool> getFlag(char shortName);
|
||||||
|
|
Loading…
Reference in New Issue