Prevent warnings in argumentparser{,tests}.cpp

This commit is contained in:
Martchus 2019-05-04 21:38:23 +02:00
parent 151a3ae9e5
commit 497826f634
2 changed files with 61 additions and 23 deletions

View File

@ -183,7 +183,9 @@ bool ArgumentReader::read(ArgumentVector &args)
if (values && lastArgInLevel->requiredValueCount() != Argument::varValueCount && values->size() < lastArgInLevel->requiredValueCount()) { if (values && lastArgInLevel->requiredValueCount() != Argument::varValueCount && values->size() < lastArgInLevel->requiredValueCount()) {
// read arg as value and continue with next arg // read arg as value and continue with next arg
values->emplace_back(argDenotation ? argDenotation : *argv); values->emplace_back(argDenotation ? argDenotation : *argv);
++index, ++argv, argDenotation = nullptr; ++index;
++argv;
argDenotation = nullptr;
continue; continue;
} }
@ -198,12 +200,21 @@ bool ArgumentReader::read(ArgumentVector &args)
argDenotation = *argv; argDenotation = *argv;
if (!*argDenotation && (!lastArgInLevel || values->size() >= lastArgInLevel->requiredValueCount())) { if (!*argDenotation && (!lastArgInLevel || values->size() >= lastArgInLevel->requiredValueCount())) {
// skip empty arguments // skip empty arguments
++index, ++argv, argDenotation = nullptr; ++index;
++argv;
argDenotation = nullptr;
continue; continue;
} }
abbreviationFound = false; abbreviationFound = false;
argDenotationType = Value; argDenotationType = Value;
*argDenotation == '-' && (++argDenotation, ++argDenotationType) && *argDenotation == '-' && (++argDenotation, ++argDenotationType); if (*argDenotation == '-') {
++argDenotation;
++argDenotationType;
if (*argDenotation == '-') {
++argDenotation;
++argDenotationType;
}
}
} }
// try to find matching Argument instance // try to find matching Argument instance
@ -250,10 +261,14 @@ bool ArgumentReader::read(ArgumentVector &args)
} }
// read sub arguments, distinguish whether further abbreviations follow // read sub arguments, distinguish whether further abbreviations follow
++index, ++parser.m_actualArgc, lastArg = lastArgInLevel = matchingArg, lastArgDenotation = argv; ++index;
++parser.m_actualArgc;
lastArg = lastArgInLevel = matchingArg;
lastArgDenotation = argv;
if (argDenotationType != Abbreviation || !argDenotation || !*argDenotation) { if (argDenotationType != Abbreviation || !argDenotation || !*argDenotation) {
// no further abbreviations follow -> read sub args for next argv // no further abbreviations follow -> read sub args for next argv
++argv, argDenotation = nullptr; ++argv;
argDenotation = nullptr;
read(lastArg->m_subArgs); read(lastArg->m_subArgs);
argDenotation = nullptr; argDenotation = nullptr;
break; break;
@ -292,13 +307,15 @@ bool ArgumentReader::read(ArgumentVector &args)
if (parentArgument == pathEnd) { if (parentArgument == pathEnd) {
break; break;
} }
}; }
} }
// unknown argument might just be a parameter value of the last argument // unknown argument might just be a parameter value of the last argument
if (lastArgInLevel && values->size() < lastArgInLevel->requiredValueCount()) { if (lastArgInLevel && values->size() < lastArgInLevel->requiredValueCount()) {
values->emplace_back(abbreviationFound ? argDenotation : *argv); values->emplace_back(abbreviationFound ? argDenotation : *argv);
++index, ++argv, argDenotation = nullptr; ++index;
++argv;
argDenotation = nullptr;
continue; continue;
} }
@ -307,7 +324,8 @@ bool ArgumentReader::read(ArgumentVector &args)
if (arg->denotesOperation() && arg->name() && !strcmp(arg->name(), *argv)) { if (arg->denotesOperation() && arg->name() && !strcmp(arg->name(), *argv)) {
(matchingArg = arg)->m_occurrences.emplace_back(index, parentPath, parentArg); (matchingArg = arg)->m_occurrences.emplace_back(index, parentPath, parentArg);
lastArgDenotation = argv; lastArgDenotation = argv;
++index, ++argv; ++index;
++argv;
break; break;
} }
} }
@ -334,7 +352,9 @@ bool ArgumentReader::read(ArgumentVector &args)
values = &matchingArg->m_occurrences.back().values; values = &matchingArg->m_occurrences.back().values;
// read sub arguments // read sub arguments
++parser.m_actualArgc, lastArg = lastArgInLevel = matchingArg, argDenotation = nullptr; ++parser.m_actualArgc;
lastArg = lastArgInLevel = matchingArg;
argDenotation = nullptr;
read(lastArg->m_subArgs); read(lastArg->m_subArgs);
argDenotation = nullptr; argDenotation = nullptr;
continue; continue;
@ -347,7 +367,9 @@ bool ArgumentReader::read(ArgumentVector &args)
} }
if (completionMode) { if (completionMode) {
// ignore unknown denotation // ignore unknown denotation
++index, ++argv, argDenotation = nullptr; ++index;
++argv;
argDenotation = nullptr;
} else { } else {
switch (parser.m_unknownArgBehavior) { switch (parser.m_unknownArgBehavior) {
case UnknownArgumentBehavior::Warn: case UnknownArgumentBehavior::Warn:
@ -355,7 +377,9 @@ bool ArgumentReader::read(ArgumentVector &args)
FALLTHROUGH; FALLTHROUGH;
case UnknownArgumentBehavior::Ignore: case UnknownArgumentBehavior::Ignore:
// ignore unknown denotation // ignore unknown denotation
++index, ++argv, argDenotation = nullptr; ++index;
++argv;
argDenotation = nullptr;
break; break;
case UnknownArgumentBehavior::Fail: case UnknownArgumentBehavior::Fail:
return false; return false;
@ -772,18 +796,18 @@ void ArgumentParser::printHelp(ostream &os) const
if (applicationVersion && *applicationVersion) { if (applicationVersion && *applicationVersion) {
os << "version " << applicationVersion; os << "version " << applicationVersion;
} }
if (dependencyVersions2.size()) { if (dependencyVersions.size()) {
if ((applicationName && *applicationName) || (applicationVersion && *applicationVersion)) { if ((applicationName && *applicationName) || (applicationVersion && *applicationVersion)) {
os << '\n'; os << '\n';
EscapeCodes::setStyle(os); EscapeCodes::setStyle(os);
} }
auto i = dependencyVersions2.begin(), end = dependencyVersions2.end(); auto i = dependencyVersions.begin(), end = dependencyVersions.end();
os << "Linked against: " << *i; os << "Linked against: " << *i;
for (++i; i != end; ++i) { for (++i; i != end; ++i) {
os << ',' << ' ' << *i; os << ',' << ' ' << *i;
} }
} }
if ((applicationName && *applicationName) || (applicationVersion && *applicationVersion) || dependencyVersions2.size()) { if ((applicationName && *applicationName) || (applicationVersion && *applicationVersion) || dependencyVersions.size()) {
os << '\n' << '\n'; os << '\n' << '\n';
} }
EscapeCodes::setStyle(os); EscapeCodes::setStyle(os);
@ -937,13 +961,14 @@ void ArgumentParser::readArgs(int argc, const char *const *argv)
const bool completionMode = !strcmp(*++argv, "--bash-completion-for"); const bool completionMode = !strcmp(*++argv, "--bash-completion-for");
// determine the index of the current word for completion and the number of arguments to be passed to ArgumentReader // determine the index of the current word for completion and the number of arguments to be passed to ArgumentReader
unsigned int currentWordIndex, argcForReader; unsigned int currentWordIndex = 0, argcForReader;
if (completionMode) { if (completionMode) {
// the first argument after "--bash-completion-for" is the index of the current word // the first argument after "--bash-completion-for" is the index of the current word
try { try {
currentWordIndex = (--argc ? stringToNumber<unsigned int, string>(*(++argv)) : 0); currentWordIndex = (--argc ? stringToNumber<unsigned int, string>(*(++argv)) : 0);
if (argc) { if (argc) {
++argv, --argc; ++argv;
--argc;
} }
} catch (const ConversionException &) { } catch (const ConversionException &) {
currentWordIndex = static_cast<unsigned int>(argc - 1); currentWordIndex = static_cast<unsigned int>(argc - 1);
@ -1100,7 +1125,7 @@ ArgumentCompletionInfo ArgumentParser::determineCompletionInfo(
// determine last detected arg // determine last detected arg
if (completion.lastDetectedArg) { if (completion.lastDetectedArg) {
completion.lastDetectedArgIndex = reader.lastArgDenotation - argv; completion.lastDetectedArgIndex = static_cast<size_t>(reader.lastArgDenotation - argv);
completion.lastDetectedArgPath = completion.lastDetectedArg->path(completion.lastDetectedArg->occurrences() - 1); completion.lastDetectedArgPath = completion.lastDetectedArg->path(completion.lastDetectedArg->occurrences() - 1);
} }
@ -1309,7 +1334,14 @@ void ArgumentParser::printBashCompletion(int argc, const char *const *argv, unsi
} else { } else {
opening = *completionInfo.lastSpecifiedArg; opening = *completionInfo.lastSpecifiedArg;
} }
*opening == '-' && (++opening, ++openingDenotationType) && *opening == '-' && (++opening, ++openingDenotationType); if (*opening == '-') {
++opening;
++openingDenotationType;
if (*opening == '-') {
++opening;
++openingDenotationType;
}
}
openingLen = strlen(opening); openingLen = strlen(opening);
} }
@ -1362,7 +1394,8 @@ void ArgumentParser::printBashCompletion(int argc, const char *const *argv, unsi
cout << *i; cout << *i;
} }
} }
++i, ++wordIndex; ++i;
++wordIndex;
switch (*i) { switch (*i) {
case ' ': case ' ':
case '\n': case '\n':
@ -1566,7 +1599,8 @@ void ArgumentParser::checkConstraints(const ArgumentVector &args)
ss << "provided. You have to provide the following parameter:"; ss << "provided. You have to provide the following parameter:";
size_t valueNamesPrint = 0; size_t valueNamesPrint = 0;
for (const auto &name : arg->m_valueNames) { for (const auto &name : arg->m_valueNames) {
ss << ' ' << name, ++valueNamesPrint; ss << ' ' << name;
++valueNamesPrint;
} }
if (arg->m_requiredValueCount != Argument::varValueCount) { if (arg->m_requiredValueCount != Argument::varValueCount) {
while (valueNamesPrint < arg->m_requiredValueCount) { while (valueNamesPrint < arg->m_requiredValueCount) {

View File

@ -271,7 +271,8 @@ void ArgumentParserTests::testParsing()
CPPUNIT_ASSERT_THROW(fileArg.values().at(1), out_of_range); CPPUNIT_ASSERT_THROW(fileArg.values().at(1), out_of_range);
// constraint checking: no multiple occurrences (not resetting verboseArg on purpose) // constraint checking: no multiple occurrences (not resetting verboseArg on purpose)
displayFileInfoArg.reset(), fileArg.reset(); displayFileInfoArg.reset();
fileArg.reset();
try { try {
parser.parseArgs(4, argv4); parser.parseArgs(4, argv4);
CPPUNIT_FAIL("Exception expected."); CPPUNIT_FAIL("Exception expected.");
@ -281,7 +282,8 @@ void ArgumentParserTests::testParsing()
} }
// constraint checking: no contraint (not resetting verboseArg on purpose) // constraint checking: no contraint (not resetting verboseArg on purpose)
displayFileInfoArg.reset(), fileArg.reset(); displayFileInfoArg.reset();
fileArg.reset();
verboseArg.setConstraints(0, Argument::varValueCount); verboseArg.setConstraints(0, Argument::varValueCount);
parser.parseArgs(4, argv4); parser.parseArgs(4, argv4);
CPPUNIT_ASSERT(!qtConfigArgs.qtWidgetsGuiArg().isPresent()); CPPUNIT_ASSERT(!qtConfigArgs.qtWidgetsGuiArg().isPresent());
@ -294,7 +296,9 @@ void ArgumentParserTests::testParsing()
// contraint checking: error about missing mandatory argument // contraint checking: error about missing mandatory argument
const char *argv5[] = { "tageditor", "-i", "-f", "test" }; const char *argv5[] = { "tageditor", "-i", "-f", "test" };
displayFileInfoArg.reset(), fileArg.reset(), verboseArg.reset(); displayFileInfoArg.reset();
fileArg.reset();
verboseArg.reset();
try { try {
parser.parseArgs(4, argv5); parser.parseArgs(4, argv5);
CPPUNIT_FAIL("Exception expected."); CPPUNIT_FAIL("Exception expected.");