Skip to content

Commit abe99cb

Browse files
authored
CmdLineParser: fixed overriding --plist-output with --output-format (#7288)
1 parent b4ff60a commit abe99cb

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

cli/cmdlineparser.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,15 +1011,18 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10111011

10121012
else if (std::strncmp(argv[i], "--output-format=", 16) == 0) {
10131013
const std::string format = argv[i] + 16;
1014-
// TODO: text and plist is missing
1015-
if (format == "sarif")
1014+
// plist can not be handled here because it requires additional data
1015+
if (format == "text")
1016+
mSettings.outputFormat = Settings::OutputFormat::text;
1017+
else if (format == "sarif")
10161018
mSettings.outputFormat = Settings::OutputFormat::sarif;
10171019
else if (format == "xml")
10181020
mSettings.outputFormat = Settings::OutputFormat::xml;
10191021
else {
1020-
mLogger.printError("argument to '--output-format=' must be 'sarif' or 'xml'.");
1022+
mLogger.printError("argument to '--output-format=' must be 'text', 'sarif' or 'xml'.");
10211023
return Result::Fail;
10221024
}
1025+
mSettings.plistOutput = "";
10231026
}
10241027

10251028

@@ -1775,6 +1778,7 @@ void CmdLineParser::printHelp() const
17751778
" --output-file=<file> Write results to file, rather than standard error.\n"
17761779
" --output-format=<format>\n"
17771780
" Specify the output format. The available formats are:\n"
1781+
" * text\n"
17781782
" * sarif\n"
17791783
" * xml\n"
17801784
" --platform=<type>, --platform=<file>\n"

test/testcmdlineparser.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,13 @@ class TestCmdlineParser : public TestFixture {
208208
TEST_CASE(maxConfigsMissingCount);
209209
TEST_CASE(maxConfigsInvalid);
210210
TEST_CASE(maxConfigsTooSmall);
211+
TEST_CASE(outputFormatText);
211212
TEST_CASE(outputFormatSarif);
212213
TEST_CASE(outputFormatXml);
213214
TEST_CASE(outputFormatOther);
214215
TEST_CASE(outputFormatImplicitPlist);
215216
TEST_CASE(outputFormatImplicitXml);
217+
TEST_CASE(outputFormatOverridePlist);
216218
TEST_CASE(premiumOptions1);
217219
TEST_CASE(premiumOptions2);
218220
TEST_CASE(premiumOptions3);
@@ -1279,6 +1281,13 @@ class TestCmdlineParser : public TestFixture {
12791281
ASSERT_EQUALS("cppcheck: error: argument to '--max-configs=' must be greater than 0.\n", logger->str());
12801282
}
12811283

1284+
void outputFormatText() {
1285+
REDIRECT;
1286+
const char * const argv[] = {"cppcheck", "--output-format=text", "file.cpp"};
1287+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
1288+
ASSERT_EQUALS_ENUM(Settings::OutputFormat::text, settings->outputFormat);
1289+
}
1290+
12821291
void outputFormatSarif() {
12831292
REDIRECT;
12841293
const char * const argv[] = {"cppcheck", "--output-format=sarif", "file.cpp"};
@@ -1295,16 +1304,17 @@ class TestCmdlineParser : public TestFixture {
12951304

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

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

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

1327+
void outputFormatOverridePlist() {
1328+
REDIRECT;
1329+
const char * const argv[] = {"cppcheck", "--plist-output=.", "--output-format=text", "file.cpp"};
1330+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(4, argv));
1331+
ASSERT_EQUALS_ENUM(Settings::OutputFormat::text, settings->outputFormat);
1332+
ASSERT_EQUALS("", settings->plistOutput);
1333+
}
1334+
13171335
void premiumOptions1() {
13181336
REDIRECT;
13191337
asPremium();

0 commit comments

Comments
 (0)