From 146930268a695dcc0432599d625ec3eb7e74025e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 18 Dec 2009 08:59:16 +0000 Subject: 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. --- src/optparser.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'src/optparser.h') diff --git a/src/optparser.h b/src/optparser.h index ed32e45..acfb35d 100644 --- a/src/optparser.h +++ b/src/optparser.h @@ -6,6 +6,8 @@ #include "bu/hash.h" #include "bu/signals.h" #include "bu/array.h" +#include "bu/membuf.h" +#include "bu/formatter.h" namespace Bu { @@ -13,11 +15,51 @@ namespace Bu class OptParser { public: + class _ValueProxy + { + public: + _ValueProxy(); + virtual ~_ValueProxy(); + + virtual void setValue( const Bu::FString & )=0; + virtual _ValueProxy *clone()=0; + }; + + template + class ValueProxy : public _ValueProxy + { + public: + ValueProxy( ptype &v ) : + v( v ) + { + } + + virtual ~ValueProxy() + { + } + + virtual void setValue( const Bu::FString &sVal ) + { + Bu::MemBuf mb( sVal ); + Bu::Formatter f( mb ); + f >> v; + } + + virtual _ValueProxy *clone() + { + return new ValueProxy( v ); + } + + private: + ptype &v; + }; + typedef Signal1 OptionSignal; class Option { public: Option(); + Option( const Option &rSrc ); virtual ~Option(); char cOpt; @@ -25,6 +67,8 @@ namespace Bu Bu::FString sHelp; OptionSignal sUsed; bool bShortHasParams; + _ValueProxy *pProxy; + Bu::FString sOverride; }; public: @@ -34,6 +78,21 @@ namespace Bu void parse( int argc, char **argv ); void addOption( const Option &opt ); + + template + void addOption( char cOpt, const Bu::FString &sOpt, vtype &var, + const Bu::FString &sHelp="", const Bu::FString &sOverride="" ) + { + Option o; + o.cOpt = cOpt; + o.sOpt = sOpt; + o.pProxy = new ValueProxy( var ); + o.bShortHasParams = true; + o.sHelp = sHelp; + o.sOverride = sOverride; + addOption( o ); + } + void addHelpOption( char c, const Bu::FString &s, const Bu::FString &sHelp ); int optHelp( StrArray aParams ); -- cgit v1.2.3