aboutsummaryrefslogtreecommitdiff
path: root/src/optparser.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-12-18 08:59:16 +0000
committerMike Buland <eichlan@xagasoft.com>2009-12-18 08:59:16 +0000
commit146930268a695dcc0432599d625ec3eb7e74025e (patch)
treea944d13981ed055b337757953014ed1e2d45e19c /src/optparser.h
parent0d3d73fb0cacd3d1cf7eb8b83ba87f8b740b871a (diff)
downloadlibbu++-146930268a695dcc0432599d625ec3eb7e74025e.tar.gz
libbu++-146930268a695dcc0432599d625ec3eb7e74025e.tar.bz2
libbu++-146930268a695dcc0432599d625ec3eb7e74025e.tar.xz
libbu++-146930268a695dcc0432599d625ec3eb7e74025e.zip
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.
Diffstat (limited to '')
-rw-r--r--src/optparser.h59
1 files changed, 59 insertions, 0 deletions
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 @@
6#include "bu/hash.h" 6#include "bu/hash.h"
7#include "bu/signals.h" 7#include "bu/signals.h"
8#include "bu/array.h" 8#include "bu/array.h"
9#include "bu/membuf.h"
10#include "bu/formatter.h"
9 11
10namespace Bu 12namespace Bu
11{ 13{
@@ -13,11 +15,51 @@ namespace Bu
13 class OptParser 15 class OptParser
14 { 16 {
15 public: 17 public:
18 class _ValueProxy
19 {
20 public:
21 _ValueProxy();
22 virtual ~_ValueProxy();
23
24 virtual void setValue( const Bu::FString & )=0;
25 virtual _ValueProxy *clone()=0;
26 };
27
28 template<typename ptype>
29 class ValueProxy : public _ValueProxy
30 {
31 public:
32 ValueProxy( ptype &v ) :
33 v( v )
34 {
35 }
36
37 virtual ~ValueProxy()
38 {
39 }
40
41 virtual void setValue( const Bu::FString &sVal )
42 {
43 Bu::MemBuf mb( sVal );
44 Bu::Formatter f( mb );
45 f >> v;
46 }
47
48 virtual _ValueProxy *clone()
49 {
50 return new ValueProxy<ptype>( v );
51 }
52
53 private:
54 ptype &v;
55 };
56
16 typedef Signal1<int, StrArray> OptionSignal; 57 typedef Signal1<int, StrArray> OptionSignal;
17 class Option 58 class Option
18 { 59 {
19 public: 60 public:
20 Option(); 61 Option();
62 Option( const Option &rSrc );
21 virtual ~Option(); 63 virtual ~Option();
22 64
23 char cOpt; 65 char cOpt;
@@ -25,6 +67,8 @@ namespace Bu
25 Bu::FString sHelp; 67 Bu::FString sHelp;
26 OptionSignal sUsed; 68 OptionSignal sUsed;
27 bool bShortHasParams; 69 bool bShortHasParams;
70 _ValueProxy *pProxy;
71 Bu::FString sOverride;
28 }; 72 };
29 73
30 public: 74 public:
@@ -34,6 +78,21 @@ namespace Bu
34 void parse( int argc, char **argv ); 78 void parse( int argc, char **argv );
35 79
36 void addOption( const Option &opt ); 80 void addOption( const Option &opt );
81
82 template<typename vtype>
83 void addOption( char cOpt, const Bu::FString &sOpt, vtype &var,
84 const Bu::FString &sHelp="", const Bu::FString &sOverride="" )
85 {
86 Option o;
87 o.cOpt = cOpt;
88 o.sOpt = sOpt;
89 o.pProxy = new ValueProxy<vtype>( var );
90 o.bShortHasParams = true;
91 o.sHelp = sHelp;
92 o.sOverride = sOverride;
93 addOption( o );
94 }
95
37 void addHelpOption( char c, const Bu::FString &s, const Bu::FString &sHelp ); 96 void addHelpOption( char c, const Bu::FString &s, const Bu::FString &sHelp );
38 97
39 int optHelp( StrArray aParams ); 98 int optHelp( StrArray aParams );