lib/cli: fix trailing incomplete unpositional arguments
The parser used to rely on the next iteration of the loop to detect if an unpositional argument was missing its value, this has now been fixed by adding an additional check on unpositional arguments waiting for a value that detects if the end of the loop has been reachedBodgeMaster-unfinished
parent
44a20c875a
commit
db3b133f88
|
@ -107,6 +107,10 @@ namespace CLI {
|
||||||
// unpositional argument
|
// unpositional argument
|
||||||
argumentsByLongName[argumentName]->present = true;
|
argumentsByLongName[argumentName]->present = true;
|
||||||
argumentWaitingForValue = argumentsByLongName[argumentName];
|
argumentWaitingForValue = argumentsByLongName[argumentName];
|
||||||
|
if (i+1 == argc) {
|
||||||
|
this->wrongUsage = true;
|
||||||
|
this->wrongUsageMessages.push_back(std::string("Argument expects value but has none: ")+argumentName);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this->wrongUsage = true;
|
this->wrongUsage = true;
|
||||||
this->wrongUsageMessages.push_back(std::string("Unknown argument or flag: ")+argument);
|
this->wrongUsageMessages.push_back(std::string("Unknown argument or flag: ")+argument);
|
||||||
|
@ -129,23 +133,27 @@ namespace CLI {
|
||||||
// length is defined as
|
// length is defined as
|
||||||
// (std::__cxx11::basic_string<char>::size_type ?)
|
// (std::__cxx11::basic_string<char>::size_type ?)
|
||||||
// starting at 1 because 0 is '-'
|
// starting at 1 because 0 is '-'
|
||||||
for (int i=1; i<(int) argument.length(); i++) {
|
for (int j=1; j<(int) argument.length(); j++) {
|
||||||
//is argument or flag?
|
//is argument or flag?
|
||||||
if (flagsByShortName.contains(argument[i])) {
|
if (flagsByShortName.contains(argument[j])) {
|
||||||
flagsByShortName[argument[i]]->present = true;
|
flagsByShortName[argument[j]]->present = true;
|
||||||
} else if (argumentsByShortName.contains(argument[i])) {
|
} else if (argumentsByShortName.contains(argument[j])) {
|
||||||
argumentsByShortName[argument[i]]->present = true;
|
argumentsByShortName[argument[j]]->present = true;
|
||||||
//FIXME: see above
|
//FIXME: see above
|
||||||
if (i+1==(int) argument.length()) {
|
if (j+1==(int) argument.length()) {
|
||||||
argumentWaitingForValue = argumentsByShortName[argument[i]];
|
argumentWaitingForValue = argumentsByShortName[argument[j]];
|
||||||
|
if (i+1 == argc) {
|
||||||
|
this->wrongUsage = true;
|
||||||
|
this->wrongUsageMessages.push_back(std::string("Argument expects value but has none: ")+argument.substr(j, 1));
|
||||||
|
}
|
||||||
} 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()-i-1);
|
argumentsByShortName[argument[j]]->value = argument.substr(j+1, argument.length()-j-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()-i));
|
this->wrongUsageMessages.push_back(std::string("Unknown argument or flag(s): ")+argument.substr(j, argument.length()-j));
|
||||||
// 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
|
||||||
|
|
Loading…
Reference in New Issue