CXX uses colorgcc, ArgParse::findKeyinArgMap bugfix, new test/test_ArgParse.hpp

master
Denes Matetelki 13 years ago
parent a7db838068
commit 4efe0d3388

3
.gitignore vendored

@ -11,6 +11,7 @@ test.out
*/lcov2.info
*/core
*.gcno
*.gcda
*.kdev_include_paths
test/generated_main.cpp
test/test
@ -18,3 +19,5 @@ leak.log
leak.log.core.*
gdb.out
html/*
test/testCppUtils
test/testCppUtils.out

@ -1,6 +1,8 @@
cmake_minimum_required (VERSION 2.6)
project (CPP_UTILS_LIB)
set(CMAKE_CXX_COMPILER "/usr/lib/colorgcc/bin/g++")
set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow "
"-ggdb -fprofile-arcs -ftest-coverage")
add_definitions( ${CXX_FLAGS} )
@ -9,4 +11,4 @@ include_directories (../include)
aux_source_directory(../src CPP_UTILS_LIB_SOURCES)
add_library (CppUtils SHARED ${CPP_UTILS_LIB_SOURCES})
target_link_libraries(CppUtils pthread rt gcov)
target_link_libraries(CppUtils pthread rt gcov mysqlclient)

@ -112,5 +112,4 @@ inline void StrToT( T &t, const std::string s )
ss >> t;
}
#endif // COMMON_HPP

@ -0,0 +1,129 @@
// g++ mysqlclient_main.cpp src/Logger.cpp src/MysqlClient.cpp src/ArgParse.cpp -I./include -lmysqlclient
#include "Logger.hpp"
#include "Common.hpp"
#include "ArgParse.hpp"
#include "MysqlClient.hpp"
#include <mysql/errmsg.h>
#include <string>
#include <list>
#include <iostream>
#include <stdexcept>
void setUpArgs(ArgParse &argParse)
{
argParse.addArgument("--host",
"Hostname/IP",
ArgParse::STRING );
argParse.addArgument("-u, --user",
"Username",
ArgParse::STRING );
argParse.addArgument("-db, --database",
"Database",
ArgParse::STRING );
argParse.addArgument("-p, --password",
"Password",
ArgParse::STRING );
argParse.addArgument("-port",
"Port",
ArgParse::INT );
argParse.addArgument("-s, --unix-socket",
"Unix socket",
ArgParse::STRING );
argParse.addArgument("-f, --client-flags",
"Client flags",
ArgParse::INT );
}
bool getArgs( int argc, char* argv[],
ArgParse &argParse,
std::string &host,
std::string &user,
std::string &db,
std::string &pass,
std::string &unixsocket,
int &port,
int &clientflags )
{
try {
argParse.parseArgs(argc, argv);
} catch (std::runtime_error e) {
std::cerr << e.what() << std::endl << std::endl;
return false;
}
argParse.argAsString("--host", host);
argParse.argAsString("-u, --user", user);
argParse.argAsString("-db, --database", db);
argParse.argAsString("-p, --password", pass);
argParse.argAsInt("-port", port);
argParse.argAsString("-s, --unix-socket", unixsocket);
argParse.argAsInt("-f, --client-flags", clientflags);
return true;
}
void printResults(std::list<std::string> &results)
{
LOG ( Logger::DEBUG, std::string("Got query result number of rows: ").
append(TToStr(results.size())).c_str() );
for (std::list<std::string>::const_iterator it = results.begin();
it != results.end();
++it ) {
LOG ( Logger::DEBUG, (*it).c_str() );
}
}
int main(int argc, char* argv[] )
{
Logger::createInstance();
Logger::init(std::cout);
Logger::setLogLevel(Logger::FINEST);
ArgParse argParse("Simple MySQL client",
"Report bugs to: denes.matetelki@gmail.com");
setUpArgs(argParse);
std::string host, user, db, pass, unixsocket;
int port, clientflags;
if ( !getArgs(argc, argv,
argParse,
host, user, db, pass, unixsocket,
port, clientflags ) ||
argParse.foundArg("-h, --help") )
{
std::cout << argParse.usage() << std::endl;
return 1;
}
init_client_errs();
MysqlClient mysqlClient (
argParse.foundArg("--host") ? host.c_str() : NULL,
argParse.foundArg("-u, --user") ? user.c_str() : NULL,
argParse.foundArg("-p, --password") ? pass.c_str() : NULL,
argParse.foundArg("-db, --database") ? db .c_str() : NULL,
argParse.foundArg("-port") ? port : 0,
argParse.foundArg("-s, --unix-socket") ? unixsocket.c_str() : NULL,
argParse.foundArg("-f, --client-flags") ? clientflags : 0 );
std::list<std::string> results;
if ( !mysqlClient.querty("SELECT * FROM seats", results) ) {
LOG ( Logger::ERR, "Could not execute query." );
} else {
printResults(results);
}
finish_client_errs();
Logger::destroy();
return 0;
}

@ -1,5 +1,6 @@
#include "ArgParse.hpp"
#include "Common.hpp"
#include <iostream>
#include <sstream>
@ -381,17 +382,23 @@ ArgParse::argCompare::operator()(const std::string a,const std::string b) const
std::map<std::string, ArgParse::Argument>::iterator
ArgParse::findKeyinArgMap(const std::string param)
{
ArgMap::iterator it;
for( it = m_params.begin(); it != m_params.end(); ++it) {
for( ArgMap::iterator it = m_params.begin(); it != m_params.end(); ++it) {
// if it's the short param at the beginning
if ( (*it).first.find(param) == 0 )
return it;
std::string first;
std::string second;
// or is it the long after the comma?
size_t commaPos = (*it).first.find(",");
if ( commaPos != std::string::npos &&
(*it).first.find( param, commaPos+1 ) != std::string::npos )
if ( commaPos == std::string::npos ) {
first = (*it).first;
} else {
first = (*it).first.substr(0, commaPos);
second = (*it).first.substr(commaPos+1);
}
first.erase(std::remove_if(first.begin(), first.end(), isspace), first.end());
second.erase(std::remove_if(second.begin(), second.end(), isspace), second.end());
if ( param == first || param == second )
return it;
}

@ -1,3 +1,6 @@
set(CMAKE_CXX_COMPILER "/usr/lib/colorgcc/bin/g++")
set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow "
"-ggdb -fprofile-arcs -ftest-coverage --std=c++0x " )
@ -15,10 +18,11 @@ if(CXXTEST_FOUND)
generated_main.cpp
Fixture.hpp
test_Singelton_call_once.hpp
# test_Singleton.hpp
# test_Singleton_meyers.hpp
# test_Singleton_DCLP.hpp
test_ArgParse.hpp
# test_Singelton_call_once.hpp
# test_Singleton.hpp
# test_Singleton_meyers.hpp
# test_Singleton_DCLP.hpp
# test_Mutex.hpp
# test_ScopedLock.hpp
# test_ConditionalVariable.hpp

@ -23,14 +23,14 @@ post="\E[00;00m"
ulimit -c unlimited
# test=$1
test=./test
# if [ $# -ne 1 ]
# then
# echo "Usage: `basename $0` <TEST_BINARY>"
# exit 1
# fi
if [ $# -ne 1 ]
then
echo "Usage: `basename $0` <TEST_BINARY>"
exit 1
fi
test=$1
# test=./test
if [ ! -e $test ]; then
echo -e "The parameter binary: $test does not exists"

@ -0,0 +1,73 @@
#include <cxxtest/TestSuite.h>
#include "Common.hpp"
#include "Fixture.hpp"
#define private public
#include "ArgParse.hpp"
#include <string>
class TestArgParseSuite : public CxxTest::TestSuite
{
public:
void testBasic( void )
{
TEST_HEADER;
ArgParse argParse("intro", "outro");
TS_ASSERT_EQUALS( argParse.usage(), std::string(
"intro\n\n"
"usage: [OPTION]\n\n"
"Options:\n"
"-h, --help Prints this help message\n\n"
"outro\n") );
}
void testfindKeyinArgMap ( void )
{
TEST_HEADER;
ArgParse argParse("intro", "outro");
argParse.addArgument("-s, --unix-rocket",
"Unix rocket",
ArgParse::STRING );
TS_ASSERT_DIFFERS ( argParse.findKeyinArgMap("-s"), argParse.m_params.end() );
TS_ASSERT_DIFFERS ( argParse.findKeyinArgMap("--unix-rocket"), argParse.m_params.end() );
TS_ASSERT_EQUALS( argParse.findKeyinArgMap("-u"), argParse.m_params.end() );
TS_ASSERT_EQUALS( argParse.findKeyinArgMap("-r"), argParse.m_params.end() );
}
void asdtestAddArgs( void )
{
TEST_HEADER;
ArgParse argParse("intro", "outro");
argParse.addArgument("-s, --unix-socket",
"Unix socket",
ArgParse::STRING );
argParse.addArgument("-u, --user",
"Username",
ArgParse::STRING );
int argc = 3;
char *argv[] = { (char*)"test",
(char*)"-u",
(char*)"username" };
argParse.parseArgs(argc, argv);
TS_ASSERT_EQUALS( argParse.isArg("-s, --unix-socket"), false );
TS_ASSERT_EQUALS( argParse.isArg("-u, --user"), true );
}
};
Loading…
Cancel
Save