diff options
author | Mike Buland <eichlan@xagasoft.com> | 2009-12-18 16:07:31 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2009-12-18 16:07:31 +0000 |
commit | 2cbc634b3b603ec67c6c312bd18177cd52c71b63 (patch) | |
tree | ad743e9cd660f325e1020cdea4f746938d102261 /src/optparser.cpp | |
parent | 038815ae3a019ac56fa1c62e18c5861166d3a975 (diff) | |
download | libbu++-2cbc634b3b603ec67c6c312bd18177cd52c71b63.tar.gz libbu++-2cbc634b3b603ec67c6c312bd18177cd52c71b63.tar.bz2 libbu++-2cbc634b3b603ec67c6c312bd18177cd52c71b63.tar.xz libbu++-2cbc634b3b603ec67c6c312bd18177cd52c71b63.zip |
Ok...sweet, the OptParser now supports everything the old one did, but in much
less code, and it does everything with more style and panache, also fewer bugs.
Diffstat (limited to '')
-rw-r--r-- | src/optparser.cpp | 112 |
1 files changed, 73 insertions, 39 deletions
diff --git a/src/optparser.cpp b/src/optparser.cpp index d656e12..14e7418 100644 --- a/src/optparser.cpp +++ b/src/optparser.cpp | |||
@@ -21,7 +21,6 @@ void Bu::OptParser::parse( int argc, char **argv ) | |||
21 | // Now we're on to something, which kind is it? | 21 | // Now we're on to something, which kind is it? |
22 | if( argv[j][1] == '-' ) | 22 | if( argv[j][1] == '-' ) |
23 | { | 23 | { |
24 | // Long param, cool, that's easy, first search for = | ||
25 | int iEPos; | 24 | int iEPos; |
26 | for( iEPos = 2; argv[j][iEPos] != '\0' && | 25 | for( iEPos = 2; argv[j][iEPos] != '\0' && |
27 | argv[j][iEPos] != '='; iEPos++ ) { } | 26 | argv[j][iEPos] != '='; iEPos++ ) { } |
@@ -39,36 +38,44 @@ void Bu::OptParser::parse( int argc, char **argv ) | |||
39 | { | 38 | { |
40 | sOpt.set( argv[j]+2 ); | 39 | sOpt.set( argv[j]+2 ); |
41 | } | 40 | } |
42 | Option *pOpt = hlOption.get( sOpt ); | 41 | try |
43 | if( pOpt->sUsed ) | ||
44 | { | 42 | { |
45 | Bu::StrArray aParams( iCount ); | 43 | // Long param, cool, that's easy, first search for = |
46 | aParams.append( sOpt ); | 44 | Option *pOpt = hlOption.get( sOpt ); |
47 | if( sExtraParam ) | 45 | if( pOpt->sUsed ) |
48 | { | 46 | { |
49 | aParams.append( argv[j]+iEPos+1 ); | 47 | Bu::StrArray aParams( iCount ); |
48 | aParams.append( sOpt ); | ||
49 | if( sExtraParam ) | ||
50 | { | ||
51 | aParams.append( argv[j]+iEPos+1 ); | ||
52 | } | ||
53 | for( int k = j+1; k < argc; k++ ) | ||
54 | { | ||
55 | aParams.append( argv[k] ); | ||
56 | } | ||
57 | j += pOpt->sUsed( aParams ); | ||
50 | } | 58 | } |
51 | for( int k = j+1; k < argc; k++ ) | 59 | else if( pOpt->pProxy ) |
52 | { | 60 | { |
53 | aParams.append( argv[k] ); | 61 | if( pOpt->sOverride ) |
62 | { | ||
63 | pOpt->pProxy->setValue( pOpt->sOverride ); | ||
64 | } | ||
65 | else if( sExtraParam ) | ||
66 | { | ||
67 | pOpt->pProxy->setValue( sExtraParam ); | ||
68 | } | ||
69 | else if( argv[j+1] != '\0' ) | ||
70 | { | ||
71 | pOpt->pProxy->setValue( argv[j+1] ); | ||
72 | j++; | ||
73 | } | ||
54 | } | 74 | } |
55 | j += pOpt->sUsed( aParams ); | ||
56 | } | 75 | } |
57 | else if( pOpt->pProxy ) | 76 | catch( Bu::HashException &e ) |
58 | { | 77 | { |
59 | if( pOpt->sOverride ) | 78 | optionError( "--" + sOpt ); |
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 | } | ||
72 | } | 79 | } |
73 | } | 80 | } |
74 | else | 81 | else |
@@ -76,22 +83,30 @@ void Bu::OptParser::parse( int argc, char **argv ) | |||
76 | int iCPos; | 83 | int iCPos; |
77 | for( iCPos = 1; argv[j][iCPos] != '\0'; iCPos++ ) | 84 | for( iCPos = 1; argv[j][iCPos] != '\0'; iCPos++ ) |
78 | { | 85 | { |
79 | Option *pOpt = hsOption.get( argv[j][iCPos] ); | 86 | try |
80 | char buf[2] = {argv[j][iCPos], '\0'}; | ||
81 | if( pOpt->bShortHasParams ) | ||
82 | { | 87 | { |
88 | Option *pOpt = hsOption.get( argv[j][iCPos] ); | ||
89 | char buf[2] = {argv[j][iCPos], '\0'}; | ||
83 | if( pOpt->sUsed ) | 90 | if( pOpt->sUsed ) |
84 | { | 91 | { |
85 | Bu::StrArray aParams( argc-j+1 ); | 92 | Bu::StrArray aParams( argc-j+1 ); |
86 | aParams.append( buf ); | 93 | aParams.append( buf ); |
94 | int iMod = 0; | ||
87 | if( argv[j][iCPos+1] != '\0' ) | 95 | if( argv[j][iCPos+1] != '\0' ) |
96 | { | ||
88 | aParams.append( argv[j]+iCPos+1 ); | 97 | aParams.append( argv[j]+iCPos+1 ); |
98 | iMod = -1; | ||
99 | } | ||
89 | for( int k = j+1; k < argc; k++ ) | 100 | for( int k = j+1; k < argc; k++ ) |
90 | { | 101 | { |
91 | aParams.append( argv[k] ); | 102 | aParams.append( argv[k] ); |
92 | } | 103 | } |
93 | j += pOpt->sUsed( aParams ); | 104 | int iUsed = pOpt->sUsed( aParams ); |
94 | break; | 105 | if( iUsed > 0 ) |
106 | { | ||
107 | j += iUsed + iMod; | ||
108 | break; | ||
109 | } | ||
95 | } | 110 | } |
96 | else if( pOpt->pProxy ) | 111 | else if( pOpt->pProxy ) |
97 | { | 112 | { |
@@ -116,20 +131,31 @@ void Bu::OptParser::parse( int argc, char **argv ) | |||
116 | } | 131 | } |
117 | } | 132 | } |
118 | } | 133 | } |
119 | else | 134 | catch( Bu::HashException &e ) |
120 | { | 135 | { |
121 | if( pOpt->sUsed ) | 136 | Bu::FString sOpt("-"); |
122 | { | 137 | sOpt += argv[j][iCPos]; |
123 | Bu::StrArray aParam( 1 ); | 138 | optionError( sOpt ); |
124 | aParam.append( buf ); | ||
125 | pOpt->sUsed( aParam ); | ||
126 | } | ||
127 | } | 139 | } |
128 | } | 140 | } |
129 | } | 141 | } |
130 | } | 142 | } |
131 | else | 143 | else |
132 | { | 144 | { |
145 | if( !sNonOption ) | ||
146 | { | ||
147 | optionError( argv[j] ); | ||
148 | } | ||
149 | else | ||
150 | { | ||
151 | int iCount = argc-j; | ||
152 | Bu::StrArray aParams( iCount ); | ||
153 | for( int k = j; k < argc; k++ ) | ||
154 | { | ||
155 | aParams.append( argv[k] ); | ||
156 | } | ||
157 | j += sNonOption( aParams ); | ||
158 | } | ||
133 | } | 159 | } |
134 | } | 160 | } |
135 | } | 161 | } |
@@ -160,7 +186,6 @@ void Bu::OptParser::addHelpOption( char c, const Bu::FString &s, const Bu::FStri | |||
160 | o.cOpt = c; | 186 | o.cOpt = c; |
161 | o.sOpt = s; | 187 | o.sOpt = s; |
162 | o.sHelp = sHelp; | 188 | o.sHelp = sHelp; |
163 | o.bShortHasParams = false; | ||
164 | addOption( o ); | 189 | addOption( o ); |
165 | } | 190 | } |
166 | 191 | ||
@@ -251,6 +276,17 @@ int Bu::OptParser::optHelp( StrArray /*aParams*/ ) | |||
251 | return 0; | 276 | return 0; |
252 | } | 277 | } |
253 | 278 | ||
279 | void Bu::OptParser::optionError( const Bu::FString sOption ) | ||
280 | { | ||
281 | sio << "Unregcognized option discovered: " << sOption << sio.nl << sio.nl; | ||
282 | exit( 1 ); | ||
283 | } | ||
284 | |||
285 | void Bu::OptParser::setNonOption( OptionSignal sSignal ) | ||
286 | { | ||
287 | sNonOption = sSignal; | ||
288 | } | ||
289 | |||
254 | Bu::FString Bu::OptParser::format( const Bu::FString &sIn, int iWidth, | 290 | Bu::FString Bu::OptParser::format( const Bu::FString &sIn, int iWidth, |
255 | int iIndent ) | 291 | int iIndent ) |
256 | { | 292 | { |
@@ -358,7 +394,6 @@ Bu::OptParser::_ValueProxy::~_ValueProxy() | |||
358 | 394 | ||
359 | Bu::OptParser::Option::Option() : | 395 | Bu::OptParser::Option::Option() : |
360 | cOpt( '\0' ), | 396 | cOpt( '\0' ), |
361 | bShortHasParams( false ), | ||
362 | pProxy( NULL ) | 397 | pProxy( NULL ) |
363 | { | 398 | { |
364 | } | 399 | } |
@@ -368,7 +403,6 @@ Bu::OptParser::Option::Option( const Option &rSrc ) : | |||
368 | sOpt( rSrc.sOpt ), | 403 | sOpt( rSrc.sOpt ), |
369 | sHelp( rSrc.sHelp ), | 404 | sHelp( rSrc.sHelp ), |
370 | sUsed( rSrc.sUsed ), | 405 | sUsed( rSrc.sUsed ), |
371 | bShortHasParams( rSrc.bShortHasParams ), | ||
372 | pProxy( NULL ), | 406 | pProxy( NULL ), |
373 | sOverride( rSrc.sOverride ) | 407 | sOverride( rSrc.sOverride ) |
374 | { | 408 | { |