Browse Source

Fix parsing top-level argument after abbreviation

experiment/improve_get_date_part
Martchus 4 years ago
parent
commit
a3192a1113
  1. 10
      application/argumentparser.cpp
  2. 14
      tests/argumentparsertests.cpp

10
application/argumentparser.cpp

@ -185,10 +185,14 @@ void ArgumentReader::read(ArgumentVector &args)
argDenotation = nullptr;
break;
} else {
// further abbreviations follow -> don't increment argv, keep processing outstanding chars of argDenotation
// further abbreviations follow -> remember current arg value
const char *const *currentArgValue = argv;
// don't increment argv, keep processing outstanding chars of argDenotation
read(lastArg->m_subArgs);
// stop further processing if denotation has been consumed
if (!argDenotation) {
// stop further processing if the denotation has been consumed or even the next value has already been loaded
if (!argDenotation || currentArgValue != argv) {
argDenotation = nullptr;
break;
}
}

14
tests/argumentparsertests.cpp

@ -125,7 +125,8 @@ void ArgumentParserTests::testParsing()
Argument displayTagInfoArg("get", 'p', "displays the values of all specified tag fields (displays all fields if none specified)");
displayTagInfoArg.setDenotesOperation(true);
displayTagInfoArg.setSubArguments({ &fieldsArg, &filesArg, &verboseArg, &notAlbumArg });
parser.setMainArguments({ &qtConfigArgs.qtWidgetsGuiArg(), &printFieldNamesArg, &displayTagInfoArg, &displayFileInfoArg, &helpArg });
NoColorArgument noColorArg;
parser.setMainArguments({ &qtConfigArgs.qtWidgetsGuiArg(), &printFieldNamesArg, &displayTagInfoArg, &displayFileInfoArg, &helpArg, &noColorArg });
// error about uncombinable arguments
const char *argv[] = { "tageditor", "get", "album", "title", "diskpos", "-f", "somefile" };
@ -313,6 +314,17 @@ void ArgumentParserTests::testParsing()
CPPUNIT_ASSERT_EQUAL(1_st, fileArg.values(0).size());
CPPUNIT_ASSERT(!strcmp(fileArg.values(0).front(), "test"));
// specifying top-level argument after abbreviation
const char *argv17[] = { "tageditor", "-if=test-v", "--no-color" };
parser.resetArgs();
parser.parseArgs(3, argv17);
CPPUNIT_ASSERT(!filesArg.isPresent());
CPPUNIT_ASSERT(fileArg.isPresent());
CPPUNIT_ASSERT(!verboseArg.isPresent());
CPPUNIT_ASSERT(noColorArg.isPresent());
CPPUNIT_ASSERT_EQUAL(1_st, fileArg.values(0).size());
CPPUNIT_ASSERT_EQUAL("test-v"s, string(fileArg.values(0).front()));
// default argument
const char *argv8[] = { "tageditor" };
parser.resetArgs();

Loading…
Cancel
Save