diff options
author | Mike Buland <eichlan@xagasoft.com> | 2010-10-27 03:27:05 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2010-10-27 03:27:05 +0000 |
commit | 10b68e6b5e7d12c7af51b960191e1be9eb788d2e (patch) | |
tree | ccc6375726b9acaf53b1bb67bec81747b0bde223 /src/optparser.cpp | |
parent | 68b8b5136677435a84da7d277e78470ac4a41a64 (diff) | |
download | libbu++-10b68e6b5e7d12c7af51b960191e1be9eb788d2e.tar.gz libbu++-10b68e6b5e7d12c7af51b960191e1be9eb788d2e.tar.bz2 libbu++-10b68e6b5e7d12c7af51b960191e1be9eb788d2e.tar.xz libbu++-10b68e6b5e7d12c7af51b960191e1be9eb788d2e.zip |
Interesting tweak to the variant and optparser classes. In the Variant, it
would always fail if a const char * was passed in, it now converts these
silently to Bu::FStrings, good to know...
Also, the OptParser now uses a Variant for overrides, meaning it doesn't have
to do extra parsing, and the amount of code you have to write may be
significantly reduced. Pretty sweet, overall. There is one downside. For the
moment if you use a non-standard type or object as the target of a parameter
it always needs to have a formatter >> operator defined, even if you override
and the formatter >> operator is never called. Hopefully we can get around
this in the future.
Also, it looks like it should be relatively trivial to create conversion
functions for the variant, they'll just be global template functions that
take two parameters, source type and target type. Should be good times.
Diffstat (limited to 'src/optparser.cpp')
-rw-r--r-- | src/optparser.cpp | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/src/optparser.cpp b/src/optparser.cpp index 864d8ce..b81691d 100644 --- a/src/optparser.cpp +++ b/src/optparser.cpp | |||
@@ -45,7 +45,11 @@ void Bu::OptParser::parse( int argc, char **argv ) | |||
45 | { | 45 | { |
46 | sOpt.set( argv[j]+2 ); | 46 | sOpt.set( argv[j]+2 ); |
47 | } | 47 | } |
48 | try | 48 | if( !hlOption.has( sOpt ) ) |
49 | { | ||
50 | optionError( "--" + sOpt ); | ||
51 | } | ||
52 | else | ||
49 | { | 53 | { |
50 | // Long param, cool, that's easy, first search for = | 54 | // Long param, cool, that's easy, first search for = |
51 | Option *pOpt = hlOption.get( sOpt ); | 55 | Option *pOpt = hlOption.get( sOpt ); |
@@ -71,26 +75,28 @@ void Bu::OptParser::parse( int argc, char **argv ) | |||
71 | } | 75 | } |
72 | else if( sExtraParam.isSet() ) | 76 | else if( sExtraParam.isSet() ) |
73 | { | 77 | { |
74 | pOpt->pProxy->setValue( sExtraParam ); | 78 | pOpt->pProxy->setValueFromStr( sExtraParam ); |
75 | } | 79 | } |
76 | else if( argv[j+1] != '\0' ) | 80 | else if( argv[j+1] != '\0' ) |
77 | { | 81 | { |
78 | pOpt->pProxy->setValue( argv[j+1] ); | 82 | pOpt->pProxy->setValueFromStr( argv[j+1] ); |
79 | j++; | 83 | j++; |
80 | } | 84 | } |
81 | } | 85 | } |
82 | } | 86 | } |
83 | catch( Bu::HashException &e ) | ||
84 | { | ||
85 | optionError( "--" + sOpt ); | ||
86 | } | ||
87 | } | 87 | } |
88 | else | 88 | else |
89 | { | 89 | { |
90 | int iCPos; | 90 | int iCPos; |
91 | for( iCPos = 1; argv[j][iCPos] != '\0'; iCPos++ ) | 91 | for( iCPos = 1; argv[j][iCPos] != '\0'; iCPos++ ) |
92 | { | 92 | { |
93 | try | 93 | if( !hsOption.has( argv[j][iCPos] ) ) |
94 | { | ||
95 | Bu::FString sOpt("-"); | ||
96 | sOpt += argv[j][iCPos]; | ||
97 | optionError( sOpt ); | ||
98 | } | ||
99 | else | ||
94 | { | 100 | { |
95 | Option *pOpt = hsOption.get( argv[j][iCPos] ); | 101 | Option *pOpt = hsOption.get( argv[j][iCPos] ); |
96 | char buf[2] = {argv[j][iCPos], '\0'}; | 102 | char buf[2] = {argv[j][iCPos], '\0'}; |
@@ -123,14 +129,14 @@ void Bu::OptParser::parse( int argc, char **argv ) | |||
123 | } | 129 | } |
124 | else if( argv[j][iCPos+1] != '\0' ) | 130 | else if( argv[j][iCPos+1] != '\0' ) |
125 | { | 131 | { |
126 | pOpt->pProxy->setValue( | 132 | pOpt->pProxy->setValueFromStr( |
127 | argv[j]+iCPos+1 | 133 | argv[j]+iCPos+1 |
128 | ); | 134 | ); |
129 | break; | 135 | break; |
130 | } | 136 | } |
131 | else if( argv[j+1] ) | 137 | else if( argv[j+1] ) |
132 | { | 138 | { |
133 | pOpt->pProxy->setValue( | 139 | pOpt->pProxy->setValueFromStr( |
134 | argv[j+1] | 140 | argv[j+1] |
135 | ); | 141 | ); |
136 | j++; | 142 | j++; |
@@ -138,12 +144,6 @@ void Bu::OptParser::parse( int argc, char **argv ) | |||
138 | } | 144 | } |
139 | } | 145 | } |
140 | } | 146 | } |
141 | catch( Bu::HashException &e ) | ||
142 | { | ||
143 | Bu::FString sOpt("-"); | ||
144 | sOpt += argv[j][iCPos]; | ||
145 | optionError( sOpt ); | ||
146 | } | ||
147 | } | 147 | } |
148 | } | 148 | } |
149 | } | 149 | } |
@@ -176,12 +176,12 @@ void Bu::OptParser::addOption( const Option &opt ) | |||
176 | hlOption.insert( opt.sOpt, &lOption.last() ); | 176 | hlOption.insert( opt.sOpt, &lOption.last() ); |
177 | } | 177 | } |
178 | 178 | ||
179 | void Bu::OptParser::setOverride( char cOpt, const Bu::FString &sOverride ) | 179 | void Bu::OptParser::setOverride( char cOpt, const Bu::Variant &sOverride ) |
180 | { | 180 | { |
181 | hsOption.get( cOpt )->sOverride = sOverride; | 181 | hsOption.get( cOpt )->sOverride = sOverride; |
182 | } | 182 | } |
183 | 183 | ||
184 | void Bu::OptParser::setOverride( const Bu::FString &sOpt, const Bu::FString &sOverride ) | 184 | void Bu::OptParser::setOverride( const Bu::FString &sOpt, const Bu::Variant &sOverride ) |
185 | { | 185 | { |
186 | hlOption.get( sOpt )->sOverride = sOverride; | 186 | hlOption.get( sOpt )->sOverride = sOverride; |
187 | } | 187 | } |