Refactorized cmdlineparser.cpp:

- Added const char* overload for PrintMessage()
- Call Path::removeQuotationMarks() before any other path formatting functions
- Fixed --help output on terminal outputs with 80 columns
- Hide -l option on systems where it does not do anything
- Added pre-check for "-" at the beginning to distinguish between options and files early
This commit is contained in:
PKEuS 2015-11-20 12:01:04 +01:00
parent 00bdc89f98
commit 6235515889
2 changed files with 548 additions and 537 deletions

View File

@ -24,6 +24,7 @@
#include "settings.h" #include "settings.h"
#include "timer.h" #include "timer.h"
#include "check.h" #include "check.h"
#include "threadexecutor.h" // Threading model
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
@ -67,8 +68,8 @@ static void AddInclPathsToList(const std::string& FileList, std::list<std::strin
std::string PathName; std::string PathName;
while (std::getline(Files, PathName)) { // next line while (std::getline(Files, PathName)) { // next line
if (!PathName.empty()) { if (!PathName.empty()) {
PathName = Path::fromNativeSeparators(PathName);
PathName = Path::removeQuotationMarks(PathName); PathName = Path::removeQuotationMarks(PathName);
PathName = Path::fromNativeSeparators(PathName);
// If path doesn't end with / or \, add it // If path doesn't end with / or \, add it
if (PathName.back() != '/') if (PathName.back() != '/')
@ -101,12 +102,18 @@ void CmdLineParser::PrintMessage(const std::string &message)
std::cout << message << std::endl; std::cout << message << std::endl;
} }
void CmdLineParser::PrintMessage(const char* message)
{
std::cout << message << std::endl;
}
bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
{ {
bool def = false; bool def = false;
bool maxconfigs = false; bool maxconfigs = false;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
if (std::strcmp(argv[i], "--version") == 0) { if (std::strcmp(argv[i], "--version") == 0) {
_showVersion = true; _showVersion = true;
_exitAfterPrint = true; _exitAfterPrint = true;
@ -399,8 +406,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
else { else {
path = 2 + argv[i]; path = 2 + argv[i];
} }
path = Path::fromNativeSeparators(path);
path = Path::removeQuotationMarks(path); path = Path::removeQuotationMarks(path);
path = Path::fromNativeSeparators(path);
// If path doesn't end with / or \, add it // If path doesn't end with / or \, add it
if (path.back() != '/') if (path.back() != '/')
@ -451,9 +458,9 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
} }
if (!path.empty()) { if (!path.empty()) {
path = Path::removeQuotationMarks(path);
path = Path::fromNativeSeparators(path); path = Path::fromNativeSeparators(path);
path = Path::simplifyPath(path); path = Path::simplifyPath(path);
path = Path::removeQuotationMarks(path);
if (FileLister::isDirectory(path)) { if (FileLister::isDirectory(path)) {
// If directory name doesn't end with / or \, add it // If directory name doesn't end with / or \, add it
@ -710,17 +717,18 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
break; break;
} }
else if (argv[i][0] == '-') { else {
std::string message("cppcheck: error: unrecognized command line option: \""); std::string message("cppcheck: error: unrecognized command line option: \"");
message += argv[i]; message += argv[i];
message += "\"."; message += "\".";
PrintMessage(message); PrintMessage(message);
return false; return false;
} }
}
else { else {
std::string path = Path::fromNativeSeparators(argv[i]); std::string path = Path::removeQuotationMarks(argv[i]);
path = Path::removeQuotationMarks(path); path = Path::fromNativeSeparators(path);
_pathnames.push_back(path); _pathnames.push_back(path);
} }
} }
@ -779,10 +787,10 @@ void CmdLineParser::PrintHelp()
" --check-library Show information messages when library files have\n" " --check-library Show information messages when library files have\n"
" incomplete info.\n" " incomplete info.\n"
" --config-exclude=<dir>\n" " --config-exclude=<dir>\n"
" Path (prefix) to be excluded from configuration checking.\n" " Path (prefix) to be excluded from configuration\n"
" Preprocessor configurations defined in headers (but not sources)\n" " checking. Preprocessor configurations defined in\n"
" matching the prefix will not be considered for evaluation\n" " headers (but not sources) matching the prefix will not\n"
" of configuration alternatives\n" " be considered for evaluation.\n"
" --config-excludes-file=<file>\n" " --config-excludes-file=<file>\n"
" A file that contains a list of config-excludes\n" " A file that contains a list of config-excludes\n"
" --dump Dump xml data for each translation unit. The dump\n" " --dump Dump xml data for each translation unit. The dump\n"
@ -865,10 +873,12 @@ void CmdLineParser::PrintHelp()
" --inline-suppr Enable inline suppressions. Use them by placing one or\n" " --inline-suppr Enable inline suppressions. Use them by placing one or\n"
" more comments, like: '// cppcheck-suppress warningId'\n" " more comments, like: '// cppcheck-suppress warningId'\n"
" on the lines before the warning to suppress.\n" " on the lines before the warning to suppress.\n"
" -j <jobs> Start [jobs] threads to do the checking simultaneously.\n" " -j <jobs> Start <jobs> threads to do the checking simultaneously.\n"
" -l <load> Specifies that no new threads should be started if there\n" #ifdef THREADING_MODEL_FORK
" are other threads running and the load average is at least\n" " -l <load> Specifies that no new threads should be started if\n"
" load (ignored on non UNIX-like systems)\n" " there are other threads running and the load average is\n"
" at least <load>.\n"
#endif
" --language=<language>, -x <language>\n" " --language=<language>, -x <language>\n"
" Forces cppcheck to check all files as the given\n" " Forces cppcheck to check all files as the given\n"
" language. Valid values are: c, c++\n" " language. Valid values are: c, c++\n"
@ -914,7 +924,7 @@ void CmdLineParser::PrintHelp()
#ifdef HAVE_RULES #ifdef HAVE_RULES
" --rule=<rule> Match regular expression.\n" " --rule=<rule> Match regular expression.\n"
" --rule-file=<file> Use given rule file. For more information, see: \n" " --rule-file=<file> Use given rule file. For more information, see: \n"
" https://sourceforge.net/projects/cppcheck/files/Articles/\n" " http://sourceforge.net/projects/cppcheck/files/Articles/\n"
#endif #endif
" --std=<id> Set standard.\n" " --std=<id> Set standard.\n"
" The available options are:\n" " The available options are:\n"

View File

@ -104,6 +104,7 @@ protected:
* Print message (to console?). * Print message (to console?).
*/ */
static void PrintMessage(const std::string &message); static void PrintMessage(const std::string &message);
static void PrintMessage(const char* message);
private: private:
std::vector<std::string> _pathnames; std::vector<std::string> _pathnames;