From 10b68e6b5e7d12c7af51b960191e1be9eb788d2e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 27 Oct 2010 03:27:05 +0000 Subject: 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. --- src/optparser.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/optparser.cpp') 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 ) { sOpt.set( argv[j]+2 ); } - try + if( !hlOption.has( sOpt ) ) + { + optionError( "--" + sOpt ); + } + else { // Long param, cool, that's easy, first search for = Option *pOpt = hlOption.get( sOpt ); @@ -71,26 +75,28 @@ void Bu::OptParser::parse( int argc, char **argv ) } else if( sExtraParam.isSet() ) { - pOpt->pProxy->setValue( sExtraParam ); + pOpt->pProxy->setValueFromStr( sExtraParam ); } else if( argv[j+1] != '\0' ) { - pOpt->pProxy->setValue( argv[j+1] ); + pOpt->pProxy->setValueFromStr( argv[j+1] ); j++; } } } - catch( Bu::HashException &e ) - { - optionError( "--" + sOpt ); - } } else { int iCPos; for( iCPos = 1; argv[j][iCPos] != '\0'; iCPos++ ) { - try + if( !hsOption.has( argv[j][iCPos] ) ) + { + Bu::FString sOpt("-"); + sOpt += argv[j][iCPos]; + optionError( sOpt ); + } + else { Option *pOpt = hsOption.get( argv[j][iCPos] ); char buf[2] = {argv[j][iCPos], '\0'}; @@ -123,14 +129,14 @@ void Bu::OptParser::parse( int argc, char **argv ) } else if( argv[j][iCPos+1] != '\0' ) { - pOpt->pProxy->setValue( + pOpt->pProxy->setValueFromStr( argv[j]+iCPos+1 ); break; } else if( argv[j+1] ) { - pOpt->pProxy->setValue( + pOpt->pProxy->setValueFromStr( argv[j+1] ); j++; @@ -138,12 +144,6 @@ void Bu::OptParser::parse( int argc, char **argv ) } } } - catch( Bu::HashException &e ) - { - Bu::FString sOpt("-"); - sOpt += argv[j][iCPos]; - optionError( sOpt ); - } } } } @@ -176,12 +176,12 @@ void Bu::OptParser::addOption( const Option &opt ) hlOption.insert( opt.sOpt, &lOption.last() ); } -void Bu::OptParser::setOverride( char cOpt, const Bu::FString &sOverride ) +void Bu::OptParser::setOverride( char cOpt, const Bu::Variant &sOverride ) { hsOption.get( cOpt )->sOverride = sOverride; } -void Bu::OptParser::setOverride( const Bu::FString &sOpt, const Bu::FString &sOverride ) +void Bu::OptParser::setOverride( const Bu::FString &sOpt, const Bu::Variant &sOverride ) { hlOption.get( sOpt )->sOverride = sOverride; } -- cgit v1.2.3