Skip to content

Commit

Permalink
CmdLineParser: parse --check-level= like other options with values
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Apr 8, 2024
1 parent 02f1ed0 commit 8161ca2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
20 changes: 14 additions & 6 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,21 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
else if (std::strcmp(argv[i], "--check-config") == 0)
mSettings.checkConfiguration = true;

// Check code exhaustively
else if (std::strcmp(argv[i], "--check-level=exhaustive") == 0)
mSettings.setCheckLevel(Settings::CheckLevel::exhaustive);
// Check level
else if (std::strncmp(argv[i], "--check-level=", 14) == 0) {
Settings::CheckLevel level = Settings::CheckLevel::normal;
const std::string level_s(argv[i] + 14);
if (level_s == "normal")
level = Settings::CheckLevel::normal;
else if (level_s == "exhaustive")
level = Settings::CheckLevel::exhaustive;
else {
mLogger.printError("unknown '--check-level' value '" + level_s + "'.");
return Result::Fail;
}

// Check code with normal analysis
else if (std::strcmp(argv[i], "--check-level=normal") == 0)
mSettings.setCheckLevel(Settings::CheckLevel::normal);
mSettings.setCheckLevel(level);
}

// Check library definitions
else if (std::strcmp(argv[i], "--check-library") == 0) {
Expand Down
8 changes: 8 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(checkLevelDefault);
TEST_CASE(checkLevelNormal);
TEST_CASE(checkLevelExhaustive);
TEST_CASE(checkLevelUnknown);

TEST_CASE(ignorepaths1);
TEST_CASE(ignorepaths2);
Expand Down Expand Up @@ -2526,6 +2527,13 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS(256, settings->performanceValueFlowMaxSubFunctionArgs);
}

void checkLevelUnknown() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--check-level=default", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: unknown '--check-level' value 'default'.\n", logger->str());
}

void ignorepaths1() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};
Expand Down

0 comments on commit 8161ca2

Please sign in to comment.