Skip to content

Commit

Permalink
CmdLineParser: fixed overriding --plist-output with `--output-forma…
Browse files Browse the repository at this point in the history
…t` (#7288)
  • Loading branch information
firewave authored Feb 7, 2025
1 parent b4ff60a commit abe99cb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 7 additions & 3 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1011,15 +1011,18 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a

else if (std::strncmp(argv[i], "--output-format=", 16) == 0) {
const std::string format = argv[i] + 16;
// TODO: text and plist is missing
if (format == "sarif")
// plist can not be handled here because it requires additional data
if (format == "text")
mSettings.outputFormat = Settings::OutputFormat::text;
else if (format == "sarif")
mSettings.outputFormat = Settings::OutputFormat::sarif;
else if (format == "xml")
mSettings.outputFormat = Settings::OutputFormat::xml;
else {
mLogger.printError("argument to '--output-format=' must be 'sarif' or 'xml'.");
mLogger.printError("argument to '--output-format=' must be 'text', 'sarif' or 'xml'.");
return Result::Fail;
}
mSettings.plistOutput = "";
}


Expand Down Expand Up @@ -1775,6 +1778,7 @@ void CmdLineParser::printHelp() const
" --output-file=<file> Write results to file, rather than standard error.\n"
" --output-format=<format>\n"
" Specify the output format. The available formats are:\n"
" * text\n"
" * sarif\n"
" * xml\n"
" --platform=<type>, --platform=<file>\n"
Expand Down
22 changes: 20 additions & 2 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,13 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(maxConfigsMissingCount);
TEST_CASE(maxConfigsInvalid);
TEST_CASE(maxConfigsTooSmall);
TEST_CASE(outputFormatText);
TEST_CASE(outputFormatSarif);
TEST_CASE(outputFormatXml);
TEST_CASE(outputFormatOther);
TEST_CASE(outputFormatImplicitPlist);
TEST_CASE(outputFormatImplicitXml);
TEST_CASE(outputFormatOverridePlist);
TEST_CASE(premiumOptions1);
TEST_CASE(premiumOptions2);
TEST_CASE(premiumOptions3);
Expand Down Expand Up @@ -1279,6 +1281,13 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: argument to '--max-configs=' must be greater than 0.\n", logger->str());
}

void outputFormatText() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--output-format=text", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS_ENUM(Settings::OutputFormat::text, settings->outputFormat);
}

void outputFormatSarif() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--output-format=sarif", "file.cpp"};
Expand All @@ -1295,16 +1304,17 @@ class TestCmdlineParser : public TestFixture {

void outputFormatOther() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--output-format=text", "file.cpp"};
const char * const argv[] = {"cppcheck", "--output-format=plist", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: argument to '--output-format=' must be 'sarif' or 'xml'.\n", logger->str());
ASSERT_EQUALS("cppcheck: error: argument to '--output-format=' must be 'text', 'sarif' or 'xml'.\n", logger->str());
}

void outputFormatImplicitPlist() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--plist-output=.", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS_ENUM(Settings::OutputFormat::plist, settings->outputFormat);
ASSERT_EQUALS("./", settings->plistOutput);
}

void outputFormatImplicitXml() {
Expand All @@ -1314,6 +1324,14 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS_ENUM(Settings::OutputFormat::xml, settings->outputFormat);
}

void outputFormatOverridePlist() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--plist-output=.", "--output-format=text", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(4, argv));
ASSERT_EQUALS_ENUM(Settings::OutputFormat::text, settings->outputFormat);
ASSERT_EQUALS("", settings->plistOutput);
}

void premiumOptions1() {
REDIRECT;
asPremium();
Expand Down

0 comments on commit abe99cb

Please sign in to comment.