Skip to content

Commit 940679e

Browse files
committed
Fix ticket cppcheck-opensource#2040 (some cli messages are missing a return at the end)
1 parent df4eec7 commit 940679e

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ CmdLineParser::CmdLineParser(Settings *settings)
6060
{
6161
}
6262

63+
void CmdLineParser::PrintMessage(const std::string &message)
64+
{
65+
std::cout << message << std::endl;
66+
}
67+
6368
bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
6469
{
6570
for (int i = 1; i < argc; i++)
@@ -100,14 +105,17 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
100105

101106
if (i >= argc)
102107
{
103-
std::cout << "cppcheck: No file specified for the --suppressions option";
108+
PrintMessage("cppcheck: No file specified for the --suppressions option");
104109
return false;
105110
}
106111

107112
std::ifstream f(argv[i]);
108113
if (!f.is_open())
109114
{
110-
std::cout << "cppcheck: Couldn't open the file \"" << std::string(argv[i]) << "\"";
115+
std::string message("cppcheck: Couldn't open the file \"");
116+
message += std::string(argv[i]);
117+
message += "\"";
118+
PrintMessage(message);
111119
return false;
112120
}
113121
const std::string errmsg(_settings->nomsg.parseFile(f));
@@ -172,7 +180,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
172180
if (!(iss >> _settings->_exitCode))
173181
{
174182
_settings->_exitCode = 0;
175-
std::cout << "cppcheck: Argument must be an integer. Try something like '--error-exitcode=1'";
183+
PrintMessage("cppcheck: Argument must be an integer. Try something like '--error-exitcode=1'");
176184
return false;
177185
}
178186
}
@@ -199,7 +207,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
199207
++i;
200208
if (i >= argc)
201209
{
202-
std::cout << "cppcheck: argument to '-I' is missing";
210+
PrintMessage("cppcheck: argument to '-I' is missing");
203211
return false;
204212
}
205213
path = argv[i];
@@ -239,7 +247,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
239247
++i;
240248
if (i >= argc)
241249
{
242-
std::cout << "cppcheck: argument to '--template' is missing";
250+
PrintMessage("cppcheck: argument to '--template' is missing");
243251
return false;
244252
}
245253

@@ -262,7 +270,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
262270
++i;
263271
if (i >= argc)
264272
{
265-
std::cout << "cppcheck: argument to '-j' is missing";
273+
PrintMessage("cppcheck: argument to '-j' is missing");
266274
return false;
267275
}
268276

@@ -279,13 +287,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
279287
std::istringstream iss(numberString);
280288
if (!(iss >> _settings->_jobs))
281289
{
282-
std::cout << "cppcheck: argument to '-j' is not a number";
290+
PrintMessage("cppcheck: argument to '-j' is not a number");
283291
return false;
284292
}
285293

286294
if (_settings->_jobs > 1000)
287295
{
288-
std::cout << "cppcheck: argument for '-j' is allowed to be 1000 at max";
296+
PrintMessage("cppcheck: argument for '-j' is allowed to be 1000 at max");
289297
return false;
290298
}
291299
}
@@ -353,7 +361,10 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
353361

354362
else if (strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0)
355363
{
356-
std::cout << "cppcheck: error: unrecognized command line option \"" << argv[i] << "\"";
364+
std::string message("cppcheck: error: unrecognized command line option \"");
365+
message += argv[i];
366+
message += "\"";
367+
PrintMessage(message);
357368
return false;
358369
}
359370

@@ -363,13 +374,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
363374

364375
if (_settings->isEnabled("unusedFunctions") && _settings->_jobs > 1)
365376
{
366-
std::cout << "unusedFunctions check can't be used with -j option, so it was disabled.";
377+
PrintMessage("unusedFunctions check can't be used with -j option, so it was disabled.");
367378
}
368379

369380
// FIXME: Make the _settings.test_2_pass thread safe
370381
if (_settings->test_2_pass && _settings->_jobs > 1)
371382
{
372-
std::cout << "--test-2-pass doesn't work with -j option yet.";
383+
PrintMessage("--test-2-pass doesn't work with -j option yet.");
373384
}
374385

375386

@@ -382,7 +393,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
382393
}
383394
else if (_pathnames.empty())
384395
{
385-
std::cout << "cppcheck: No C or C++ source files found.";
396+
PrintMessage("cppcheck: No C or C++ source files found.");
386397
return false;
387398
}
388399

cli/cmdlineparser.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ class CmdLineParser
9191
*/
9292
void PrintHelp();
9393

94+
/**
95+
* Print message (to console?).
96+
*/
97+
void PrintMessage(const std::string &message);
98+
9499
private:
95100
Settings *_settings;
96101
bool _showHelp;

0 commit comments

Comments
 (0)