Browse Source

Merge pull request #710 from lassoan/fix-multi-value-cmdline-arg-parsing

BUG: Fixed multi-value argument parsing
Jean-Christophe Fillion-Robin 8 years ago
parent
commit
185e6e3ffa

+ 1 - 1
Libs/Core/Testing/Cpp/ctkCommandLineParserTest1.cpp

@@ -92,9 +92,9 @@ int ctkCommandLineParserTest1(int, char*[])
   arguments3 << "ctkCommandLineParserTest1";
   arguments3 << "--test-string" << "TestingIsGood";
   arguments3 << "--test-string2"<< "CTKSuperRocks";
-  arguments3 << "--test-integer"<< "-3";
 //  arguments3 << "--test-double"<< "-3.14";
   arguments3 << "--test-stringlist"<< "item1" << "item2" << "item3";
+  arguments3 << "--test-integer"<< "-3"; // test if argument is recognized after multi-value argument
   ctkCommandLineParser parser3;
   parser3.addArgument("--test-string", "", QVariant::String, "This is a test string");
   parser3.addArgument("--test-string2", "", QVariant::String, "This is a test string2", "CTKGood");

+ 7 - 2
Libs/Core/ctkCommandLineParser.cpp

@@ -447,8 +447,10 @@ QHash<QString, QVariant> ctkCommandLineParser::parseArguments(const QStringList&
             {
             qDebug() << "  Processing parameter" << j << ", value:" << parameter;
             }
-          if (this->argumentAdded(parameter))
+          if (this->Internal->argumentDescription(parameter) != 0)
             {
+            // we've found a known argument, it means there are no more
+            // parameter for the current argument
             this->Internal->ErrorString =
                 missingParameterError.arg(argument).arg(j-1).arg(numberOfParametersToProcess);
             if (this->Internal->Debug) { qDebug() << this->Internal->ErrorString; }
@@ -478,12 +480,15 @@ QHash<QString, QVariant> ctkCommandLineParser::parseArguments(const QStringList&
         int j = 1;
         while(j + i < arguments.size())
           {
-          if (this->argumentAdded(arguments.at(j + i)))
+          if (this->Internal->argumentDescription(arguments.at(j + i)) != 0)
             {
+            // we've found a known argument, it means there are no more
+            // parameter for the current argument
             if (this->Internal->Debug)
               {
               qDebug() << "  No more parameter for" << argument;
               }
+            j--; // this parameter does not belong to current argument
             break;
             }
           QString parameter = arguments.at(j + i);