Improve readability of ArgumentReader::read and use Phrases

This commit is contained in:
Martchus 2017-10-19 00:48:05 +02:00
parent e459dca98d
commit 46f652ad00
2 changed files with 160 additions and 151 deletions

View File

@ -18,6 +18,7 @@
using namespace std;
using namespace std::placeholders;
using namespace ConversionUtilities;
using namespace EscapeCodes;
using namespace IoUtilities;
/*!
@ -97,11 +98,14 @@ void ArgumentReader::read(ArgumentVector &args)
// iterate through all argument denotations; loop might exit earlier when an denotation is unknown
while (argv != end) {
if (values && lastArgInLevel->requiredValueCount() != static_cast<size_t>(-1) && values->size() < lastArgInLevel->requiredValueCount()) {
// there are still values to read
// check whether there are still values to read
if (values && lastArgInLevel->requiredValueCount() != Argument::varValueCount && values->size() < lastArgInLevel->requiredValueCount()) {
// read arg as value and continue with next arg
values->emplace_back(argDenotation ? argDenotation : *argv);
++index, ++argv, argDenotation = nullptr;
} else {
continue;
}
// determine how denotation must be processed
bool abbreviationFound = false;
if (argDenotation) {
@ -177,7 +181,11 @@ void ArgumentReader::read(ArgumentVector &args)
}
}
if (!matchingArg) {
// continue with next arg if we've got a match already
if (matchingArg) {
continue;
}
// unknown argument might be a sibling of the parent element
if (argDenotationType != Value) {
for (auto parentArgument = parentPath.crbegin(), pathEnd = parentPath.crend();; ++parentArgument) {
@ -251,7 +259,7 @@ void ArgumentReader::read(ArgumentVector &args)
} else {
switch (parser.m_unknownArgBehavior) {
case UnknownArgumentBehavior::Warn:
cerr << "The specified argument \"" << *argv << "\" is unknown and will be ignored." << endl;
cerr << Phrases::Warning << "The specified argument \"" << *argv << "\" is unknown and will be ignored." << Phrases::EndFlush;
FALLTHROUGH;
case UnknownArgumentBehavior::Ignore:
// ignore unknown denotation
@ -261,8 +269,6 @@ void ArgumentReader::read(ArgumentVector &args)
throw Failure(argsToString("The specified argument \"", *argv, "\" is unknown."));
}
}
} // if(!matchingArg)
} // no values to read
} // while(argv != end)
}

View File

@ -9,6 +9,7 @@
#include "../application/failure.h"
#include "../application/fakeqtconfigarguments.h"
#include "../io/ansiescapecodes.h"
#include "../io/path.h"
#include "resources/config.h"
@ -187,6 +188,7 @@ void ArgumentParserTests::testParsing()
stringstream buffer;
streambuf *regularCerrBuffer = cerr.rdbuf(buffer.rdbuf());
parser.resetArgs();
EscapeCodes::enabled = false;
try {
parser.parseArgs(6, argv3);
} catch (...) {
@ -194,11 +196,11 @@ void ArgumentParserTests::testParsing()
throw;
}
cerr.rdbuf(regularCerrBuffer);
CPPUNIT_ASSERT_EQUAL("The specified argument \"album\" is unknown and will be ignored.\n"s
"The specified argument \"title\" is unknown and will be ignored.\n"s
"The specified argument \"diskpos\" is unknown and will be ignored.\n"s
"The specified argument \"--files\" is unknown and will be ignored.\n"s
"The specified argument \"somefile\" is unknown and will be ignored.\n"s,
CPPUNIT_ASSERT_EQUAL("Warning: The specified argument \"album\" is unknown and will be ignored.\n"s
"Warning: The specified argument \"title\" is unknown and will be ignored.\n"s
"Warning: The specified argument \"diskpos\" is unknown and will be ignored.\n"s
"Warning: The specified argument \"--files\" is unknown and will be ignored.\n"s
"Warning: The specified argument \"somefile\" is unknown and will be ignored.\n"s,
buffer.str());
// none of the arguments should be present now
CPPUNIT_ASSERT(!qtConfigArgs.qtWidgetsGuiArg().isPresent());
@ -666,6 +668,7 @@ void ArgumentParserTests::testHelp()
" default environment variable: FILES\n"
"\n"
"Project website: " APP_URL "\n");
EscapeCodes::enabled = true;
parser.parseArgs(2, argv);
}
}