From dcd914665f6e8ea6ea4e648f7357f1c842273d87 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 29 Jun 2006 05:34:39 +0000 Subject: All done with all of the basic and some nice, extended functionality, now I just need to update our other projects... --- src/paramproc.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++--- src/paramproc.h | 9 ++++++ src/test/param.cpp | 22 +++++++++------ 3 files changed, 101 insertions(+), 12 deletions(-) diff --git a/src/paramproc.cpp b/src/paramproc.cpp index eaaf466..85274e2 100644 --- a/src/paramproc.cpp +++ b/src/paramproc.cpp @@ -141,6 +141,12 @@ void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, as->sValue = lpValue; lArg.push_back( as ); + + if( !lBan.empty() ) + { + if( lBan.back()->pBefore == NULL ) + lBan.back()->pBefore = as; + } } void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, @@ -203,7 +209,7 @@ void ParamProc::process( int argc, char *argv[] ) { for( int arg = 1; arg < argc; arg++ ) { - printf(":::%d:::%s\n", arg, argv[arg] ); + //printf(":::%d:::%s\n", arg, argv[arg] ); if( argv[arg][0] == '-' ) { if( argv[arg][1] == '-' ) @@ -270,6 +276,10 @@ void ParamProc::process( int argc, char *argv[] ) } continue; } + else + { + unknownParam( argc-arg, argv+arg ); + } } else { @@ -347,15 +357,23 @@ void ParamProc::process( int argc, char *argv[] ) } } } + else + { + unknownParam( argc-arg, argv+arg ); + } } } } + else + { + cmdParam( argc-arg, argv+arg ); + } } } ParamProc::ArgSpec *ParamProc::checkWord( const char *arg ) { - printf("Checking \"%s\"...\n", arg ); + //printf("Checking \"%s\"...\n", arg ); std::list::const_iterator i; for( i = lArg.begin(); i != lArg.end(); i++ ) { @@ -380,7 +398,7 @@ ParamProc::ArgSpec *ParamProc::checkWord( const char *arg ) ParamProc::ArgSpec *ParamProc::checkLetr( const char arg ) { - printf("Checking \'%c\'...\n", arg ); + //printf("Checking \'%c\'...\n", arg ); std::list::const_iterator i; for( i = lArg.begin(); i != lArg.end(); i++ ) { @@ -408,6 +426,62 @@ int ParamProc::unknownParam( int argc, char *argv[] ) int ParamProc::help( int argc, char *argv[] ) { - // Insert help here later on + std::list::const_iterator b = lBan.begin(); + std::list::const_iterator i; + int len=0; + for( i = lArg.begin(); i != lArg.end(); i++ ) + { + if( len < (*i)->sWord.getLength() ) + len = (*i)->sWord.getLength(); + } + char fmt[10]; + sprintf( fmt, "%%-%ds ", len ); + + for( i = lArg.begin(); i != lArg.end(); i++ ) + { + if( b != lBan.end() ) + { + if( (*b)->pBefore == (*i) ) + { + printf( (*b)->sBanner.getString() ); + b++; + } + } + printf(" "); + if( (*i)->cChar ) + { + printf("-%c, ", (*i)->cChar ); + } + else + { + printf(" "); + } + if( (*i)->sWord.getString() ) + { + printf("--"); + printf( fmt, (*i)->sWord.getString() ); + } + else + { + printf(" "); + printf(fmt, "" ); + } + printf("%s\n", (*i)->sDesc.getString() ); + } + if( b != lBan.end() ) + { + if( (*b)->pBefore == NULL ) + { + printf( (*b)->sBanner.getString() ); + } + } +} + +void ParamProc::addHelpBanner( const char *sHelpBanner ) +{ + Banner *pBan = new Banner; + pBan->sBanner = sHelpBanner; + pBan->pBefore = NULL; + lBan.push_back( pBan ); } diff --git a/src/paramproc.h b/src/paramproc.h index d462eac..d857193 100644 --- a/src/paramproc.h +++ b/src/paramproc.h @@ -127,6 +127,9 @@ public: ); void process( int argc, char *argv[] ); + void addHelpBanner( const char *sHelpBanner ); + +private: ArgSpec *checkWord( const char *arg ); ArgSpec *checkLetr( const char arg ); @@ -136,6 +139,12 @@ public: virtual int help( int argc, char *argv[] ); private: + typedef struct Banner + { + StaticString sBanner; + ArgSpec *pBefore; + } Banner; + std::list lBan; std::list lArg; }; diff --git a/src/test/param.cpp b/src/test/param.cpp index 0641f90..a4d2824 100644 --- a/src/test/param.cpp +++ b/src/test/param.cpp @@ -3,12 +3,16 @@ Param::Param() { - addParam( "name", 's', mkproc( Param::printStuff ), &str ); + addHelpBanner("param - A test of the libbu++ parameter systems\n" + "Enjoy with care and caution\n\nTest stuff:\n"); + addParam( "name", 's', mkproc( Param::printStuff ), &str, "Test a param param" ); //addParam( "name", &str ); - addParam( "job", 'U', mkproc( Param::printStuff ) ); + addParam( "job", 'U', mkproc( Param::printStuff ), "Test a paramless param" ); - // --name=Bobo - // --job hello + addHelpBanner("\nInformational:\n"); + addParam( "help", mkproc( ParamProc::help ), "Help!" ); + + addHelpBanner("\nThanks for trying my test!\n\n"); } Param::~Param() @@ -30,11 +34,13 @@ int Param::printStuff( int argc, char *argv[] ) int main( int argc, char *argv[] ) { - printf("Starting...\n"); + if( argc == 1 ) + { + printf("You have to enter some parameter, try '--help'\n\n"); + return 0; + } + Param p; p.process( argc, argv ); - - //printf("Calling by hand...\n"); - //p.printStuff(); } -- cgit v1.2.3