lib/cli: minor refactoring to make things less confusing and nicer to use

I renamed "unpositional arguments" to "options" and "positional arguments" to "arguments".
This is intended to make the code more readable and easier to type out.
BodgeMaster-unfinished
BodgeMaster 2022-08-02 03:16:54 +02:00
parent 69f15e928a
commit b59fe1857e
4 changed files with 423 additions and 422 deletions

View File

@ -32,10 +32,10 @@ namespace CLI {
this->present = false;
}
UnpositionalArgument::UnpositionalArgument() {
Option::Option() {
this->present = false;
}
UnpositionalArgument::UnpositionalArgument(char shortName, std::string longName, std::string placeholder, std::string description) {
Option::Option(char shortName, std::string longName, std::string placeholder, std::string description) {
this->shortName = shortName;
this->longName = longName;
this->description = description;
@ -43,23 +43,22 @@ namespace CLI {
this->present = false;
}
PositionalArgument::PositionalArgument() {
Argument::Argument() {
this->present = false;
}
PositionalArgument::PositionalArgument(std::string placeholder, std::string description) {
Argument::Argument(std::string placeholder, std::string description) {
this->description = description;
this->placeholder = placeholder;
this->present = false;
}
// using int here bc that's how main() is defined
ArgumentsParser::ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<UnpositionalArgument> unpositionalArguments, std::vector<PositionalArgument> positionalArguments) {
ArgumentsParser::ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<Option> options, std::vector<Argument> arguments) {
this->wrongUsage = false;
this->wrongUsageMessages = std::vector<std::string>();
this->programName = std::string(argv[0]);
this->positionalArguments = positionalArguments;
// create lookup tables for all flags and unpositional arguments
// by their names
this->arguments = arguments;
// create lookup tables for all flags and options by their names
this->flagsByShortName = std::map<char, Flag*>();
this->flagsByLongName = std::map<std::string, Flag*>();
for (Flag flag: flags) {
@ -68,25 +67,25 @@ namespace CLI {
this->flagsByShortName[flag.shortName] = flagPointer;
this->flagsByLongName[flag.longName] = flagPointer;
}
this->argumentsByShortName = std::map<char, UnpositionalArgument*>();
this->argumentsByLongName = std::map<std::string, UnpositionalArgument*>();
for (UnpositionalArgument unpositionalArgument: unpositionalArguments) {
UnpositionalArgument* argumentPointer = new UnpositionalArgument();
*argumentPointer = unpositionalArgument;
this->argumentsByShortName[unpositionalArgument.shortName] = argumentPointer;
this->argumentsByLongName[unpositionalArgument.longName] = argumentPointer;
this->optionsByShortName = std::map<char, Option*>();
this->optionsByLongName = std::map<std::string, Option*>();
for (Option option: options) {
Option* optionPointer = new Option();
*optionPointer = option;
this->optionsByShortName[option.shortName] = optionPointer;
this->optionsByLongName[option.longName] = optionPointer;
}
UnpositionalArgument* argumentWaitingForValue = nullptr;
std::vector<CLI::PositionalArgument>::size_type positionalArgumentCounter = 0;
Option* optionWaitingForValue = nullptr;
std::vector<CLI::Argument>::size_type argumentCounter = 0;
for (int i=1; i<argc; i++) {
std::string argument(argv[i]);
if (argument[0]=='-') {
// do we have unfinished business?
if (argumentWaitingForValue!=nullptr) {
if (optionWaitingForValue!=nullptr) {
this->wrongUsage = true;
this->wrongUsageMessages.push_back(std::string("Argument expects value but has none: ")+argumentWaitingForValue->longName);
argumentWaitingForValue = nullptr;
this->wrongUsageMessages.push_back(std::string("Argument expects value but has none: ")+optionWaitingForValue->longName);
optionWaitingForValue = nullptr;
}
// long name or short name?
if (argument[1]=='-') {
@ -98,15 +97,15 @@ namespace CLI {
auto position = argument.find("=");
if (position==std::string::npos) {
// no =value
//is argument or flag?
//is option or flag?
std::string argumentName = argument.substr(2,argument.length()-2);
if (flagsByLongName.contains(argumentName)) {
// flag
flagsByLongName[argumentName]->present = true;
} else if (argumentsByLongName.contains(argumentName)) {
// unpositional argument
argumentsByLongName[argumentName]->present = true;
argumentWaitingForValue = argumentsByLongName[argumentName];
} else if (optionsByLongName.contains(argumentName)) {
// option
optionsByLongName[argumentName]->present = true;
optionWaitingForValue = optionsByLongName[argumentName];
if (i+1 == argc) {
this->wrongUsage = true;
this->wrongUsageMessages.push_back(std::string("Argument expects value but has none: ")+argumentName);
@ -119,9 +118,9 @@ namespace CLI {
// has =value
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;
if (optionsByLongName.contains(argumentName)) {
optionsByLongName[argumentName]->present = true;
optionsByLongName[argumentName]->value = value;
} else {
this->wrongUsage = true;
this->wrongUsageMessages.push_back(std::string("Unknown argument (or it's a flag that doesn't take a value): ")+argument);
@ -134,21 +133,23 @@ namespace CLI {
// (std::__cxx11::basic_string<char>::size_type ?)
// starting at 1 because 0 is '-'
for (int j=1; j<(int) argument.length(); j++) {
//is argument or flag?
// is option or flag?
if (flagsByShortName.contains(argument[j])) {
// flag
flagsByShortName[argument[j]]->present = true;
} else if (argumentsByShortName.contains(argument[j])) {
argumentsByShortName[argument[j]]->present = true;
} else if (optionsByShortName.contains(argument[j])) {
// option
optionsByShortName[argument[j]]->present = true;
//FIXME: see above
if (j+1==(int) argument.length()) {
argumentWaitingForValue = argumentsByShortName[argument[j]];
optionWaitingForValue = optionsByShortName[argument[j]];
if (i+1 == argc) {
this->wrongUsage = true;
this->wrongUsageMessages.push_back(std::string("Argument expects value but has none: ")+this->argumentsByShortName[argument[j]]->longName);
this->wrongUsageMessages.push_back(std::string("Argument expects value but has none: ")+this->optionsByShortName[argument[j]]->longName);
}
} else {
//assume the rest of the argv is a concatenated argument value
argumentsByShortName[argument[j]]->value = argument.substr(j+1, argument.length()-j-1);
optionsByShortName[argument[j]]->value = argument.substr(j+1, argument.length()-j-1);
break;
}
} else {
@ -163,37 +164,37 @@ namespace CLI {
}
}
} else {
// positional argument or value for unpositional arg?
if (argumentWaitingForValue==nullptr) {
// positional argument
if (positionalArgumentCounter < this->positionalArguments.size()) {
this->positionalArguments.at(positionalArgumentCounter).present = true;
this->positionalArguments.at(positionalArgumentCounter).value = argument;
// argument or value for option?
if (optionWaitingForValue==nullptr) {
// argument
if (argumentCounter < this->arguments.size()) {
this->arguments.at(argumentCounter).present = true;
this->arguments.at(argumentCounter).value = argument;
} else {
this->wrongUsage = true;
this->wrongUsageMessages.push_back(std::string("Too many positional arguments! Unexpected encounter of: ")+argument);
this->wrongUsageMessages.push_back(std::string("Too many arguments! Unexpected encounter of: ")+argument);
}
positionalArgumentCounter++;
argumentCounter++;
} else {
// value for unpositional argument
argumentWaitingForValue->value = argument;
argumentWaitingForValue = nullptr;
// value for option
optionWaitingForValue->value = argument;
optionWaitingForValue = nullptr;
}
}
}
for (PositionalArgument const& positionalArgument: this->positionalArguments) {
if (!positionalArgument.present) {
for (Argument const& argument: this->arguments) {
if (!argument.present) {
this->wrongUsage = true;
this->wrongUsageMessages.push_back(std::string("Too few positional arguments! Missing: ")+positionalArgument.placeholder);
this->wrongUsageMessages.push_back(std::string("Too few arguments! Missing: ")+argument.placeholder);
}
}
}
ArgumentsParser::ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<UnpositionalArgument> unpositionalArguments, std::vector<PositionalArgument> positionalArguments, std::string description): ArgumentsParser::ArgumentsParser(argc, argv, flags, unpositionalArguments, positionalArguments) {
ArgumentsParser::ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<Option> options, std::vector<Argument> arguments, std::string description): ArgumentsParser::ArgumentsParser(argc, argv, flags, options, arguments) {
this->description = description;
}
ArgumentsParser::ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<UnpositionalArgument> unpositionalArguments, std::vector<PositionalArgument> positionalArguments, std::string description, std::string additionalInfo): ArgumentsParser::ArgumentsParser(argc, argv, flags, unpositionalArguments, positionalArguments) {
ArgumentsParser::ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<Option> options, std::vector<Argument> arguments, std::string description, std::string additionalInfo): ArgumentsParser::ArgumentsParser(argc, argv, flags, options, arguments) {
this->description = description;
this->additionalInfo = additionalInfo;
}
@ -203,8 +204,8 @@ namespace CLI {
for (auto const& [shortName, flag]: this->flagsByShortName) {
delete flag;
}
for (auto const& [shortName, unpositionalArgument]: this->argumentsByShortName) {
delete unpositionalArgument;
for (auto const& [shortName, option]: this->optionsByShortName) {
delete option;
}
}
@ -228,33 +229,33 @@ namespace CLI {
else return ErrorOr<bool> (false, ErrorCodes::NOT_PRESENT, false);
}
ErrorOr<std::string> ArgumentsParser::getPositionalArgument(std::vector<CLI::PositionalArgument>::size_type position){
if (position >= this->positionalArguments.size()) return ErrorOr<std::string>(true, ErrorCodes::OUT_OF_RANGE, std::string(""));
ErrorOr<std::string> ArgumentsParser::getArgument(std::vector<CLI::Argument>::size_type position){
if (position >= this->arguments.size()) return ErrorOr<std::string>(true, ErrorCodes::OUT_OF_RANGE, std::string(""));
if (this->wrongUsage) {
if (this->positionalArguments.at(position).present) return ErrorOr<std::string>(true, ErrorCodes::WRONG_USAGE, this->positionalArguments.at(position).value);
if (this->arguments.at(position).present) return ErrorOr<std::string>(true, ErrorCodes::WRONG_USAGE, this->arguments.at(position).value);
else return ErrorOr<std::string>(true, ErrorCodes::NOT_PRESENT, std::string(""));
}
return ErrorOr<std::string>(this->positionalArguments.at(position).value);
return ErrorOr<std::string>(this->arguments.at(position).value);
}
ErrorOr<std::string> ArgumentsParser::getUnpositionalArgument(char shortName) {
if (!this->argumentsByShortName.contains(shortName)) return ErrorOr<std::string>(true, ErrorCodes::UNKNOWN_KEY, std::string(""));
ErrorOr<std::string> ArgumentsParser::getOption(char shortName) {
if (!this->optionsByShortName.contains(shortName)) return ErrorOr<std::string>(true, ErrorCodes::UNKNOWN_KEY, std::string(""));
if (this->wrongUsage) {
if (this->argumentsByShortName[shortName]->present) return ErrorOr<std::string>(true, ErrorCodes::WRONG_USAGE, this->argumentsByShortName[shortName]->value);
if (this->optionsByShortName[shortName]->present) return ErrorOr<std::string>(true, ErrorCodes::WRONG_USAGE, this->optionsByShortName[shortName]->value);
else return ErrorOr<std::string>(true, ErrorCodes::NOT_PRESENT, std::string(""));
}
if (this->argumentsByShortName[shortName]->present) return ErrorOr<std::string>(this->argumentsByShortName[shortName]->value);
if (this->optionsByShortName[shortName]->present) return ErrorOr<std::string>(this->optionsByShortName[shortName]->value);
// argument is not present, but this is not an error -> false, NOT_PRESENT, ""
else return ErrorOr<std::string>(false, ErrorCodes::NOT_PRESENT, std::string(""));
}
ErrorOr<std::string> ArgumentsParser::getUnpositionalArgument(std::string longName) {
if (!this->argumentsByLongName.contains(longName)) return ErrorOr<std::string>(true, ErrorCodes::UNKNOWN_KEY, std::string(""));
ErrorOr<std::string> ArgumentsParser::getOption(std::string longName) {
if (!this->optionsByLongName.contains(longName)) return ErrorOr<std::string>(true, ErrorCodes::UNKNOWN_KEY, std::string(""));
if (this->wrongUsage) {
if (this->argumentsByLongName[longName]->present) return ErrorOr<std::string>(true, ErrorCodes::WRONG_USAGE, this->argumentsByLongName[longName]->value);
if (this->optionsByLongName[longName]->present) return ErrorOr<std::string>(true, ErrorCodes::WRONG_USAGE, this->optionsByLongName[longName]->value);
else return ErrorOr<std::string>(true, ErrorCodes::NOT_PRESENT, std::string(""));
}
if (this->argumentsByLongName[longName]->present) return ErrorOr<std::string>(this->argumentsByLongName[longName]->value);
if (this->optionsByLongName[longName]->present) return ErrorOr<std::string>(this->optionsByLongName[longName]->value);
// argument is not present, but this is not an error -> false, NOT_PRESENT, ""
else return ErrorOr<std::string>(false, ErrorCodes::NOT_PRESENT, std::string(""));
}

View File

@ -35,7 +35,7 @@ namespace CLI {
Flag(char shortName, std::string longName, std::string description);
};
struct UnpositionalArgument {
struct Option {
char shortName;
std::string longName;
// used for automatic usage generation
@ -45,11 +45,11 @@ namespace CLI {
bool present;
std::string value;
UnpositionalArgument();
UnpositionalArgument(char shortName, std::string longName, std::string placeholder, std::string description);
Option();
Option(char shortName, std::string longName, std::string placeholder, std::string description);
};
struct PositionalArgument {
struct Argument {
// used for automatic usage generation
std::string description;
std::string placeholder; // the "HOST" in "ping [-c <COUNT>] <HOST>"
@ -57,17 +57,17 @@ namespace CLI {
bool present;
std::string value;
PositionalArgument();
PositionalArgument(std::string placeholder, std::string description);
Argument();
Argument(std::string placeholder, std::string description);
};
class ArgumentsParser {
private:
std::map<char, Flag*> flagsByShortName;
std::map<std::string, Flag*> flagsByLongName;
std::map<char, UnpositionalArgument*> argumentsByShortName;
std::map<std::string, UnpositionalArgument*> argumentsByLongName;
std::vector<PositionalArgument> positionalArguments;
std::map<char, Option*> optionsByShortName;
std::map<std::string, Option*> optionsByLongName;
std::vector<Argument> arguments;
std::string description;
std::string additionalInfo;
@ -77,16 +77,16 @@ namespace CLI {
std::vector<std::string> wrongUsageMessages;
// using int here bc that's how main() is defined
ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<UnpositionalArgument> unpositionalArguments, std::vector<PositionalArgument> positionalArguments);
ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<UnpositionalArgument> unpositionalArguments, std::vector<PositionalArgument> positionalArguments, std::string description);
ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<UnpositionalArgument> unpositionalArguments, std::vector<PositionalArgument> positionalArguments, std::string description, std::string additionalInfo);
ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<Option> options, std::vector<Argument> arguments);
ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<Option> options, std::vector<Argument> arguments, std::string description);
ArgumentsParser(int argc, const char* const argv[], std::vector<Flag> flags, std::vector<Option> options, std::vector<Argument> arguments, std::string description, std::string additionalInfo);
~ArgumentsParser();
ErrorOr<bool> getFlag(char shortName);
ErrorOr<bool> getFlag(std::string longName);
ErrorOr<std::string> getPositionalArgument(std::vector<CLI::PositionalArgument>::size_type position);
ErrorOr<std::string> getUnpositionalArgument(char shortName);
ErrorOr<std::string> getUnpositionalArgument(std::string longName);
ErrorOr<std::string> getArgument(std::vector<CLI::Argument>::size_type position);
ErrorOr<std::string> getOption(char shortName);
ErrorOr<std::string> getOption(std::string longName);
std::string getUsage();
};

View File

@ -31,7 +31,7 @@ int main() {
// with many variations of valid input on a single command line.
//
// simulated command line:
// test -0 -ab -12345 --long-flag -cconcatenated -d separate-value -efdouble-concatenated "positional argument 0" -gh concatenated-separate-value --long-argument-with-value-included="included value" --long-argument-with-value-separated "separate value" "positional argument 1" "positional argument 2"
// test -0 -ab -12345 --long-flag -cconcatenated -d separate-value -efdouble-concatenated "argument 0" -gh concatenated-separate-value --long-argument-with-value-included="included value" --long-argument-with-value-separated "separate value" "argument 1" "argument 2"
std::vector<CLI::Flag> validTestFlags;
validTestFlags.push_back(CLI::Flag('0', "00000", "a short flag on its own"));
@ -47,19 +47,19 @@ int main() {
validTestFlags.push_back(CLI::Flag('g', "ggggg", "short flags concatenated with an argument that has a separate value"));
validTestFlags.push_back(CLI::Flag('6', "66666", "unused flag"));
std::vector<CLI::UnpositionalArgument> validTestUnpositionalArguments;
validTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('c', "ccccc", "VALUE", "short argument concatenated with its value"));
validTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('d', "ddddd", " VALUE", "short argument with separate value"));
validTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('f', "fffff", "VALUE", "short argument concatenated with a flag and its value"));
validTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('h', "hhhhh", " VALUE", "short argument concatenated with a flag with separate value"));
validTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('x', "long-argument-with-value-included", "VALUE", "long argument with its value included using ="));
validTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('y', "long-argument-with-value-separated", " VALUE", "long argument with separate value"));
validTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('z', "zzzzz", "NOPE", "unused argument"));
std::vector<CLI::Option> validTestOptions;
validTestOptions.push_back(CLI::Option('c', "ccccc", "VALUE", "short argument concatenated with its value"));
validTestOptions.push_back(CLI::Option('d', "ddddd", " VALUE", "short argument with separate value"));
validTestOptions.push_back(CLI::Option('f', "fffff", "VALUE", "short argument concatenated with a flag and its value"));
validTestOptions.push_back(CLI::Option('h', "hhhhh", " VALUE", "short argument concatenated with a flag with separate value"));
validTestOptions.push_back(CLI::Option('x', "long-argument-with-value-included", "VALUE", "long argument with its value included using ="));
validTestOptions.push_back(CLI::Option('y', "long-argument-with-value-separated", " VALUE", "long argument with separate value"));
validTestOptions.push_back(CLI::Option('z', "zzzzz", "NOPE", "unused argument"));
std::vector<CLI::PositionalArgument> validTestPositionalArguments;
validTestPositionalArguments.push_back(CLI::PositionalArgument("argument0", "positional argument between optional parameters"));
validTestPositionalArguments.push_back(CLI::PositionalArgument("argument1", "positional argument"));
validTestPositionalArguments.push_back(CLI::PositionalArgument("argument2", "positional argument"));
std::vector<CLI::Argument> validTestArguments;
validTestArguments.push_back(CLI::Argument("argument0", "argument between optional parameters"));
validTestArguments.push_back(CLI::Argument("argument1", "argument"));
validTestArguments.push_back(CLI::Argument("argument2", "argument"));
const char** validTestParameterList = new const char*[17];
validTestParameterList[ 0] = "test";
@ -71,17 +71,17 @@ int main() {
validTestParameterList[ 6] = "-d";
validTestParameterList[ 7] = "separate-value";
validTestParameterList[ 8] = "-efdouble-concatenated";
validTestParameterList[ 9] = "positional argument 0";
validTestParameterList[ 9] = "argument 0";
validTestParameterList[10] = "-gh";
validTestParameterList[11] = "concatenated-separate-value";
validTestParameterList[12] = "--long-argument-with-value-included=included value";
validTestParameterList[13] = "--long-argument-with-value-separated";
validTestParameterList[14] = "separate value";
validTestParameterList[15] = "positional argument 1";
validTestParameterList[16] = "positional argument 2";
validTestParameterList[15] = "argument 1";
validTestParameterList[16] = "argument 2";
int validTestParameterCount = 17;
CLI::ArgumentsParser validTestParameterParser = CLI::ArgumentsParser(validTestParameterCount, validTestParameterList, validTestFlags, validTestUnpositionalArguments, validTestPositionalArguments);
CLI::ArgumentsParser validTestParameterParser = CLI::ArgumentsParser(validTestParameterCount, validTestParameterList, validTestFlags, validTestOptions, validTestArguments);
ASSERT(!validTestParameterParser.wrongUsage);
ASSERT(validTestParameterParser.programName == std::string("test"));
@ -115,50 +115,50 @@ int main() {
ASSERT(!validTestParameterParser.getFlag('6').isError);
ASSERT(!validTestParameterParser.getFlag('6').value);
ASSERT(!validTestParameterParser.getFlag(std::string("66666")).value);
ASSERT(!validTestParameterParser.getUnpositionalArgument('c').isError);
ASSERT(validTestParameterParser.getUnpositionalArgument('c').value==std::string("concatenated"));
ASSERT(validTestParameterParser.getUnpositionalArgument(std::string("ccccc")).value==std::string("concatenated"));
ASSERT(!validTestParameterParser.getUnpositionalArgument('d').isError);
ASSERT(validTestParameterParser.getUnpositionalArgument('d').value==std::string("separate-value"));
ASSERT(validTestParameterParser.getUnpositionalArgument(std::string("ddddd")).value==std::string("separate-value"));
ASSERT(!validTestParameterParser.getOption('c').isError);
ASSERT(validTestParameterParser.getOption('c').value==std::string("concatenated"));
ASSERT(validTestParameterParser.getOption(std::string("ccccc")).value==std::string("concatenated"));
ASSERT(!validTestParameterParser.getOption('d').isError);
ASSERT(validTestParameterParser.getOption('d').value==std::string("separate-value"));
ASSERT(validTestParameterParser.getOption(std::string("ddddd")).value==std::string("separate-value"));
ASSERT(!validTestParameterParser.getFlag('e').isError);
ASSERT(validTestParameterParser.getFlag('e').value);
ASSERT(validTestParameterParser.getFlag(std::string("eeeee")).value);
ASSERT(!validTestParameterParser.getUnpositionalArgument('f').isError);
ASSERT(validTestParameterParser.getUnpositionalArgument('f').value==std::string("double-concatenated"));
ASSERT(validTestParameterParser.getUnpositionalArgument(std::string("fffff")).value==std::string("double-concatenated"));
ASSERT(!validTestParameterParser.getPositionalArgument(0).isError);
ASSERT(validTestParameterParser.getPositionalArgument(0).value=="positional argument 0");
ASSERT(!validTestParameterParser.getOption('f').isError);
ASSERT(validTestParameterParser.getOption('f').value==std::string("double-concatenated"));
ASSERT(validTestParameterParser.getOption(std::string("fffff")).value==std::string("double-concatenated"));
ASSERT(!validTestParameterParser.getArgument(0).isError);
ASSERT(validTestParameterParser.getArgument(0).value=="argument 0");
ASSERT(!validTestParameterParser.getFlag('g').isError);
ASSERT(validTestParameterParser.getFlag('g').value);
ASSERT(validTestParameterParser.getFlag(std::string("ggggg")).value);
ASSERT(!validTestParameterParser.getUnpositionalArgument('h').isError);
ASSERT(validTestParameterParser.getUnpositionalArgument('h').value==std::string("concatenated-separate-value"));
ASSERT(validTestParameterParser.getUnpositionalArgument(std::string("hhhhh")).value==std::string("concatenated-separate-value"));
ASSERT(!validTestParameterParser.getUnpositionalArgument('x').isError);
ASSERT(validTestParameterParser.getUnpositionalArgument('x').value==std::string("included value"));
ASSERT(validTestParameterParser.getUnpositionalArgument(std::string("long-argument-with-value-included")).value==std::string("included value"));
ASSERT(!validTestParameterParser.getUnpositionalArgument('y').isError);
ASSERT(validTestParameterParser.getUnpositionalArgument('y').value==std::string("separate value"));
ASSERT(validTestParameterParser.getUnpositionalArgument(std::string("long-argument-with-value-separated")).value==std::string("separate value"));
ASSERT(!validTestParameterParser.getPositionalArgument(1).isError);
ASSERT(validTestParameterParser.getPositionalArgument(1).value=="positional argument 1");
ASSERT(!validTestParameterParser.getPositionalArgument(2).isError);
ASSERT(validTestParameterParser.getPositionalArgument(2).value=="positional argument 2");
ASSERT(!validTestParameterParser.getUnpositionalArgument('z').isError);
ASSERT(validTestParameterParser.getUnpositionalArgument('z').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(validTestParameterParser.getUnpositionalArgument('z').value == std::string(""));
ASSERT(!validTestParameterParser.getOption('h').isError);
ASSERT(validTestParameterParser.getOption('h').value==std::string("concatenated-separate-value"));
ASSERT(validTestParameterParser.getOption(std::string("hhhhh")).value==std::string("concatenated-separate-value"));
ASSERT(!validTestParameterParser.getOption('x').isError);
ASSERT(validTestParameterParser.getOption('x').value==std::string("included value"));
ASSERT(validTestParameterParser.getOption(std::string("long-argument-with-value-included")).value==std::string("included value"));
ASSERT(!validTestParameterParser.getOption('y').isError);
ASSERT(validTestParameterParser.getOption('y').value==std::string("separate value"));
ASSERT(validTestParameterParser.getOption(std::string("long-argument-with-value-separated")).value==std::string("separate value"));
ASSERT(!validTestParameterParser.getArgument(1).isError);
ASSERT(validTestParameterParser.getArgument(1).value=="argument 1");
ASSERT(!validTestParameterParser.getArgument(2).isError);
ASSERT(validTestParameterParser.getArgument(2).value=="argument 2");
ASSERT(!validTestParameterParser.getOption('z').isError);
ASSERT(validTestParameterParser.getOption('z').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(validTestParameterParser.getOption('z').value == std::string(""));
ASSERT(validTestParameterParser.wrongUsageMessages.size() == 0);
delete[] validTestParameterList;
std::cout << "Passed valid input test." << std::endl;
std::cout << "Passed valid command line test." << std::endl;
// empty input (valid and invalid) #################################
std::vector<CLI::Flag> emptyTestFlags;
std::vector<CLI::UnpositionalArgument> emptyTestUnpositionalArguments;
std::vector<CLI::Option> emptyTestOptions;
std::vector<CLI::PositionalArgument> emptyTestPositionalArguments;
std::vector<CLI::Argument> emptyTestArguments;
const char** emptyTestParameterList = new const char*[1];
// The command is always a part of a command line, even if it is empty
@ -168,24 +168,24 @@ int main() {
int emptyTestParameterCount = 1;
// valid
CLI::ArgumentsParser validEmptyTestParameterParser = CLI::ArgumentsParser(emptyTestParameterCount, emptyTestParameterList, emptyTestFlags, emptyTestUnpositionalArguments, emptyTestPositionalArguments);
CLI::ArgumentsParser validEmptyTestParameterParser = CLI::ArgumentsParser(emptyTestParameterCount, emptyTestParameterList, emptyTestFlags, emptyTestOptions, emptyTestArguments);
ASSERT(!validEmptyTestParameterParser.wrongUsage);
ASSERT(validEmptyTestParameterParser.programName == std::string("test"));
ASSERT(validEmptyTestParameterParser.wrongUsageMessages.size() == 0);
//invalid
emptyTestPositionalArguments.push_back(CLI::PositionalArgument("argument", "positional argument"));
emptyTestArguments.push_back(CLI::Argument("argument", "argument"));
CLI::ArgumentsParser invalidEmptyTestParameterParser = CLI::ArgumentsParser(emptyTestParameterCount, emptyTestParameterList, emptyTestFlags, emptyTestUnpositionalArguments, emptyTestPositionalArguments);
CLI::ArgumentsParser invalidEmptyTestParameterParser = CLI::ArgumentsParser(emptyTestParameterCount, emptyTestParameterList, emptyTestFlags, emptyTestOptions, emptyTestArguments);
ASSERT(invalidEmptyTestParameterParser.wrongUsage);
ASSERT(invalidEmptyTestParameterParser.programName == std::string("test"));
ASSERT(invalidEmptyTestParameterParser.getPositionalArgument(0).isError);
ASSERT(invalidEmptyTestParameterParser.getPositionalArgument(0).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(invalidEmptyTestParameterParser.getArgument(0).isError);
ASSERT(invalidEmptyTestParameterParser.getArgument(0).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(invalidEmptyTestParameterParser.wrongUsageMessages.size() == 1);
ASSERT(invalidEmptyTestParameterParser.wrongUsageMessages.at(0) == "Too few positional arguments! Missing: argument");
ASSERT(invalidEmptyTestParameterParser.wrongUsageMessages.at(0) == "Too few arguments! Missing: argument");
delete[] emptyTestParameterList;
std::cout << "Passed empty input test." << std::endl;
std::cout << "Passed empty command line test." << std::endl;
// unknown flag ####################################################
@ -197,9 +197,9 @@ int main() {
unknownFlagTestFlags.push_back(CLI::Flag('4', "four", "test flag"));
unknownFlagTestFlags.push_back(CLI::Flag('5', "five", "test flag"));
std::vector<CLI::UnpositionalArgument> unknownFlagTestUnpositionalArguments;
std::vector<CLI::Option> unknownFlagTestOptions;
std::vector<CLI::PositionalArgument> unknownFlagTestPositionalArguments;
std::vector<CLI::Argument> unknownFlagTestArguments;
const char** unknownFlagTestParameterList = new const char*[7];
unknownFlagTestParameterList[0] = "test";
@ -211,7 +211,7 @@ int main() {
unknownFlagTestParameterList[6] = "-5";
int unknownFlagStandaloneTestParameterCount = 2;
CLI::ArgumentsParser unknownFlagStandaloneTestParser = CLI::ArgumentsParser(unknownFlagStandaloneTestParameterCount, unknownFlagTestParameterList, unknownFlagTestFlags, unknownFlagTestUnpositionalArguments, unknownFlagTestPositionalArguments);
CLI::ArgumentsParser unknownFlagStandaloneTestParser = CLI::ArgumentsParser(unknownFlagStandaloneTestParameterCount, unknownFlagTestParameterList, unknownFlagTestFlags, unknownFlagTestOptions, unknownFlagTestArguments);
ASSERT(unknownFlagStandaloneTestParser.getFlag('1').isError);
ASSERT(unknownFlagStandaloneTestParser.getFlag('1').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(!unknownFlagStandaloneTestParser.getFlag('1').value);
@ -223,7 +223,7 @@ int main() {
int unknownFlagMixedTestParameterCount = 7;
CLI::ArgumentsParser unknownFlagMixedTestParser = CLI::ArgumentsParser(unknownFlagMixedTestParameterCount, unknownFlagTestParameterList, unknownFlagTestFlags, unknownFlagTestUnpositionalArguments, unknownFlagTestPositionalArguments);
CLI::ArgumentsParser unknownFlagMixedTestParser = CLI::ArgumentsParser(unknownFlagMixedTestParameterCount, unknownFlagTestParameterList, unknownFlagTestFlags, unknownFlagTestOptions, unknownFlagTestArguments);
ASSERT(unknownFlagMixedTestParser.getFlag('1').isError);
ASSERT(unknownFlagMixedTestParser.getFlag('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownFlagMixedTestParser.getFlag('1').value);
@ -243,9 +243,9 @@ int main() {
unknownLongFlagTestFlags.push_back(CLI::Flag('4', "four", "test flag"));
unknownLongFlagTestFlags.push_back(CLI::Flag('5', "five", "test flag"));
std::vector<CLI::UnpositionalArgument> unknownLongFlagTestUnpositionalArguments;
std::vector<CLI::Option> unknownLongFlagTestOptions;
std::vector<CLI::PositionalArgument> unknownLongFlagTestPositionalArguments;
std::vector<CLI::Argument> unknownLongFlagTestArguments;
const char** unknownLongFlagTestParameterList = new const char*[7];
unknownLongFlagTestParameterList[0] = "test";
@ -257,7 +257,7 @@ int main() {
unknownLongFlagTestParameterList[6] = "-5";
int unknownLongFlagStandaloneTestParameterCount = 2;
CLI::ArgumentsParser unknownLongFlagStandaloneTestParser = CLI::ArgumentsParser(unknownLongFlagStandaloneTestParameterCount, unknownLongFlagTestParameterList, unknownLongFlagTestFlags, unknownLongFlagTestUnpositionalArguments, unknownLongFlagTestPositionalArguments);
CLI::ArgumentsParser unknownLongFlagStandaloneTestParser = CLI::ArgumentsParser(unknownLongFlagStandaloneTestParameterCount, unknownLongFlagTestParameterList, unknownLongFlagTestFlags, unknownLongFlagTestOptions, unknownLongFlagTestArguments);
ASSERT(unknownLongFlagStandaloneTestParser.getFlag('1').isError);
ASSERT(unknownLongFlagStandaloneTestParser.getFlag('1').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(!unknownLongFlagStandaloneTestParser.getFlag('1').value);
@ -269,7 +269,7 @@ int main() {
int unknownLongFlagMixedTestParameterCount = 7;
CLI::ArgumentsParser unknownLongFlagMixedTestParser = CLI::ArgumentsParser(unknownLongFlagMixedTestParameterCount, unknownLongFlagTestParameterList, unknownLongFlagTestFlags, unknownLongFlagTestUnpositionalArguments, unknownLongFlagTestPositionalArguments);
CLI::ArgumentsParser unknownLongFlagMixedTestParser = CLI::ArgumentsParser(unknownLongFlagMixedTestParameterCount, unknownLongFlagTestParameterList, unknownLongFlagTestFlags, unknownLongFlagTestOptions, unknownLongFlagTestArguments);
ASSERT(unknownLongFlagMixedTestParser.getFlag('1').isError);
ASSERT(unknownLongFlagMixedTestParser.getFlag('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownLongFlagMixedTestParser.getFlag('1').value);
@ -282,359 +282,359 @@ int main() {
delete[] unknownLongFlagTestParameterList;
std::cout << "Passed unknown flag test." << std::endl;
// unknown unpositional argument ###################################
// unknown argument ################################################
std::vector<CLI::Flag> unknownArgumentTestFlags;
std::vector<CLI::Flag> unknownOptionTestFlags;
std::vector<CLI::UnpositionalArgument> unknownArgumentTestUnpositionalArguments;
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('0', "zero", "zero", "test argument"));
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('1', "one", "one", "test argument"));
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('2', "two", "two", "test argument"));
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('3', "three","three","test argument"));
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('4', "four", "four", "test argument"));
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('5', "five", "five", "test argument"));
std::vector<CLI::Option> unknownOptionTestOptions;
unknownOptionTestOptions.push_back(CLI::Option('0', "zero", "zero", "test argument"));
unknownOptionTestOptions.push_back(CLI::Option('1', "one", "one", "test argument"));
unknownOptionTestOptions.push_back(CLI::Option('2', "two", "two", "test argument"));
unknownOptionTestOptions.push_back(CLI::Option('3', "three","three","test argument"));
unknownOptionTestOptions.push_back(CLI::Option('4', "four", "four", "test argument"));
unknownOptionTestOptions.push_back(CLI::Option('5', "five", "five", "test argument"));
std::vector<CLI::PositionalArgument> unknownArgumentTestPositionalArguments;
std::vector<CLI::Argument> unknownOptionTestArguments;
const char** unknownArgumentTestParameterList = new const char*[7];
unknownArgumentTestParameterList[0] = "test";
unknownArgumentTestParameterList[1] = "-a123";
unknownArgumentTestParameterList[2] = "-1a";
unknownArgumentTestParameterList[3] = "-2b";
unknownArgumentTestParameterList[4] = "-3c";
unknownArgumentTestParameterList[5] = "-4d";
unknownArgumentTestParameterList[6] = "-5e";
int unknownArgumentStandaloneTestParameterCount = 2;
const char** unknownOptionTestParameterList = new const char*[7];
unknownOptionTestParameterList[0] = "test";
unknownOptionTestParameterList[1] = "-a123";
unknownOptionTestParameterList[2] = "-1a";
unknownOptionTestParameterList[3] = "-2b";
unknownOptionTestParameterList[4] = "-3c";
unknownOptionTestParameterList[5] = "-4d";
unknownOptionTestParameterList[6] = "-5e";
int unknownOptionStandaloneTestParameterCount = 2;
CLI::ArgumentsParser unknownArgumentStandaloneTestParser = CLI::ArgumentsParser(unknownArgumentStandaloneTestParameterCount, unknownArgumentTestParameterList, unknownArgumentTestFlags, unknownArgumentTestUnpositionalArguments, unknownArgumentTestPositionalArguments);
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument('1').isError);
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument('1').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument('1').value == "");
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).isError);
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).value == "");
ASSERT(unknownArgumentStandaloneTestParser.wrongUsageMessages.size() == 1);
ASSERT(unknownArgumentStandaloneTestParser.wrongUsageMessages.at(0) == "Unknown argument or flag(s): a123");
CLI::ArgumentsParser unknownOptionStandaloneTestParser = CLI::ArgumentsParser(unknownOptionStandaloneTestParameterCount, unknownOptionTestParameterList, unknownOptionTestFlags, unknownOptionTestOptions, unknownOptionTestArguments);
ASSERT(unknownOptionStandaloneTestParser.getOption('1').isError);
ASSERT(unknownOptionStandaloneTestParser.getOption('1').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(unknownOptionStandaloneTestParser.getOption('1').value == "");
ASSERT(unknownOptionStandaloneTestParser.getOption(std::string("four")).isError);
ASSERT(unknownOptionStandaloneTestParser.getOption(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(unknownOptionStandaloneTestParser.getOption(std::string("four")).value == "");
ASSERT(unknownOptionStandaloneTestParser.wrongUsageMessages.size() == 1);
ASSERT(unknownOptionStandaloneTestParser.wrongUsageMessages.at(0) == "Unknown argument or flag(s): a123");
int unknownArgumentMixedTestParameterCount = 7;
int unknownOptionMixedTestParameterCount = 7;
CLI::ArgumentsParser unknownArgumentMixedTestParser = CLI::ArgumentsParser(unknownArgumentMixedTestParameterCount, unknownArgumentTestParameterList, unknownArgumentTestFlags, unknownArgumentTestUnpositionalArguments, unknownArgumentTestPositionalArguments);
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument('1').isError);
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument('1').value == "a");
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).isError);
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).value == "d");
ASSERT(unknownArgumentMixedTestParser.wrongUsageMessages.size() == 1);
ASSERT(unknownArgumentMixedTestParser.wrongUsageMessages.at(0) == "Unknown argument or flag(s): a123");
CLI::ArgumentsParser unknownOptionMixedTestParser = CLI::ArgumentsParser(unknownOptionMixedTestParameterCount, unknownOptionTestParameterList, unknownOptionTestFlags, unknownOptionTestOptions, unknownOptionTestArguments);
ASSERT(unknownOptionMixedTestParser.getOption('1').isError);
ASSERT(unknownOptionMixedTestParser.getOption('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownOptionMixedTestParser.getOption('1').value == "a");
ASSERT(unknownOptionMixedTestParser.getOption(std::string("four")).isError);
ASSERT(unknownOptionMixedTestParser.getOption(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownOptionMixedTestParser.getOption(std::string("four")).value == "d");
ASSERT(unknownOptionMixedTestParser.wrongUsageMessages.size() == 1);
ASSERT(unknownOptionMixedTestParser.wrongUsageMessages.at(0) == "Unknown argument or flag(s): a123");
delete[] unknownArgumentTestParameterList;
delete[] unknownOptionTestParameterList;
std::vector<CLI::Flag> unknownLongArgumentTestFlags;
std::vector<CLI::Flag> unknownLongOptionTestFlags;
std::vector<CLI::UnpositionalArgument> unknownLongArgumentTestUnpositionalArguments;
unknownLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('0', "zero", "zero", "test argument"));
unknownLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('1', "one", "one", "test argument"));
unknownLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('2', "two", "two", "test argument"));
unknownLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('3', "three","three","test argument"));
unknownLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('4', "four", "four", "test argument"));
unknownLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('5', "five", "five", "test argument"));
std::vector<CLI::Option> unknownLongOptionTestOptions;
unknownLongOptionTestOptions.push_back(CLI::Option('0', "zero", "zero", "test argument"));
unknownLongOptionTestOptions.push_back(CLI::Option('1', "one", "one", "test argument"));
unknownLongOptionTestOptions.push_back(CLI::Option('2', "two", "two", "test argument"));
unknownLongOptionTestOptions.push_back(CLI::Option('3', "three","three","test argument"));
unknownLongOptionTestOptions.push_back(CLI::Option('4', "four", "four", "test argument"));
unknownLongOptionTestOptions.push_back(CLI::Option('5', "five", "five", "test argument"));
std::vector<CLI::PositionalArgument> unknownLongArgumentTestPositionalArguments;
std::vector<CLI::Argument> unknownLongOptionTestArguments;
const char** unknownLongArgumentTestParameterList = new const char*[7];
unknownLongArgumentTestParameterList[0] = "test";
unknownLongArgumentTestParameterList[1] = "--a=123";
unknownLongArgumentTestParameterList[2] = "-1a";
unknownLongArgumentTestParameterList[3] = "-2b";
unknownLongArgumentTestParameterList[4] = "-3c";
unknownLongArgumentTestParameterList[5] = "-4d";
unknownLongArgumentTestParameterList[6] = "-5e";
int unknownLongArgumentStandaloneTestParameterCount = 2;
const char** unknownLongOptionTestParameterList = new const char*[7];
unknownLongOptionTestParameterList[0] = "test";
unknownLongOptionTestParameterList[1] = "--a=123";
unknownLongOptionTestParameterList[2] = "-1a";
unknownLongOptionTestParameterList[3] = "-2b";
unknownLongOptionTestParameterList[4] = "-3c";
unknownLongOptionTestParameterList[5] = "-4d";
unknownLongOptionTestParameterList[6] = "-5e";
int unknownLongOptionStandaloneTestParameterCount = 2;
CLI::ArgumentsParser unknownLongArgumentStandaloneTestParser = CLI::ArgumentsParser(unknownLongArgumentStandaloneTestParameterCount, unknownLongArgumentTestParameterList, unknownLongArgumentTestFlags, unknownLongArgumentTestUnpositionalArguments, unknownLongArgumentTestPositionalArguments);
ASSERT(unknownLongArgumentStandaloneTestParser.getUnpositionalArgument('1').isError);
ASSERT(unknownLongArgumentStandaloneTestParser.getUnpositionalArgument('1').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(unknownLongArgumentStandaloneTestParser.getUnpositionalArgument('1').value == "");
ASSERT(unknownLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).isError);
ASSERT(unknownLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(unknownLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).value == "");
ASSERT(unknownLongArgumentStandaloneTestParser.wrongUsageMessages.size() == 1);
ASSERT(unknownLongArgumentStandaloneTestParser.wrongUsageMessages.at(0) == "Unknown argument (or it's a flag that doesn't take a value): --a=123");
CLI::ArgumentsParser unknownLongOptionStandaloneTestParser = CLI::ArgumentsParser(unknownLongOptionStandaloneTestParameterCount, unknownLongOptionTestParameterList, unknownLongOptionTestFlags, unknownLongOptionTestOptions, unknownLongOptionTestArguments);
ASSERT(unknownLongOptionStandaloneTestParser.getOption('1').isError);
ASSERT(unknownLongOptionStandaloneTestParser.getOption('1').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(unknownLongOptionStandaloneTestParser.getOption('1').value == "");
ASSERT(unknownLongOptionStandaloneTestParser.getOption(std::string("four")).isError);
ASSERT(unknownLongOptionStandaloneTestParser.getOption(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(unknownLongOptionStandaloneTestParser.getOption(std::string("four")).value == "");
ASSERT(unknownLongOptionStandaloneTestParser.wrongUsageMessages.size() == 1);
ASSERT(unknownLongOptionStandaloneTestParser.wrongUsageMessages.at(0) == "Unknown argument (or it's a flag that doesn't take a value): --a=123");
int unknownLongArgumentMixedTestParameterCount = 7;
int unknownLongOptionMixedTestParameterCount = 7;
CLI::ArgumentsParser unknownLongArgumentMixedTestParser = CLI::ArgumentsParser(unknownLongArgumentMixedTestParameterCount, unknownLongArgumentTestParameterList, unknownLongArgumentTestFlags, unknownLongArgumentTestUnpositionalArguments, unknownLongArgumentTestPositionalArguments);
ASSERT(unknownLongArgumentMixedTestParser.getUnpositionalArgument('1').isError);
ASSERT(unknownLongArgumentMixedTestParser.getUnpositionalArgument('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownLongArgumentMixedTestParser.getUnpositionalArgument('1').value == "a");
ASSERT(unknownLongArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).isError);
ASSERT(unknownLongArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownLongArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).value == "d");
ASSERT(unknownLongArgumentMixedTestParser.wrongUsageMessages.size() == 1);
ASSERT(unknownLongArgumentMixedTestParser.wrongUsageMessages.at(0) == "Unknown argument (or it's a flag that doesn't take a value): --a=123");
CLI::ArgumentsParser unknownLongOptionMixedTestParser = CLI::ArgumentsParser(unknownLongOptionMixedTestParameterCount, unknownLongOptionTestParameterList, unknownLongOptionTestFlags, unknownLongOptionTestOptions, unknownLongOptionTestArguments);
ASSERT(unknownLongOptionMixedTestParser.getOption('1').isError);
ASSERT(unknownLongOptionMixedTestParser.getOption('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownLongOptionMixedTestParser.getOption('1').value == "a");
ASSERT(unknownLongOptionMixedTestParser.getOption(std::string("four")).isError);
ASSERT(unknownLongOptionMixedTestParser.getOption(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownLongOptionMixedTestParser.getOption(std::string("four")).value == "d");
ASSERT(unknownLongOptionMixedTestParser.wrongUsageMessages.size() == 1);
ASSERT(unknownLongOptionMixedTestParser.wrongUsageMessages.at(0) == "Unknown argument (or it's a flag that doesn't take a value): --a=123");
delete[] unknownLongArgumentTestParameterList;
std::cout << "Passed unknown unpositional argument test." << std::endl;
delete[] unknownLongOptionTestParameterList;
std::cout << "Passed unknown option test." << std::endl;
// incomplete unpositional argument ###################################
// incomplete option ###############################################
std::vector<CLI::Flag> incompleteArgumentTestFlags;
std::vector<CLI::Flag> incompleteOptionTestFlags;
std::vector<CLI::UnpositionalArgument> incompleteArgumentTestUnpositionalArguments;
incompleteArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('0', "zero", "zero", "test argument"));
incompleteArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('1', "one", "one", "test argument"));
incompleteArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('2', "two", "two", "test argument"));
incompleteArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('3', "three","three","test argument"));
incompleteArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('4', "four", "four", "test argument"));
incompleteArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('5', "five", "five", "test argument"));
std::vector<CLI::Option> incompleteOptionTestOptions;
incompleteOptionTestOptions.push_back(CLI::Option('0', "zero", "zero", "test option"));
incompleteOptionTestOptions.push_back(CLI::Option('1', "one", "one", "test option"));
incompleteOptionTestOptions.push_back(CLI::Option('2', "two", "two", "test option"));
incompleteOptionTestOptions.push_back(CLI::Option('3', "three","three","test option"));
incompleteOptionTestOptions.push_back(CLI::Option('4', "four", "four", "test option"));
incompleteOptionTestOptions.push_back(CLI::Option('5', "five", "five", "test option"));
std::vector<CLI::PositionalArgument> incompleteArgumentTestPositionalArguments;
std::vector<CLI::Argument> incompleteOptionTestArguments;
const char** incompleteArgumentTestParameterList = new const char*[6];
incompleteArgumentTestParameterList[0] = "test";
incompleteArgumentTestParameterList[1] = "-1";
incompleteArgumentTestParameterList[2] = "--two="; // value ""
incompleteArgumentTestParameterList[3] = "-3c";
incompleteArgumentTestParameterList[4] = "-4";
incompleteArgumentTestParameterList[5] = "-5e";
int incompleteArgumentStandaloneTestParameterCount = 2;
const char** incompleteOptionTestParameterList = new const char*[6];
incompleteOptionTestParameterList[0] = "test";
incompleteOptionTestParameterList[1] = "-1";
incompleteOptionTestParameterList[2] = "--two="; // value ""
incompleteOptionTestParameterList[3] = "-3c";
incompleteOptionTestParameterList[4] = "-4";
incompleteOptionTestParameterList[5] = "-5e";
int incompleteOptionStandaloneTestParameterCount = 2;
CLI::ArgumentsParser incompleteArgumentStandaloneTestParser = CLI::ArgumentsParser(incompleteArgumentStandaloneTestParameterCount, incompleteArgumentTestParameterList, incompleteArgumentTestFlags, incompleteArgumentTestUnpositionalArguments, incompleteArgumentTestPositionalArguments);
ASSERT(incompleteArgumentStandaloneTestParser.getUnpositionalArgument('1').isError);
ASSERT(incompleteArgumentStandaloneTestParser.getUnpositionalArgument('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteArgumentStandaloneTestParser.getUnpositionalArgument('1').value == "");
ASSERT(incompleteArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).isError);
ASSERT(incompleteArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(incompleteArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).value == "");
ASSERT(incompleteArgumentStandaloneTestParser.wrongUsageMessages.size() == 1);
ASSERT(incompleteArgumentStandaloneTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one");
CLI::ArgumentsParser incompleteOptionStandaloneTestParser = CLI::ArgumentsParser(incompleteOptionStandaloneTestParameterCount, incompleteOptionTestParameterList, incompleteOptionTestFlags, incompleteOptionTestOptions, incompleteOptionTestArguments);
ASSERT(incompleteOptionStandaloneTestParser.getOption('1').isError);
ASSERT(incompleteOptionStandaloneTestParser.getOption('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteOptionStandaloneTestParser.getOption('1').value == "");
ASSERT(incompleteOptionStandaloneTestParser.getOption(std::string("four")).isError);
ASSERT(incompleteOptionStandaloneTestParser.getOption(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(incompleteOptionStandaloneTestParser.getOption(std::string("four")).value == "");
ASSERT(incompleteOptionStandaloneTestParser.wrongUsageMessages.size() == 1);
ASSERT(incompleteOptionStandaloneTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one");
int incompleteArgumentMixedTestParameterCount = 6;
int incompleteOptionMixedTestParameterCount = 6;
CLI::ArgumentsParser incompleteArgumentMixedTestParser = CLI::ArgumentsParser(incompleteArgumentMixedTestParameterCount, incompleteArgumentTestParameterList, incompleteArgumentTestFlags, incompleteArgumentTestUnpositionalArguments, incompleteArgumentTestPositionalArguments);
ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument('1').isError);
ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument('1').value == "");
ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).isError);
ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).value == "");
ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument('5').isError);
ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument('5').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument('5').value == "e");
ASSERT(incompleteArgumentMixedTestParser.wrongUsageMessages.size() == 2);
ASSERT(incompleteArgumentMixedTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one");
ASSERT(incompleteArgumentMixedTestParser.wrongUsageMessages.at(1) == "Argument expects value but has none: four");
CLI::ArgumentsParser incompleteOptionMixedTestParser = CLI::ArgumentsParser(incompleteOptionMixedTestParameterCount, incompleteOptionTestParameterList, incompleteOptionTestFlags, incompleteOptionTestOptions, incompleteOptionTestArguments);
ASSERT(incompleteOptionMixedTestParser.getOption('1').isError);
ASSERT(incompleteOptionMixedTestParser.getOption('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteOptionMixedTestParser.getOption('1').value == "");
ASSERT(incompleteOptionMixedTestParser.getOption(std::string("four")).isError);
ASSERT(incompleteOptionMixedTestParser.getOption(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteOptionMixedTestParser.getOption(std::string("four")).value == "");
ASSERT(incompleteOptionMixedTestParser.getOption('5').isError);
ASSERT(incompleteOptionMixedTestParser.getOption('5').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteOptionMixedTestParser.getOption('5').value == "e");
ASSERT(incompleteOptionMixedTestParser.wrongUsageMessages.size() == 2);
ASSERT(incompleteOptionMixedTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one");
ASSERT(incompleteOptionMixedTestParser.wrongUsageMessages.at(1) == "Argument expects value but has none: four");
delete[] incompleteArgumentTestParameterList;
delete[] incompleteOptionTestParameterList;
std::vector<CLI::Flag> incompleteLongArgumentTestFlags;
std::vector<CLI::Flag> incompleteLongOptionTestFlags;
std::vector<CLI::UnpositionalArgument> incompleteLongArgumentTestUnpositionalArguments;
incompleteLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('0', "zero", "zero", "test argument"));
incompleteLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('1', "one", "one", "test argument"));
incompleteLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('2', "two", "two", "test argument"));
incompleteLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('3', "three","three","test argument"));
incompleteLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('4', "four", "four", "test argument"));
incompleteLongArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('5', "five", "five", "test argument"));
std::vector<CLI::Option> incompleteLongOptionTestOptions;
incompleteLongOptionTestOptions.push_back(CLI::Option('0', "zero", "zero", "test argument"));
incompleteLongOptionTestOptions.push_back(CLI::Option('1', "one", "one", "test argument"));
incompleteLongOptionTestOptions.push_back(CLI::Option('2', "two", "two", "test argument"));
incompleteLongOptionTestOptions.push_back(CLI::Option('3', "three","three","test argument"));
incompleteLongOptionTestOptions.push_back(CLI::Option('4', "four", "four", "test argument"));
incompleteLongOptionTestOptions.push_back(CLI::Option('5', "five", "five", "test argument"));
std::vector<CLI::PositionalArgument> incompleteLongArgumentTestPositionalArguments;
std::vector<CLI::Argument> incompleteLongOptionTestArguments;
const char** incompleteLongArgumentTestParameterList = new const char*[8];
incompleteLongArgumentTestParameterList[0] = "test";
incompleteLongArgumentTestParameterList[1] = "--one";
incompleteLongArgumentTestParameterList[2] = "--two";
incompleteLongArgumentTestParameterList[3] = "b";
incompleteLongArgumentTestParameterList[4] = "-3c";
incompleteLongArgumentTestParameterList[5] = "positional_arg";
incompleteLongArgumentTestParameterList[6] = "--four";
incompleteLongArgumentTestParameterList[7] = "--five=e";
int incompleteLongArgumentStandaloneTestParameterCount = 2;
const char** incompleteLongOptionTestParameterList = new const char*[8];
incompleteLongOptionTestParameterList[0] = "test";
incompleteLongOptionTestParameterList[1] = "--one";
incompleteLongOptionTestParameterList[2] = "--two";
incompleteLongOptionTestParameterList[3] = "b";
incompleteLongOptionTestParameterList[4] = "-3c";
incompleteLongOptionTestParameterList[5] = "arg";
incompleteLongOptionTestParameterList[6] = "--four";
incompleteLongOptionTestParameterList[7] = "--five=e";
int incompleteLongOptionStandaloneTestParameterCount = 2;
CLI::ArgumentsParser incompleteLongArgumentStandaloneTestParser = CLI::ArgumentsParser(incompleteLongArgumentStandaloneTestParameterCount, incompleteLongArgumentTestParameterList, incompleteLongArgumentTestFlags, incompleteLongArgumentTestUnpositionalArguments, incompleteLongArgumentTestPositionalArguments);
ASSERT(incompleteLongArgumentStandaloneTestParser.getUnpositionalArgument('1').isError);
ASSERT(incompleteLongArgumentStandaloneTestParser.getUnpositionalArgument('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteLongArgumentStandaloneTestParser.getUnpositionalArgument('1').value == "");
ASSERT(incompleteLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).isError);
ASSERT(incompleteLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(incompleteLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).value == "");
ASSERT(incompleteLongArgumentStandaloneTestParser.wrongUsageMessages.size() == 1);
ASSERT(incompleteLongArgumentStandaloneTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one");
CLI::ArgumentsParser incompleteLongOptionStandaloneTestParser = CLI::ArgumentsParser(incompleteLongOptionStandaloneTestParameterCount, incompleteLongOptionTestParameterList, incompleteLongOptionTestFlags, incompleteLongOptionTestOptions, incompleteLongOptionTestArguments);
ASSERT(incompleteLongOptionStandaloneTestParser.getOption('1').isError);
ASSERT(incompleteLongOptionStandaloneTestParser.getOption('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteLongOptionStandaloneTestParser.getOption('1').value == "");
ASSERT(incompleteLongOptionStandaloneTestParser.getOption(std::string("four")).isError);
ASSERT(incompleteLongOptionStandaloneTestParser.getOption(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(incompleteLongOptionStandaloneTestParser.getOption(std::string("four")).value == "");
ASSERT(incompleteLongOptionStandaloneTestParser.wrongUsageMessages.size() == 1);
ASSERT(incompleteLongOptionStandaloneTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one");
int incompleteLongArgumentMixedTestParameterCount = 8;
incompleteLongArgumentTestPositionalArguments.push_back(CLI::PositionalArgument("arg", "argument"));
int incompleteLongOptionMixedTestParameterCount = 8;
incompleteLongOptionTestArguments.push_back(CLI::Argument("arg", "argument"));
CLI::ArgumentsParser incompleteLongArgumentMixedTestParser = CLI::ArgumentsParser(incompleteLongArgumentMixedTestParameterCount, incompleteLongArgumentTestParameterList, incompleteLongArgumentTestFlags, incompleteLongArgumentTestUnpositionalArguments, incompleteLongArgumentTestPositionalArguments);
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('1').isError);
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('1').value == "");
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('2').isError);
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('2').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('2').value == "b");
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).isError);
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).value == "");
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('5').isError);
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('5').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('5').value == "e");
ASSERT(incompleteLongArgumentMixedTestParser.wrongUsageMessages.size() == 2);
ASSERT(incompleteLongArgumentMixedTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one");
ASSERT(incompleteLongArgumentMixedTestParser.wrongUsageMessages.at(1) == "Argument expects value but has none: four");
CLI::ArgumentsParser incompleteLongOptionMixedTestParser = CLI::ArgumentsParser(incompleteLongOptionMixedTestParameterCount, incompleteLongOptionTestParameterList, incompleteLongOptionTestFlags, incompleteLongOptionTestOptions, incompleteLongOptionTestArguments);
ASSERT(incompleteLongOptionMixedTestParser.getOption('1').isError);
ASSERT(incompleteLongOptionMixedTestParser.getOption('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteLongOptionMixedTestParser.getOption('1').value == "");
ASSERT(incompleteLongOptionMixedTestParser.getOption('2').isError);
ASSERT(incompleteLongOptionMixedTestParser.getOption('2').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteLongOptionMixedTestParser.getOption('2').value == "b");
ASSERT(incompleteLongOptionMixedTestParser.getOption(std::string("four")).isError);
ASSERT(incompleteLongOptionMixedTestParser.getOption(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteLongOptionMixedTestParser.getOption(std::string("four")).value == "");
ASSERT(incompleteLongOptionMixedTestParser.getOption('5').isError);
ASSERT(incompleteLongOptionMixedTestParser.getOption('5').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(incompleteLongOptionMixedTestParser.getOption('5').value == "e");
ASSERT(incompleteLongOptionMixedTestParser.wrongUsageMessages.size() == 2);
ASSERT(incompleteLongOptionMixedTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one");
ASSERT(incompleteLongOptionMixedTestParser.wrongUsageMessages.at(1) == "Argument expects value but has none: four");
delete[] incompleteLongArgumentTestParameterList;
std::cout << "Passed incomplete unpositional argument test." << std::endl;
delete[] incompleteLongOptionTestParameterList;
std::cout << "Passed incomplete option test." << std::endl;
// too few positional arguments
// too few arguments ###############################################
std::vector<CLI::Flag> tooFewArgumentsTestFlags;
tooFewArgumentsTestFlags.push_back(CLI::Flag('a', "aaaaa", "test flag"));
std::vector<CLI::UnpositionalArgument> tooFewArgumentsTestUnpositionalArguments;
tooFewArgumentsTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('b', "bbbbb", "b", "test argument"));
tooFewArgumentsTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('c', "ccccc", "c", "test argument"));
tooFewArgumentsTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('d', "ddddd", "d", "test argument"));
std::vector<CLI::Option> tooFewArgumentsTestOptions;
tooFewArgumentsTestOptions.push_back(CLI::Option('b', "bbbbb", "b", "test argument"));
tooFewArgumentsTestOptions.push_back(CLI::Option('c', "ccccc", "c", "test argument"));
tooFewArgumentsTestOptions.push_back(CLI::Option('d', "ddddd", "d", "test argument"));
std::vector<CLI::PositionalArgument> tooFewArgumentsTestPositionalArguments;
tooFewArgumentsTestPositionalArguments.push_back(CLI::PositionalArgument("argument0", "test argument"));
tooFewArgumentsTestPositionalArguments.push_back(CLI::PositionalArgument("argument1", "test argument"));
tooFewArgumentsTestPositionalArguments.push_back(CLI::PositionalArgument("argument2", "test argument"));
tooFewArgumentsTestPositionalArguments.push_back(CLI::PositionalArgument("argument3", "test argument"));
tooFewArgumentsTestPositionalArguments.push_back(CLI::PositionalArgument("argument4", "test argument"));
std::vector<CLI::Argument> tooFewArgumentsTestArguments;
tooFewArgumentsTestArguments.push_back(CLI::Argument("argument0", "test argument"));
tooFewArgumentsTestArguments.push_back(CLI::Argument("argument1", "test argument"));
tooFewArgumentsTestArguments.push_back(CLI::Argument("argument2", "test argument"));
tooFewArgumentsTestArguments.push_back(CLI::Argument("argument3", "test argument"));
tooFewArgumentsTestArguments.push_back(CLI::Argument("argument4", "test argument"));
const char** tooFewArgumentsTestParameterList = new const char*[10];
tooFewArgumentsTestParameterList[0] = "test";
tooFewArgumentsTestParameterList[1] = "positional_arg_0";
tooFewArgumentsTestParameterList[1] = "arg_0";
tooFewArgumentsTestParameterList[2] = "-a";//
tooFewArgumentsTestParameterList[3] = "-b";
tooFewArgumentsTestParameterList[4] = "1";
tooFewArgumentsTestParameterList[5] = "-c5";
tooFewArgumentsTestParameterList[6] = "positional_arg_1";
tooFewArgumentsTestParameterList[6] = "arg_1";
tooFewArgumentsTestParameterList[7] = "--ddddd";
tooFewArgumentsTestParameterList[8] = "ddd";
tooFewArgumentsTestParameterList[9] = "positional_arg_2";
tooFewArgumentsTestParameterList[9] = "arg_2";
int tooFewArgumentsStandaloneTestParameterCount = 2;
CLI::ArgumentsParser tooFewArgumentsStandaloneTestParser = CLI::ArgumentsParser(tooFewArgumentsStandaloneTestParameterCount, tooFewArgumentsTestParameterList, tooFewArgumentsTestFlags, tooFewArgumentsTestUnpositionalArguments, tooFewArgumentsTestPositionalArguments);
CLI::ArgumentsParser tooFewArgumentsStandaloneTestParser = CLI::ArgumentsParser(tooFewArgumentsStandaloneTestParameterCount, tooFewArgumentsTestParameterList, tooFewArgumentsTestFlags, tooFewArgumentsTestOptions, tooFewArgumentsTestArguments);
ASSERT(tooFewArgumentsStandaloneTestParser.getPositionalArgument(0).isError);
ASSERT(tooFewArgumentsStandaloneTestParser.getPositionalArgument(0).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsStandaloneTestParser.getPositionalArgument(0).value == "positional_arg_0");
ASSERT(tooFewArgumentsStandaloneTestParser.getPositionalArgument(1).isError);
ASSERT(tooFewArgumentsStandaloneTestParser.getPositionalArgument(1).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsStandaloneTestParser.getPositionalArgument(1).value == "");
ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('b').isError);
ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('b').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('b').value == "");
ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('c').isError);
ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('c').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('c').value == "");
ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('d').isError);
ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('d').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('d').value == "");
ASSERT(tooFewArgumentsStandaloneTestParser.getArgument(0).isError);
ASSERT(tooFewArgumentsStandaloneTestParser.getArgument(0).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsStandaloneTestParser.getArgument(0).value == "arg_0");
ASSERT(tooFewArgumentsStandaloneTestParser.getArgument(1).isError);
ASSERT(tooFewArgumentsStandaloneTestParser.getArgument(1).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsStandaloneTestParser.getArgument(1).value == "");
ASSERT(tooFewArgumentsStandaloneTestParser.getOption('b').isError);
ASSERT(tooFewArgumentsStandaloneTestParser.getOption('b').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsStandaloneTestParser.getOption('b').value == "");
ASSERT(tooFewArgumentsStandaloneTestParser.getOption('c').isError);
ASSERT(tooFewArgumentsStandaloneTestParser.getOption('c').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsStandaloneTestParser.getOption('c').value == "");
ASSERT(tooFewArgumentsStandaloneTestParser.getOption('d').isError);
ASSERT(tooFewArgumentsStandaloneTestParser.getOption('d').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsStandaloneTestParser.getOption('d').value == "");
ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.size() == 4);
ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(0) == "Too few positional arguments! Missing: argument1");
ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(1) == "Too few positional arguments! Missing: argument2");
ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(2) == "Too few positional arguments! Missing: argument3");
ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(3) == "Too few positional arguments! Missing: argument4");
ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(0) == "Too few arguments! Missing: argument1");
ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(1) == "Too few arguments! Missing: argument2");
ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(2) == "Too few arguments! Missing: argument3");
ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(3) == "Too few arguments! Missing: argument4");
int tooFewArgumentsMixedTestParameterCount = 10;
CLI::ArgumentsParser tooFewArgumentsMixedTestParser = CLI::ArgumentsParser(tooFewArgumentsMixedTestParameterCount, tooFewArgumentsTestParameterList, tooFewArgumentsTestFlags, tooFewArgumentsTestUnpositionalArguments, tooFewArgumentsTestPositionalArguments);
CLI::ArgumentsParser tooFewArgumentsMixedTestParser = CLI::ArgumentsParser(tooFewArgumentsMixedTestParameterCount, tooFewArgumentsTestParameterList, tooFewArgumentsTestFlags, tooFewArgumentsTestOptions, tooFewArgumentsTestArguments);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(0).isError);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(0).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(0).value == "positional_arg_0");
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(1).isError);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(1).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(1).value == "positional_arg_1");
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(2).isError);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(2).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(2).value == "positional_arg_2");
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(3).isError);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(3).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(3).value == "");
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(4).isError);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(4).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(4).value == "");
ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('b').isError);
ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('b').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('b').value == "1");
ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('c').isError);
ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('c').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('c').value == "5");
ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('d').isError);
ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('d').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('d').value == "ddd");
ASSERT(tooFewArgumentsMixedTestParser.getArgument(0).isError);
ASSERT(tooFewArgumentsMixedTestParser.getArgument(0).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getArgument(0).value == "arg_0");
ASSERT(tooFewArgumentsMixedTestParser.getArgument(1).isError);
ASSERT(tooFewArgumentsMixedTestParser.getArgument(1).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getArgument(1).value == "arg_1");
ASSERT(tooFewArgumentsMixedTestParser.getArgument(2).isError);
ASSERT(tooFewArgumentsMixedTestParser.getArgument(2).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getArgument(2).value == "arg_2");
ASSERT(tooFewArgumentsMixedTestParser.getArgument(3).isError);
ASSERT(tooFewArgumentsMixedTestParser.getArgument(3).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsMixedTestParser.getArgument(3).value == "");
ASSERT(tooFewArgumentsMixedTestParser.getArgument(4).isError);
ASSERT(tooFewArgumentsMixedTestParser.getArgument(4).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooFewArgumentsMixedTestParser.getArgument(4).value == "");
ASSERT(tooFewArgumentsMixedTestParser.getOption('b').isError);
ASSERT(tooFewArgumentsMixedTestParser.getOption('b').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getOption('b').value == "1");
ASSERT(tooFewArgumentsMixedTestParser.getOption('c').isError);
ASSERT(tooFewArgumentsMixedTestParser.getOption('c').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getOption('c').value == "5");
ASSERT(tooFewArgumentsMixedTestParser.getOption('d').isError);
ASSERT(tooFewArgumentsMixedTestParser.getOption('d').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooFewArgumentsMixedTestParser.getOption('d').value == "ddd");
ASSERT(tooFewArgumentsMixedTestParser.wrongUsageMessages.size() == 2);
ASSERT(tooFewArgumentsMixedTestParser.wrongUsageMessages.at(0) == "Too few positional arguments! Missing: argument3");
ASSERT(tooFewArgumentsMixedTestParser.wrongUsageMessages.at(1) == "Too few positional arguments! Missing: argument4");
ASSERT(tooFewArgumentsMixedTestParser.wrongUsageMessages.at(0) == "Too few arguments! Missing: argument3");
ASSERT(tooFewArgumentsMixedTestParser.wrongUsageMessages.at(1) == "Too few arguments! Missing: argument4");
delete[] tooFewArgumentsTestParameterList;
std::cout << "Passed too few positional arguments test." << std::endl;
std::cout << "Passed too few arguments test." << std::endl;
// too many positional arguments
// too many arguments ##############################################
std::vector<CLI::Flag> tooManyArgumentsTestFlags;
tooManyArgumentsTestFlags.push_back(CLI::Flag('a', "aaaaa", "test flag"));
std::vector<CLI::UnpositionalArgument> tooManyArgumentsTestUnpositionalArguments;
tooManyArgumentsTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('b', "bbbbb", "b", "test argument"));
tooManyArgumentsTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('c', "ccccc", "c", "test argument"));
tooManyArgumentsTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('d', "ddddd", "d", "test argument"));
std::vector<CLI::Option> tooManyArgumentsTestOptions;
tooManyArgumentsTestOptions.push_back(CLI::Option('b', "bbbbb", "b", "test argument"));
tooManyArgumentsTestOptions.push_back(CLI::Option('c', "ccccc", "c", "test argument"));
tooManyArgumentsTestOptions.push_back(CLI::Option('d', "ddddd", "d", "test argument"));
std::vector<CLI::PositionalArgument> tooManyArgumentsTestPositionalArguments;
tooManyArgumentsTestPositionalArguments.push_back(CLI::PositionalArgument("argument0", "test argument"));
std::vector<CLI::Argument> tooManyArgumentsTestArguments;
tooManyArgumentsTestArguments.push_back(CLI::Argument("argument0", "test argument"));
const char** tooManyArgumentsTestParameterList = new const char*[10];
tooManyArgumentsTestParameterList[0] = "test";
tooManyArgumentsTestParameterList[1] = "positional_arg_0";
tooManyArgumentsTestParameterList[2] = "positional_arg_1";
tooManyArgumentsTestParameterList[1] = "arg_0";
tooManyArgumentsTestParameterList[2] = "arg_1";
tooManyArgumentsTestParameterList[3] = "-a";//
tooManyArgumentsTestParameterList[4] = "-b";
tooManyArgumentsTestParameterList[5] = "1";
tooManyArgumentsTestParameterList[6] = "-c5";
tooManyArgumentsTestParameterList[7] = "--ddddd";
tooManyArgumentsTestParameterList[8] = "ddd";
tooManyArgumentsTestParameterList[9] = "positional_arg_2";
tooManyArgumentsTestParameterList[9] = "arg_2";
int tooManyArgumentsStandaloneTestParameterCount = 3;
CLI::ArgumentsParser tooManyArgumentsStandaloneTestParser = CLI::ArgumentsParser(tooManyArgumentsStandaloneTestParameterCount, tooManyArgumentsTestParameterList, tooManyArgumentsTestFlags, tooManyArgumentsTestUnpositionalArguments, tooManyArgumentsTestPositionalArguments);
CLI::ArgumentsParser tooManyArgumentsStandaloneTestParser = CLI::ArgumentsParser(tooManyArgumentsStandaloneTestParameterCount, tooManyArgumentsTestParameterList, tooManyArgumentsTestFlags, tooManyArgumentsTestOptions, tooManyArgumentsTestArguments);
ASSERT(tooManyArgumentsStandaloneTestParser.getPositionalArgument(0).isError);
ASSERT(tooManyArgumentsStandaloneTestParser.getPositionalArgument(0).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooManyArgumentsStandaloneTestParser.getPositionalArgument(0).value == "positional_arg_0");
ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('b').isError);
ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('b').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('b').value == "");
ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('c').isError);
ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('c').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('c').value == "");
ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('d').isError);
ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('d').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('d').value == "");
ASSERT(tooManyArgumentsStandaloneTestParser.getArgument(0).isError);
ASSERT(tooManyArgumentsStandaloneTestParser.getArgument(0).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooManyArgumentsStandaloneTestParser.getArgument(0).value == "arg_0");
ASSERT(tooManyArgumentsStandaloneTestParser.getOption('b').isError);
ASSERT(tooManyArgumentsStandaloneTestParser.getOption('b').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooManyArgumentsStandaloneTestParser.getOption('b').value == "");
ASSERT(tooManyArgumentsStandaloneTestParser.getOption('c').isError);
ASSERT(tooManyArgumentsStandaloneTestParser.getOption('c').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooManyArgumentsStandaloneTestParser.getOption('c').value == "");
ASSERT(tooManyArgumentsStandaloneTestParser.getOption('d').isError);
ASSERT(tooManyArgumentsStandaloneTestParser.getOption('d').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(tooManyArgumentsStandaloneTestParser.getOption('d').value == "");
ASSERT(tooManyArgumentsStandaloneTestParser.wrongUsageMessages.size() == 1);
ASSERT(tooManyArgumentsStandaloneTestParser.wrongUsageMessages.at(0) == "Too many positional arguments! Unexpected encounter of: positional_arg_1");
ASSERT(tooManyArgumentsStandaloneTestParser.wrongUsageMessages.at(0) == "Too many arguments! Unexpected encounter of: arg_1");
int tooManyArgumentsMixedTestParameterCount = 10;
CLI::ArgumentsParser tooManyArgumentsMixedTestParser = CLI::ArgumentsParser(tooManyArgumentsMixedTestParameterCount, tooManyArgumentsTestParameterList, tooManyArgumentsTestFlags, tooManyArgumentsTestUnpositionalArguments, tooManyArgumentsTestPositionalArguments);
CLI::ArgumentsParser tooManyArgumentsMixedTestParser = CLI::ArgumentsParser(tooManyArgumentsMixedTestParameterCount, tooManyArgumentsTestParameterList, tooManyArgumentsTestFlags, tooManyArgumentsTestOptions, tooManyArgumentsTestArguments);
ASSERT(tooManyArgumentsMixedTestParser.getPositionalArgument(0).isError);
ASSERT(tooManyArgumentsMixedTestParser.getPositionalArgument(0).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooManyArgumentsMixedTestParser.getPositionalArgument(0).value == "positional_arg_0");
ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('b').isError);
ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('b').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('b').value == "1");
ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('c').isError);
ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('c').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('c').value == "5");
ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('d').isError);
ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('d').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('d').value == "ddd");
ASSERT(tooManyArgumentsMixedTestParser.getArgument(0).isError);
ASSERT(tooManyArgumentsMixedTestParser.getArgument(0).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooManyArgumentsMixedTestParser.getArgument(0).value == "arg_0");
ASSERT(tooManyArgumentsMixedTestParser.getOption('b').isError);
ASSERT(tooManyArgumentsMixedTestParser.getOption('b').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooManyArgumentsMixedTestParser.getOption('b').value == "1");
ASSERT(tooManyArgumentsMixedTestParser.getOption('c').isError);
ASSERT(tooManyArgumentsMixedTestParser.getOption('c').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooManyArgumentsMixedTestParser.getOption('c').value == "5");
ASSERT(tooManyArgumentsMixedTestParser.getOption('d').isError);
ASSERT(tooManyArgumentsMixedTestParser.getOption('d').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(tooManyArgumentsMixedTestParser.getOption('d').value == "ddd");
ASSERT(tooManyArgumentsMixedTestParser.wrongUsageMessages.size() == 2);
ASSERT(tooManyArgumentsMixedTestParser.wrongUsageMessages.at(0) == "Too many positional arguments! Unexpected encounter of: positional_arg_1");
ASSERT(tooManyArgumentsMixedTestParser.wrongUsageMessages.at(1) == "Too many positional arguments! Unexpected encounter of: positional_arg_2");
ASSERT(tooManyArgumentsMixedTestParser.wrongUsageMessages.at(0) == "Too many arguments! Unexpected encounter of: arg_1");
ASSERT(tooManyArgumentsMixedTestParser.wrongUsageMessages.at(1) == "Too many arguments! Unexpected encounter of: arg_2");
delete[] tooManyArgumentsTestParameterList;
std::cout << "Passed too many positional arguments test." << std::endl;
std::cout << "Passed too many arguments test." << std::endl;
return 0;
}

View File

@ -97,14 +97,14 @@ int main(int argc, char* argv[]){
flags.push_back(CLI::Flag('t', "tcp", "use TCP, defaults to both when -t and -u are omitted, otherwise uses what is specified"));
flags.push_back(CLI::Flag('u', "udp", "use UDP, defaults to both when -t and -u are omitted, otherwise uses what is specified"));
std::vector<CLI::UnpositionalArgument> unpositionalArguments;
unpositionalArguments.push_back(CLI::UnpositionalArgument('c', "connect", "HOST", "connect to HOST, listen for incoming connections if omitted"));
unpositionalArguments.push_back(CLI::UnpositionalArgument('m', "mtu-optimize", "MTU", "Optimize for a specific maximum transfer unit by reading MTU bytes at a time."));
std::vector<CLI::Option> options;
options.push_back(CLI::Option('c', "connect", "HOST", "connect to HOST, listen for incoming connections if omitted"));
options.push_back(CLI::Option('m', "mtu-optimize", "MTU", "Optimize for a specific maximum transfer unit by reading MTU bytes at a time."));
std::vector<CLI::PositionalArgument> positionalArguments;
positionalArguments.push_back(CLI::PositionalArgument("PORT", "the port to use"));
std::vector<CLI::Argument> arguments;
arguments.push_back(CLI::Argument("PORT", "the port to use"));
CLI::ArgumentsParser cliParser = CLI::ArgumentsParser(argc, argv, flags, unpositionalArguments, positionalArguments);
CLI::ArgumentsParser cliParser = CLI::ArgumentsParser(argc, argv, flags, options, arguments);
if (cliParser.wrongUsage) {
//TODO: spit out usage information generated by the parser
@ -118,16 +118,16 @@ int main(int argc, char* argv[]){
tcp = cliParser.getFlag('t').value;
udp = cliParser.getFlag('u').value;
}
if (cliParser.getUnpositionalArgument('c').errorCode == ErrorCodes::NOT_PRESENT) {
if (cliParser.getOption('c').errorCode == ErrorCodes::NOT_PRESENT) {
listenMode = true;
}
if (cliParser.getUnpositionalArgument('m').errorCode == ErrorCodes::SUCCESS) {
mtu = std::stol(cliParser.getUnpositionalArgument('m').value);
if (cliParser.getOption('m').errorCode == ErrorCodes::SUCCESS) {
mtu = std::stol(cliParser.getOption('m').value);
}
host = cliParser.getUnpositionalArgument('c').value;
host = cliParser.getOption('c').value;
//FIXME: use a function that returns a fixed-width data type instead,
// ensure that the given value is a valid port
port = (in_port_t) std::stoi(cliParser.getPositionalArgument(0).value);
port = (in_port_t) std::stoi(cliParser.getArgument(0).value);
if (listenMode) {
if (ipv6) {