From 469bbcf0701e1eb8a6670c23145b0da87357e178 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 25 Mar 2012 20:00:08 +0000 Subject: Code is all reorganized. We're about ready to release. I should write up a little explenation of the arrangement. --- src/stable/optparser.h | 223 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 src/stable/optparser.h (limited to 'src/stable/optparser.h') diff --git a/src/stable/optparser.h b/src/stable/optparser.h new file mode 100644 index 0000000..f2fe531 --- /dev/null +++ b/src/stable/optparser.h @@ -0,0 +1,223 @@ +/* + * Copyright (C) 2007-2011 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 << Bu::Fmt().tokenize( false ); + 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 + { + Bu::MemBuf mb; + Bu::Formatter f( mb ); +// f << vVar; + setValueFromStr( mb.getString() ); + } + } + + 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