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;
using namespace std::placeholders; using namespace std::placeholders;
using namespace ConversionUtilities; using namespace ConversionUtilities;
using namespace EscapeCodes;
using namespace IoUtilities; 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 // iterate through all argument denotations; loop might exit earlier when an denotation is unknown
while (argv != end) { while (argv != end) {
if (values && lastArgInLevel->requiredValueCount() != static_cast<size_t>(-1) && values->size() < lastArgInLevel->requiredValueCount()) { // check whether there are still values to read
// 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); values->emplace_back(argDenotation ? argDenotation : *argv);
++index, ++argv, argDenotation = nullptr; ++index, ++argv, argDenotation = nullptr;
} else { continue;
}
// determine how denotation must be processed // determine how denotation must be processed
bool abbreviationFound = false; bool abbreviationFound = false;
if (argDenotation) { 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 // unknown argument might be a sibling of the parent element
if (argDenotationType != Value) { if (argDenotationType != Value) {
for (auto parentArgument = parentPath.crbegin(), pathEnd = parentPath.crend();; ++parentArgument) { for (auto parentArgument = parentPath.crbegin(), pathEnd = parentPath.crend();; ++parentArgument) {
@ -251,7 +259,7 @@ void ArgumentReader::read(ArgumentVector &args)
} else { } else {
switch (parser.m_unknownArgBehavior) { switch (parser.m_unknownArgBehavior) {
case UnknownArgumentBehavior::Warn: 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; FALLTHROUGH;
case UnknownArgumentBehavior::Ignore: case UnknownArgumentBehavior::Ignore:
// ignore unknown denotation // ignore unknown denotation
@ -261,8 +269,6 @@ void ArgumentReader::read(ArgumentVector &args)
throw Failure(argsToString("The specified argument \"", *argv, "\" is unknown.")); throw Failure(argsToString("The specified argument \"", *argv, "\" is unknown."));
} }
} }
} // if(!matchingArg)
} // no values to read
} // while(argv != end) } // while(argv != end)
} }

View File

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