summaryrefslogtreecommitdiff
path: root/src/stable/optparser.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-11-05 22:41:51 +0000
committerMike Buland <eichlan@xagasoft.com>2012-11-05 22:41:51 +0000
commitec05778d5718a7912e506764d443a78d6a6179e3 (patch)
tree78a9a01532180030c095acefc45763f07c14edb8 /src/stable/optparser.h
parentb20414ac1fe80a71a90601f4cd1767fa7014a9ba (diff)
downloadlibbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.gz
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.bz2
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.xz
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.zip
Converted tabs to spaces with tabconv.
Diffstat (limited to 'src/stable/optparser.h')
-rw-r--r--src/stable/optparser.h398
1 files changed, 199 insertions, 199 deletions
diff --git a/src/stable/optparser.h b/src/stable/optparser.h
index 2da93c9..412f262 100644
--- a/src/stable/optparser.h
+++ b/src/stable/optparser.h
@@ -19,205 +19,205 @@
19 19
20namespace Bu 20namespace Bu
21{ 21{
22 typedef Bu::Array<Bu::String> StrArray; 22 typedef Bu::Array<Bu::String> StrArray;
23 23
24 /** 24 /**
25 * POSIX/Gnu style command line parser. Handles long and short options in 25 * POSIX/Gnu style command line parser. Handles long and short options in
26 * a variety of fun and useful ways, along with singal based callbacks and 26 * a variety of fun and useful ways, along with singal based callbacks and
27 * automatic variable setting. It's pretty easy to use, and very flexible. 27 * automatic variable setting. It's pretty easy to use, and very flexible.
28 * 28 *
29 * OptParser supports it's own builtin help mechanism which automatically 29 * OptParser supports it's own builtin help mechanism which automatically
30 * enumerates the available options and their help in a well formatted and 30 * enumerates the available options and their help in a well formatted and
31 * easy to read way, automatically formatting your help text per option and 31 * easy to read way, automatically formatting your help text per option and
32 * allows for addition "help banners" which can be placed wherever you 32 * allows for addition "help banners" which can be placed wherever you
33 * would like. 33 * would like.
34 */ 34 */
35 class OptParser 35 class OptParser
36 { 36 {
37 private: 37 private:
38 class _ValueProxy 38 class _ValueProxy
39 { 39 {
40 public: 40 public:
41 _ValueProxy(); 41 _ValueProxy();
42 virtual ~_ValueProxy(); 42 virtual ~_ValueProxy();
43 43
44 virtual void setValueFromStr( const Bu::String & )=0; 44 virtual void setValueFromStr( const Bu::String & )=0;
45 virtual void setValue( const Bu::Variant &vVar )=0; 45 virtual void setValue( const Bu::Variant &vVar )=0;
46 virtual _ValueProxy *clone()=0; 46 virtual _ValueProxy *clone()=0;
47 }; 47 };
48 48
49 template<typename ptype> 49 template<typename ptype>
50 class ValueProxy : public _ValueProxy 50 class ValueProxy : public _ValueProxy
51 { 51 {
52 public: 52 public:
53 ValueProxy( ptype &v ) : 53 ValueProxy( ptype &v ) :
54 v( v ) 54 v( v )
55 { 55 {
56 } 56 }
57 57
58 virtual ~ValueProxy() 58 virtual ~ValueProxy()
59 { 59 {
60 } 60 }
61 61
62 virtual void setValueFromStr( const Bu::String &sVal ) 62 virtual void setValueFromStr( const Bu::String &sVal )
63 { 63 {
64 Bu::MemBuf mb( sVal ); 64 Bu::MemBuf mb( sVal );
65 Bu::Formatter f( mb ); 65 Bu::Formatter f( mb );
66 f << Bu::Fmt().tokenize( false ); 66 f << Bu::Fmt().tokenize( false );
67 f >> v; 67 f >> v;
68 } 68 }
69 69
70 virtual void setValue( const Bu::Variant &vVar ) 70 virtual void setValue( const Bu::Variant &vVar )
71 { 71 {
72 if( vVar.getType() == typeid(ptype) ) 72 if( vVar.getType() == typeid(ptype) )
73 { 73 {
74 v = vVar.get<ptype>(); 74 v = vVar.get<ptype>();
75 } 75 }
76 else if( vVar.getType() == typeid(Bu::String) ) 76 else if( vVar.getType() == typeid(Bu::String) )
77 { 77 {
78 setValueFromStr( vVar.get<Bu::String>() ); 78 setValueFromStr( vVar.get<Bu::String>() );
79 } 79 }
80 else 80 else
81 { 81 {
82 Bu::MemBuf mb; 82 Bu::MemBuf mb;
83 Bu::Formatter f( mb ); 83 Bu::Formatter f( mb );
84// f << vVar; 84// f << vVar;
85 setValueFromStr( mb.getString() ); 85 setValueFromStr( mb.getString() );
86 } 86 }
87 } 87 }
88 88
89 virtual _ValueProxy *clone() 89 virtual _ValueProxy *clone()
90 { 90 {
91 return new ValueProxy<ptype>( v ); 91 return new ValueProxy<ptype>( v );
92 } 92 }
93 93
94 private: 94 private:
95 ptype &v; 95 ptype &v;
96 }; 96 };
97 97
98 public: 98 public:
99 typedef Signal1<int, StrArray> OptionSignal; 99 typedef Signal1<int, StrArray> OptionSignal;
100 class Option 100 class Option
101 { 101 {
102 public: 102 public:
103 Option(); 103 Option();
104 Option( const Option &rSrc ); 104 Option( const Option &rSrc );
105 virtual ~Option(); 105 virtual ~Option();
106 106
107 char cOpt; 107 char cOpt;
108 Bu::String sOpt; 108 Bu::String sOpt;
109 Bu::String sHelp; 109 Bu::String sHelp;
110 OptionSignal sUsed; 110 OptionSignal sUsed;
111 _ValueProxy *pProxy; 111 _ValueProxy *pProxy;
112 Bu::Variant sOverride; 112 Bu::Variant sOverride;
113 Bu::String sHelpDefault; 113 Bu::String sHelpDefault;
114 }; 114 };
115 115
116 private: 116 private:
117 typedef Bu::List<Option> OptionList; 117 typedef Bu::List<Option> OptionList;
118 typedef Bu::Hash<char, Option *> ShortOptionHash; 118 typedef Bu::Hash<char, Option *> ShortOptionHash;
119 typedef Bu::Hash<Bu::String, Option *> LongOptionHash; 119 typedef Bu::Hash<Bu::String, Option *> LongOptionHash;
120 120
121 class Banner 121 class Banner
122 { 122 {
123 public: 123 public:
124 Bu::String sText; 124 Bu::String sText;
125 bool bFormatted; 125 bool bFormatted;
126 OptionList::const_iterator iAfter; 126 OptionList::const_iterator iAfter;
127 }; 127 };
128 128
129 typedef Bu::List<Banner> BannerList; 129 typedef Bu::List<Banner> BannerList;
130 130
131 public: 131 public:
132 OptParser(); 132 OptParser();
133 virtual ~OptParser(); 133 virtual ~OptParser();
134 134
135 void parse( int argc, char **argv ); 135 void parse( int argc, char **argv );
136 void parse( const Bu::String &sLine ); 136 void parse( const Bu::String &sLine );
137 137
138 void addOption( const Option &opt ); 138 void addOption( const Option &opt );
139 139
140 template<typename vtype> 140 template<typename vtype>
141 void addOption( vtype &var, char cOpt, const Bu::String &sOpt, 141 void addOption( vtype &var, char cOpt, const Bu::String &sOpt,
142 const Bu::String &sHelp ) 142 const Bu::String &sHelp )
143 { 143 {
144 Option o; 144 Option o;
145 o.cOpt = cOpt; 145 o.cOpt = cOpt;
146 o.sOpt = sOpt; 146 o.sOpt = sOpt;
147 o.pProxy = new ValueProxy<vtype>( var ); 147 o.pProxy = new ValueProxy<vtype>( var );
148 o.sHelp = sHelp; 148 o.sHelp = sHelp;
149 addOption( o ); 149 addOption( o );
150 } 150 }
151 151
152 template<typename vtype> 152 template<typename vtype>
153 void addOption( vtype &var, const Bu::String &sOpt, 153 void addOption( vtype &var, const Bu::String &sOpt,
154 const Bu::String &sHelp ) 154 const Bu::String &sHelp )
155 { 155 {
156 addOption( var, '\0', sOpt, sHelp ); 156 addOption( var, '\0', sOpt, sHelp );
157 } 157 }
158 158
159 template<typename vtype> 159 template<typename vtype>
160 void addOption( vtype &var, char cOpt, const Bu::String &sHelp ) 160 void addOption( vtype &var, char cOpt, const Bu::String &sHelp )
161 { 161 {
162 addOption( var, cOpt, "", sHelp ); 162 addOption( var, cOpt, "", sHelp );
163 } 163 }
164 164
165 void addOption( OptionSignal sUsed, char cOpt, const Bu::String &sOpt, 165 void addOption( OptionSignal sUsed, char cOpt, const Bu::String &sOpt,
166 const Bu::String &sHelp ) 166 const Bu::String &sHelp )
167 { 167 {
168 Option o; 168 Option o;
169 o.cOpt = cOpt; 169 o.cOpt = cOpt;
170 o.sOpt = sOpt; 170 o.sOpt = sOpt;
171 o.sUsed = sUsed; 171 o.sUsed = sUsed;
172 o.sHelp = sHelp; 172 o.sHelp = sHelp;
173 addOption( o ); 173 addOption( o );
174 } 174 }
175 175
176 void addOption( OptionSignal sUsed, const Bu::String &sOpt, 176 void addOption( OptionSignal sUsed, const Bu::String &sOpt,
177 const Bu::String &sHelp ) 177 const Bu::String &sHelp )
178 { 178 {
179 addOption( sUsed, '\0', sOpt, sHelp ); 179 addOption( sUsed, '\0', sOpt, sHelp );
180 } 180 }
181 181
182 void addOption( OptionSignal sUsed, char cOpt, 182 void addOption( OptionSignal sUsed, char cOpt,
183 const Bu::String &sHelp ) 183 const Bu::String &sHelp )
184 { 184 {
185 addOption( sUsed, cOpt, "", sHelp ); 185 addOption( sUsed, cOpt, "", sHelp );
186 } 186 }
187 187
188 void setOverride( char cOpt, const Bu::Variant &sOverride ); 188 void setOverride( char cOpt, const Bu::Variant &sOverride );
189 void setOverride( const Bu::String &sOpt, 189 void setOverride( const Bu::String &sOpt,
190 const Bu::Variant &sOverride ); 190 const Bu::Variant &sOverride );
191 191
192 void setHelpDefault( const Bu::String &sOpt, const Bu::String &sTxt ); 192 void setHelpDefault( const Bu::String &sOpt, const Bu::String &sTxt );
193 193
194 void addHelpOption( char c='h', const Bu::String &s="help", 194 void addHelpOption( char c='h', const Bu::String &s="help",
195 const Bu::String &sHelp="This help." ); 195 const Bu::String &sHelp="This help." );
196 void addHelpBanner( const Bu::String &sText, bool bFormatted=true ); 196 void addHelpBanner( const Bu::String &sText, bool bFormatted=true );
197 197
198 int optHelp( StrArray aParams ); 198 int optHelp( StrArray aParams );
199 199
200 /** 200 /**
201 * This function is called when an unrecognized option is found, the 201 * This function is called when an unrecognized option is found, the
202 * default behaviour is to print an error to stdout and exit( 1 ), if 202 * default behaviour is to print an error to stdout and exit( 1 ), if
203 * you want to do something different, just override this function. 203 * you want to do something different, just override this function.
204 * This is also called by default when something is found that hasn't 204 * This is also called by default when something is found that hasn't
205 * been handled by an option, and isn't an option (starts with - or --). 205 * been handled by an option, and isn't an option (starts with - or --).
206 * To change this behaviour call 206 * To change this behaviour call
207 */ 207 */
208 virtual void optionError( const Bu::String &sOption ); 208 virtual void optionError( const Bu::String &sOption );
209 209
210 void setNonOption( OptionSignal sSignal ); 210 void setNonOption( OptionSignal sSignal );
211 211
212 private: 212 private:
213 Bu::String format( const Bu::String &sIn, int iWidth, int iIndent ); 213 Bu::String format( const Bu::String &sIn, int iWidth, int iIndent );
214 214
215 OptionList lOption; 215 OptionList lOption;
216 ShortOptionHash hsOption; 216 ShortOptionHash hsOption;
217 LongOptionHash hlOption; 217 LongOptionHash hlOption;
218 BannerList lBanner; 218 BannerList lBanner;
219 OptionSignal sNonOption; 219 OptionSignal sNonOption;
220 }; 220 };
221}; 221};
222 222
223#endif 223#endif