/* * Copyright (C) 2007-2010 Xagasoft, All rights reserved. * * This file is part of the libbu++ library and is released under the * terms of the license contained in the file LICENSE. */ #ifndef BU_OPT_PARSER_H #define BU_OPT_PARSER_H #include "bu/string.h" #include "bu/list.h" #include "bu/hash.h" #include "bu/signals.h" #include "bu/array.h" #include "bu/membuf.h" #include "bu/formatter.h" #include "bu/variant.h" namespace Bu { typedef Bu::Array StrArray; /** * POSIX/Gnu style command line parser. Handles long and short options in * a variety of fun and useful ways, along with singal based callbacks and * automatic variable setting. It's pretty easy to use, and very flexible. * * OptParser supports it's own builtin help mechanism which automatically * enumerates the available options and their help in a well formatted and * easy to read way, automatically formatting your help text per option and * allows for addition "help banners" which can be placed wherever you * would like. */ class OptParser { private: class _ValueProxy { public: _ValueProxy(); virtual ~_ValueProxy(); virtual void setValueFromStr( const Bu::String & )=0; virtual void setValue( const Bu::Variant &vVar )=0; virtual _ValueProxy *clone()=0; }; template class ValueProxy : public _ValueProxy { public: ValueProxy( ptype &v ) : v( v ) { } virtual ~ValueProxy() { } virtual void setValueFromStr( const Bu::String &sVal ) { Bu::MemBuf mb( sVal ); Bu::Formatter f( mb ); f >> v; } virtual void setValue( const Bu::Variant &vVar ) { if( vVar.getType() == typeid(ptype) ) { v = vVar.get(); } else if( vVar.getType() == typeid(Bu::String) ) { setValueFromStr( vVar.get() ); } else { setValueFromStr( vVar.toString() ); } } virtual _ValueProxy *clone() { return new ValueProxy( v ); } private: ptype &v; }; public: typedef Signal1 OptionSignal; class Option { public: Option(); Option( const Option &rSrc ); virtual ~Option(); char cOpt; Bu::String sOpt; Bu::String sHelp; OptionSignal sUsed; _ValueProxy *pProxy; Bu::Variant sOverride; Bu::String sHelpDefault; }; private: typedef Bu::List