Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
diridari committed Jun 12, 2019
2 parents 550d10d + 3e82eb3 commit 565e7a8
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ find_package(GTest)
if (GTest_FOUND)
message(" -- gtest found at " ${GTEST_INCLUDE_DIRS})
include_directories(${GTEST_INCLUDE_DIRS})
set(tests_src tests/argvParserTest.cpp tests/configReaderTest.cpp tests/exampleTest.cpp tests/definedNumberOfArgs.cpp tests/advancedConfigurationTest.cpp tests/lambdaTest.cpp)
set(tests_src tests/argvParserTest.cpp tests/configReaderTest.cpp tests/exampleTest.cpp tests/definedNumberOfArgs.cpp tests/advancedConfigurationTest.cpp tests/lambdaTest.cpp tests/defaultConfigFileTest.cpp)
add_executable(argParser_Test ${tests_src} ${src})
target_link_libraries(argParser_Test ${GTEST_BOTH_LIBRARIES} pthread)
enable_testing()
Expand Down
25 changes: 22 additions & 3 deletions include/argvParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class argvParser : public argParserAdvancedConfiguration {
* Help message = description + list of configured param + required param
* @param addDefaultHelpCommand add the default help implementation it enables -h or -help with one optional argument
* @param description description of the application
* @param commentToken char's that define the start of a comment the comment ends on the end of the line
*/
explicit argvParser(bool addDefaultHelpCommand = true,string description = "");
explicit argvParser(bool addDefaultHelpCommand = true,string description = "", string commentToken = "");

/**
* Add Argument.
Expand Down Expand Up @@ -136,10 +137,21 @@ class argvParser : public argParserAdvancedConfiguration {
*/
void addSection(string sectionName);

private:
/**
* check the default config file before reading user defined arguments.
* This function defines the location and name of the config files.
* Each location gets checked before parsing the cli arguments.
* At first the first location string gets checked
* the the second one and so on
* so the last defines file location ca overwrite arguments from the first file
* locations are sperated with < > or <,>
*/
void checkForDefaulConfigFilesIn(string defaultConfigFileName, string locations);

private:


string commentToken;

/**
* last failed argument
Expand All @@ -161,7 +173,14 @@ class argvParser : public argParserAdvancedConfiguration {
*/
bool addHelp;

bool genAutoCompl = true;
bool analyseArgvNotJetRun = true;

string nameOfDefaultConfigFile;
string defaultConfigFilesLocations;

bool analyzeConfigFile(string fileName);


};


Expand Down
77 changes: 53 additions & 24 deletions src/argvParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Created by basto on 4/15/18.
//

#include <sstream>
#include "../include/argvParser.h"
#include "configFileReader.h"

Expand All @@ -21,6 +22,8 @@ void resetCLI(){
void printGreen(){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
}
#define FOLDER_SEPERATOR ("\\")
#define ENVIREMENT_HOME (getenv("USERPROFILE"))
#endif
#ifdef __linux__

Expand All @@ -35,7 +38,8 @@ void resetCLI() {
void printGreen() {
cout << "\u001B[1;32m";
}

#define FOLDER_SEPERATOR ( "/")
#define ENVIREMENT_HOME (getenv("HOME"))
#endif

extern int callBackInstallAutoCompletion(int index, char **buff);
Expand Down Expand Up @@ -75,11 +79,11 @@ argvParser::addArg(string argvShort, string argvLong, string help, int (*callBac
}


argvParser::argvParser(bool addDefaultHelpCommand, string description_) : argParserAdvancedConfiguration() {
argvParser::argvParser(bool addDefaultHelpCommand, string description_, string commentToken) : argParserAdvancedConfiguration() {
description = description_ + "\n";
addHelp = addDefaultHelpCommand;
requiredArgs = "";

this->commentToken = commentToken;
}

void argvParser::printHelpMessage(bool colored) {
Expand Down Expand Up @@ -111,21 +115,32 @@ void argvParser::printHelpMessage(bool colored) {


bool argvParser::analyseArgv(int args, char **argv) {

if(analyseArgvNotJetRun) {
analyseArgvNotJetRun = false;
#ifdef __linux__ // auto completion jet just under linux supported
if(genAutoCompl) {
genAutoCompl = false;
this->addSection("Argument auto completion");
this->addArg("-instAutoCompl", "", "install auto completion for cli usage", callBackInstallAutoCompletion)
->addAdditionalHelp(
"This command generates a bash autocompletion script that can be loaded temporary or permanent.");
generateAutoCompletion();
}
#endif
istringstream tokenStream(defaultConfigFilesLocations);
string location = "loc not set";
while(getline(tokenStream,location,' ')){
if(location.size() >0 && location.at(0) == '~'){
location.replace(0,1,ENVIREMENT_HOME);
}
//cout << " open " << location+"/"+nameOfDefaultConfigFile<<endl;
analyzeConfigFile(location+FOLDER_SEPERATOR+nameOfDefaultConfigFile);
}
}

if(addHelp) {
addHelp = false;
this->addSection("utils");
// Default help implementation
this->addArg("-h", "help", "help message or additional infomations about an command e.g. \"help <command>\"",
this->addArg("-h", "help", "help message or additional information's about an command e.g. \"help <command>\"",
[&](int i, char **buff) {
printHelpMessage();
string s = "";
Expand Down Expand Up @@ -160,29 +175,37 @@ bool argvParser::analyseArgv(int args, char **argv) {
}
}
} else { // check config file
configFileReader *reader = new configFileReader(argv[i]);
if (!reader->isEOF()) {
vector<string> *arg = new vector<string>();
arg->push_back("program Name");
while (!reader->isEOF()) {
string param = reader->readUntilNextSeparator();
arg->push_back(param);
}
char *buff[arg->size()];
for (int y = 0; y < arg->size(); y++) {
buff[y] = const_cast<char *>(arg->at(y).c_str());
}
analyseArgv(arg->size(), buff);
delete reader;
} else {
delete reader;
if(!analyzeConfigFile(argv[i]))
return false;
}

}
}
return foundAllRequierdArgs();
}

bool argvParser::analyzeConfigFile(string fileName) {
configFileReader *reader = new configFileReader(fileName);
reader->setCommentChar(commentToken);
if (!reader->isEOF()) {
vector<string> *arg = new vector<string>();
arg->push_back("program Name");
while (!reader->isEOF()) {
string param = reader->readUntilNextSeparator();
arg->push_back(param);
}
char *buff[arg->size()];
for (int y = 0; y < arg->size(); y++) {
buff[y] = const_cast<char *>(arg->at(y).c_str());
}
analyseArgv(arg->size(), buff);
delete reader;
} else {
delete reader;
return false;
}
return true;
}

string argvParser::getHelpMessage() {
string s;
s += description + "usage:\n" + helpMessage + "\n";
Expand Down Expand Up @@ -238,6 +261,12 @@ bool argvParser::checkNextArgumentIfEnum(string arg, char *nextElement) {
return true;
}

void argvParser::checkForDefaulConfigFilesIn(string defaultConfigFileName, string location) {
nameOfDefaultConfigFile = defaultConfigFileName;
defaultConfigFilesLocations = location;
}





Expand Down
23 changes: 20 additions & 3 deletions src/configFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ string configFileReader::readUntilNextSeparator() {
// go to the next element
while (!isEOF()) {
typeOfSeparator tmp = isSeparator(peekNextChar());
if (tmp == typeOfSeparator::none || tmp == typeOfSeparator::separator) {
if (tmp == typeOfSeparator::none || tmp == typeOfSeparator::separator || tmp == typeOfSeparator::isInComment) {
if (tmp == typeOfSeparator::separator) {
isInSeperator = true;
skipNextChar(); // lift over separator
Expand All @@ -29,7 +29,10 @@ string configFileReader::readUntilNextSeparator() {

} else if (isInSeperator && tmp == typeOfSeparator::space) {
out += getNextChar();
} else {
} else if(tmp == typeOfSeparator::isInComment){
skipNextChar();
}
else{
skipNextChar();
if (out == "")
out = readUntilNextSeparator();
Expand All @@ -42,11 +45,21 @@ string configFileReader::readUntilNextSeparator() {
}

configFileReader::typeOfSeparator configFileReader::isSeparator(char toCheck) {
static bool isInComment = false;
for (int i = 0; i < septarators->size(); i++) {
if (toCheck == septarators->at(i)) {
return typeOfSeparator::separator;
} else if (toCheck == ' ' || toCheck == '\n' || toCheck == '\t') {
} else if(toCheck == '\n' || toCheck == '\t'){
isInComment = false;
return typeOfSeparator::space;
}else if(isInComment){
return typeOfSeparator::isInComment;
}
else if(toCheck == ' ') {
return typeOfSeparator::space;
}else if(commentToken.find(toCheck) != string::npos){
isInComment = true;
return typeOfSeparator::isInComment;
}
}
return typeOfSeparator::none;
Expand Down Expand Up @@ -123,3 +136,7 @@ void configFileReader::openFile() {
isOpen = true;
}
}

void configFileReader::setCommentChar(string commentToken) {
this->commentToken = commentToken;
}
10 changes: 9 additions & 1 deletion src/configFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class configFileReader {
enum typeOfSeparator {
none, // no Separator
space, // any kind of space
separator // any kind of Separator
separator, // any kind of Separator
isInComment
};

/**
Expand Down Expand Up @@ -89,6 +90,8 @@ class configFileReader {
*/
ifstream *configFile;
bool isOpen = false;

string commentToken;
public:

/**
Expand Down Expand Up @@ -121,6 +124,11 @@ class configFileReader {
*/
string readUntilNextSeparator();

/**
* define char as comment start
*/
void setCommentChar(string commentToken);


};

Expand Down
2 changes: 2 additions & 0 deletions tests/configFiles/config8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# a
a
7 changes: 7 additions & 0 deletions tests/configFiles/config9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# a
a
#
#a
# a
a #&% a
%
1 change: 1 addition & 0 deletions tests/configFiles/defaultConf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-b mainFolder
1 change: 1 addition & 0 deletions tests/configFiles/subFolder/defaultConf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-a "subFolder"
66 changes: 65 additions & 1 deletion tests/configReaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

#include <gtest/gtest.h>
#include <argvParser.h>
#include "../src/configFileReader.h"

TEST(configReader, readEmptyText) {
Expand Down Expand Up @@ -232,4 +233,67 @@ TEST(configReader, unfinishedJoin) {
ASSERT_EQ(reader->readUntilNextSeparator(), "abc");
ASSERT_EQ(reader->readUntilNextSeparator(), "def");
ASSERT_EQ(reader->readUntilNextSeparator(), "");
}
}

int xx = 0;
TEST(commentsInConfigFile, comment1){
xx = 0;
argvParser parser(true,"test","");
parser.addArg("a", "-a", "test", [] {xx++;});
parser.addArg("#", "", "x", [] {});
char *arg[] = {"program", "../tests/configFiles/config8.txt"};
parser.analyseArgv(2,arg);
ASSERT_EQ(xx,2);
}
TEST(commentsInConfigFile, comment2){
xx = 0;
argvParser parser(true,"test","#");
parser.addArg("a", "-a", "test", [] {xx++;});
parser.addArg("#", "", "x", [] {});
char *arg[] = {"program", "../tests/configFiles/config8.txt"};
parser.analyseArgv(2,arg);
ASSERT_EQ(xx,1);
}

TEST(commentsInConfigFile, comment3){
xx = 0;
argvParser parser(true,"test","/%#");
parser.addArg("a", "-a", "test", [] {xx++;});
parser.addArg("#", "", "x", [] {});
char *arg[] = {"program", "../tests/configFiles/config8.txt"};
parser.analyseArgv(2,arg);
ASSERT_EQ(xx,1);
}
TEST(defaultConfigFile, comment4){
xx = 0;
argvParser parser(true,"test","/#%");
parser.addArg("a", "-a", "test", [] {xx++;});
parser.addArg("#", "", "x", [] {});
char *arg[] = {"program", "../tests/configFiles/config8.txt"};
parser.analyseArgv(2,arg);
ASSERT_EQ(xx,1);
}
TEST(commentsInConfigFile, comment5){
xx = 0;
argvParser parser(true,"test","#%/");
function<void()> f = []{
xx++;
};
parser.addArg("a", "-a", "test",f );
parser.addArg("#", "", "x", [] {});
char *arg[] = {"program", "../tests/configFiles/config8.txt"};
parser.analyseArgv(2,arg);
ASSERT_EQ(xx,1);
}
TEST(commentsInConfigFile, comment6){
xx = 0;
argvParser parser(true,"test","#%/");
function<void()> f = []{
xx++;
};
parser.addArg("a", "-a", "test",f );
parser.addArg("#", "", "x", [] {});
char *arg[] = {"program", "../tests/configFiles/config9.txt"};
parser.analyseArgv(2,arg);
ASSERT_EQ(xx,2);
}
Loading

0 comments on commit 565e7a8

Please sign in to comment.