Browse Source

Util+Test: Implement exit on first error in main ArgParser loop

master
Riyyi 3 years ago
parent
commit
dc4fcc10ce
  1. 4
      src/util/argparser.cpp
  2. 29
      test/testutilargparser.cpp

4
src/util/argparser.cpp

@ -241,6 +241,10 @@ bool ArgParser::parse(int argc, const char* argv[])
}
printf("-> argu: '%s'", argument.data());
}
if (m_exitOnFirstError && !result) {
break;
}
}
return result;

29
test/testutilargparser.cpp

@ -461,8 +461,8 @@ TEST_CASE(VectorStringOptions)
EXPECT_EQ(result, true);
EXPECT_EQ(vectorOpt1.size(), 2);
if (vectorOpt1.size() == 2) {
EXPECT_EQ(vectorOpt1[0], "hello");
EXPECT_EQ(vectorOpt1[1], "world");
EXPECT_EQ(vectorOpt1.at(0), "hello");
EXPECT_EQ(vectorOpt1.at(1), "world");
}
}
@ -590,4 +590,29 @@ TEST_CASE(StopOnFirstNonOption)
EXPECT_EQ(result, true);
EXPECT_EQ(boolOpt1, true);
EXPECT_EQ(boolOpt2, false);
// -----------------------------------------
TEST_CASE(ExitOnFirstError)
{
// Do not stop on first error, one non-existing given
// Expected: parsing fails, boolOpt1 is set
bool boolOpt1 = false;
auto result = runParser({ "--this-doesnt-exist", "--this-exist" }, [&](auto& parser) {
parser.setExitOnFirstError(false);
parser.addOption(boolOpt1, '\0', "this-exist", nullptr, nullptr);
});
EXPECT_EQ(result, false);
EXPECT_EQ(boolOpt1, true);
// Stop on first error, one non-existing given
// Expected: parsing fails, boolOpt1 is not set
boolOpt1 = false;
result = runParser({ "--this-doesnt-exist", "--this-exist" }, [&](auto& parser) {
parser.setExitOnFirstError(true);
parser.addOption(boolOpt1, '\0', "this-exist", nullptr, nullptr);
});
EXPECT_EQ(result, false);
EXPECT_EQ(boolOpt1, false);
}
}

Loading…
Cancel
Save