summaryrefslogtreecommitdiff
path: root/src/optparser.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-12-18 08:59:16 +0000
committerMike Buland <eichlan@xagasoft.com>2009-12-18 08:59:16 +0000
commit146930268a695dcc0432599d625ec3eb7e74025e (patch)
treea944d13981ed055b337757953014ed1e2d45e19c /src/optparser.cpp
parent0d3d73fb0cacd3d1cf7eb8b83ba87f8b740b871a (diff)
downloadlibbu++-146930268a695dcc0432599d625ec3eb7e74025e.tar.gz
libbu++-146930268a695dcc0432599d625ec3eb7e74025e.tar.bz2
libbu++-146930268a695dcc0432599d625ec3eb7e74025e.tar.xz
libbu++-146930268a695dcc0432599d625ec3eb7e74025e.zip
The OptParser still needs help banners and more helper functions, but otherwise,
it's done. It works great, and provides much flexibility and usefulness. It now relies on the input side of the Formatter class, which at the moment supports reading strings...not real useful yet... Next up, adding readers for numbers and such, then it'll be mostly complete. Also, fixed a bug when copying uninitialized signal objects.
Diffstat (limited to 'src/optparser.cpp')
-rw-r--r--src/optparser.cpp105
1 files changed, 86 insertions, 19 deletions
diff --git a/src/optparser.cpp b/src/optparser.cpp
index f99dd85..2a8e64b 100644
--- a/src/optparser.cpp
+++ b/src/optparser.cpp
@@ -28,28 +28,48 @@ void Bu::OptParser::parse( int argc, char **argv )
28 28
29 Bu::FString sOpt; 29 Bu::FString sOpt;
30 int iCount = argc-j; 30 int iCount = argc-j;
31 Bu::FString sExtraParam;
31 if( argv[j][iEPos] == '=' ) 32 if( argv[j][iEPos] == '=' )
32 { 33 {
33 sOpt.set( argv[j]+2, iEPos-2 ); 34 sOpt.set( argv[j]+2, iEPos-2 );
34 iCount++; 35 iCount++;
36 sExtraParam.set( argv[j]+iEPos+1 );
35 } 37 }
36 else 38 else
37 { 39 {
38 sOpt.set( argv[j]+2 ); 40 sOpt.set( argv[j]+2 );
39 } 41 }
40 Option *pOpt = hlOption.get( sOpt ); 42 Option *pOpt = hlOption.get( sOpt );
41 Bu::StrArray aParams( iCount ); 43 if( pOpt->sUsed )
42 aParams.append( sOpt );
43 if( argv[j][iEPos] == '=' )
44 { 44 {
45 aParams.append( argv[j]+iEPos+1 ); 45 Bu::StrArray aParams( iCount );
46 aParams.append( sOpt );
47 if( sExtraParam )
48 {
49 aParams.append( argv[j]+iEPos+1 );
50 }
51 for( int k = j+1; k < argc; k++ )
52 {
53 aParams.append( argv[k] );
54 }
55 j += pOpt->sUsed( aParams );
46 } 56 }
47 for( int k = j+1; k < argc; k++ ) 57 else if( pOpt->pProxy )
48 { 58 {
49 aParams.append( argv[k] ); 59 if( pOpt->sOverride )
60 {
61 pOpt->pProxy->setValue( pOpt->sOverride );
62 }
63 else if( sExtraParam )
64 {
65 pOpt->pProxy->setValue( sExtraParam );
66 }
67 else if( argv[j+1] != '\0' )
68 {
69 pOpt->pProxy->setValue( argv[j+1] );
70 j++;
71 }
50 } 72 }
51 if( pOpt->sUsed )
52 j += pOpt->sUsed( aParams );
53 } 73 }
54 else 74 else
55 { 75 {
@@ -57,26 +77,45 @@ void Bu::OptParser::parse( int argc, char **argv )
57 for( iCPos = 1; argv[j][iCPos] != '\0'; iCPos++ ) 77 for( iCPos = 1; argv[j][iCPos] != '\0'; iCPos++ )
58 { 78 {
59 Option *pOpt = hsOption.get( argv[j][iCPos] ); 79 Option *pOpt = hsOption.get( argv[j][iCPos] );
60 Bu::StrArray aParams( argc-j+1 );
61 char buf[2] = {argv[j][iCPos], '\0'}; 80 char buf[2] = {argv[j][iCPos], '\0'};
62 aParams.append( buf );
63 if( pOpt->bShortHasParams ) 81 if( pOpt->bShortHasParams )
64 { 82 {
65 if( argv[j][iCPos+1] != '\0' )
66 aParams.append( argv[j]+iCPos+1 );
67 for( int k = j+1; k < argc; k++ )
68 {
69 aParams.append( argv[k] );
70 }
71 if( pOpt->sUsed ) 83 if( pOpt->sUsed )
72 { 84 {
85 Bu::StrArray aParams( argc-j+1 );
86 aParams.append( buf );
87 if( argv[j][iCPos+1] != '\0' )
88 aParams.append( argv[j]+iCPos+1 );
89 for( int k = j+1; k < argc; k++ )
90 {
91 aParams.append( argv[k] );
92 }
73 j += pOpt->sUsed( aParams ); 93 j += pOpt->sUsed( aParams );
94 break;
95 }
96 else if( pOpt->pProxy )
97 {
98 if( pOpt->sOverride )
99 {
100 pOpt->pProxy->setValue( pOpt->sOverride );
101 }
102 else if( argv[j][iCPos+1] != '\0' )
103 {
104 pOpt->pProxy->setValue(
105 argv[j]+iCPos+1
106 );
107 break;
108 }
74 } 109 }
75 break;
76 } 110 }
77 else 111 else
78 { 112 {
79 pOpt->sUsed( aParams ); 113 if( pOpt->sUsed )
114 {
115 Bu::StrArray aParam( 1 );
116 aParam.append( buf );
117 pOpt->sUsed( aParam );
118 }
80 } 119 }
81 } 120 }
82 } 121 }
@@ -247,16 +286,44 @@ Bu::FString Bu::OptParser::format( const Bu::FString &sIn, int iWidth,
247 286
248 287
249// 288//
289// Code for Bu::OptParser::_ValueProxy
290//
291
292Bu::OptParser::_ValueProxy::_ValueProxy()
293{
294}
295
296Bu::OptParser::_ValueProxy::~_ValueProxy()
297{
298}
299
300//
250// Code for Bu::OptParser::Option 301// Code for Bu::OptParser::Option
251// 302//
252 303
253Bu::OptParser::Option::Option() : 304Bu::OptParser::Option::Option() :
254 cOpt( '\0' ), 305 cOpt( '\0' ),
255 bShortHasParams( false ) 306 bShortHasParams( false ),
307 pProxy( NULL )
308{
309}
310
311Bu::OptParser::Option::Option( const Option &rSrc ) :
312 cOpt( rSrc.cOpt ),
313 sOpt( rSrc.sOpt ),
314 sHelp( rSrc.sHelp ),
315 sUsed( rSrc.sUsed ),
316 bShortHasParams( rSrc.bShortHasParams ),
317 pProxy( NULL ),
318 sOverride( rSrc.sOverride )
256{ 319{
320 if( rSrc.pProxy )
321 pProxy = rSrc.pProxy->clone();
257} 322}
258 323
259Bu::OptParser::Option::~Option() 324Bu::OptParser::Option::~Option()
260{ 325{
326 delete pProxy;
327 pProxy = NULL;
261} 328}
262 329