summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/paramproc.cpp82
-rw-r--r--src/paramproc.h9
-rw-r--r--src/test/param.cpp22
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,
141 as->sValue = lpValue; 141 as->sValue = lpValue;
142 142
143 lArg.push_back( as ); 143 lArg.push_back( as );
144
145 if( !lBan.empty() )
146 {
147 if( lBan.back()->pBefore == NULL )
148 lBan.back()->pBefore = as;
149 }
144} 150}
145 151
146void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, 152void ParamProc::addParam( const char *lpWord, char cChar, Proc proc,
@@ -203,7 +209,7 @@ void ParamProc::process( int argc, char *argv[] )
203{ 209{
204 for( int arg = 1; arg < argc; arg++ ) 210 for( int arg = 1; arg < argc; arg++ )
205 { 211 {
206 printf(":::%d:::%s\n", arg, argv[arg] ); 212 //printf(":::%d:::%s\n", arg, argv[arg] );
207 if( argv[arg][0] == '-' ) 213 if( argv[arg][0] == '-' )
208 { 214 {
209 if( argv[arg][1] == '-' ) 215 if( argv[arg][1] == '-' )
@@ -270,6 +276,10 @@ void ParamProc::process( int argc, char *argv[] )
270 } 276 }
271 continue; 277 continue;
272 } 278 }
279 else
280 {
281 unknownParam( argc-arg, argv+arg );
282 }
273 } 283 }
274 else 284 else
275 { 285 {
@@ -347,15 +357,23 @@ void ParamProc::process( int argc, char *argv[] )
347 } 357 }
348 } 358 }
349 } 359 }
360 else
361 {
362 unknownParam( argc-arg, argv+arg );
363 }
350 } 364 }
351 } 365 }
352 } 366 }
367 else
368 {
369 cmdParam( argc-arg, argv+arg );
370 }
353 } 371 }
354} 372}
355 373
356ParamProc::ArgSpec *ParamProc::checkWord( const char *arg ) 374ParamProc::ArgSpec *ParamProc::checkWord( const char *arg )
357{ 375{
358 printf("Checking \"%s\"...\n", arg ); 376 //printf("Checking \"%s\"...\n", arg );
359 std::list<ArgSpec *>::const_iterator i; 377 std::list<ArgSpec *>::const_iterator i;
360 for( i = lArg.begin(); i != lArg.end(); i++ ) 378 for( i = lArg.begin(); i != lArg.end(); i++ )
361 { 379 {
@@ -380,7 +398,7 @@ ParamProc::ArgSpec *ParamProc::checkWord( const char *arg )
380 398
381ParamProc::ArgSpec *ParamProc::checkLetr( const char arg ) 399ParamProc::ArgSpec *ParamProc::checkLetr( const char arg )
382{ 400{
383 printf("Checking \'%c\'...\n", arg ); 401 //printf("Checking \'%c\'...\n", arg );
384 std::list<ArgSpec *>::const_iterator i; 402 std::list<ArgSpec *>::const_iterator i;
385 for( i = lArg.begin(); i != lArg.end(); i++ ) 403 for( i = lArg.begin(); i != lArg.end(); i++ )
386 { 404 {
@@ -408,6 +426,62 @@ int ParamProc::unknownParam( int argc, char *argv[] )
408 426
409int ParamProc::help( int argc, char *argv[] ) 427int ParamProc::help( int argc, char *argv[] )
410{ 428{
411 // Insert help here later on 429 std::list<Banner *>::const_iterator b = lBan.begin();
430 std::list<ArgSpec *>::const_iterator i;
431 int len=0;
432 for( i = lArg.begin(); i != lArg.end(); i++ )
433 {
434 if( len < (*i)->sWord.getLength() )
435 len = (*i)->sWord.getLength();
436 }
437 char fmt[10];
438 sprintf( fmt, "%%-%ds ", len );
439
440 for( i = lArg.begin(); i != lArg.end(); i++ )
441 {
442 if( b != lBan.end() )
443 {
444 if( (*b)->pBefore == (*i) )
445 {
446 printf( (*b)->sBanner.getString() );
447 b++;
448 }
449 }
450 printf(" ");
451 if( (*i)->cChar )
452 {
453 printf("-%c, ", (*i)->cChar );
454 }
455 else
456 {
457 printf(" ");
458 }
459 if( (*i)->sWord.getString() )
460 {
461 printf("--");
462 printf( fmt, (*i)->sWord.getString() );
463 }
464 else
465 {
466 printf(" ");
467 printf(fmt, "" );
468 }
469 printf("%s\n", (*i)->sDesc.getString() );
470 }
471 if( b != lBan.end() )
472 {
473 if( (*b)->pBefore == NULL )
474 {
475 printf( (*b)->sBanner.getString() );
476 }
477 }
478}
479
480void ParamProc::addHelpBanner( const char *sHelpBanner )
481{
482 Banner *pBan = new Banner;
483 pBan->sBanner = sHelpBanner;
484 pBan->pBefore = NULL;
485 lBan.push_back( pBan );
412} 486}
413 487
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:
127 ); 127 );
128 128
129 void process( int argc, char *argv[] ); 129 void process( int argc, char *argv[] );
130 void addHelpBanner( const char *sHelpBanner );
131
132private:
130 ArgSpec *checkWord( const char *arg ); 133 ArgSpec *checkWord( const char *arg );
131 ArgSpec *checkLetr( const char arg ); 134 ArgSpec *checkLetr( const char arg );
132 135
@@ -136,6 +139,12 @@ public:
136 virtual int help( int argc, char *argv[] ); 139 virtual int help( int argc, char *argv[] );
137 140
138private: 141private:
142 typedef struct Banner
143 {
144 StaticString sBanner;
145 ArgSpec *pBefore;
146 } Banner;
147 std::list<Banner *> lBan;
139 std::list<ArgSpec *> lArg; 148 std::list<ArgSpec *> lArg;
140}; 149};
141 150
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 @@
3 3
4Param::Param() 4Param::Param()
5{ 5{
6 addParam( "name", 's', mkproc( Param::printStuff ), &str ); 6 addHelpBanner("param - A test of the libbu++ parameter systems\n"
7 "Enjoy with care and caution\n\nTest stuff:\n");
8 addParam( "name", 's', mkproc( Param::printStuff ), &str, "Test a param param" );
7 //addParam( "name", &str ); 9 //addParam( "name", &str );
8 addParam( "job", 'U', mkproc( Param::printStuff ) ); 10 addParam( "job", 'U', mkproc( Param::printStuff ), "Test a paramless param" );
9 11
10 // --name=Bobo 12 addHelpBanner("\nInformational:\n");
11 // --job hello 13 addParam( "help", mkproc( ParamProc::help ), "Help!" );
14
15 addHelpBanner("\nThanks for trying my test!\n\n");
12} 16}
13 17
14Param::~Param() 18Param::~Param()
@@ -30,11 +34,13 @@ int Param::printStuff( int argc, char *argv[] )
30 34
31int main( int argc, char *argv[] ) 35int main( int argc, char *argv[] )
32{ 36{
33 printf("Starting...\n"); 37 if( argc == 1 )
38 {
39 printf("You have to enter some parameter, try '--help'\n\n");
40 return 0;
41 }
42
34 Param p; 43 Param p;
35 p.process( argc, argv ); 44 p.process( argc, argv );
36
37 //printf("Calling by hand...\n");
38 //p.printStuff();
39} 45}
40 46