summaryrefslogtreecommitdiff
path: root/src/optparser.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-12-18 16:07:31 +0000
committerMike Buland <eichlan@xagasoft.com>2009-12-18 16:07:31 +0000
commit2cbc634b3b603ec67c6c312bd18177cd52c71b63 (patch)
treead743e9cd660f325e1020cdea4f746938d102261 /src/optparser.h
parent038815ae3a019ac56fa1c62e18c5861166d3a975 (diff)
downloadlibbu++-2cbc634b3b603ec67c6c312bd18177cd52c71b63.tar.gz
libbu++-2cbc634b3b603ec67c6c312bd18177cd52c71b63.tar.bz2
libbu++-2cbc634b3b603ec67c6c312bd18177cd52c71b63.tar.xz
libbu++-2cbc634b3b603ec67c6c312bd18177cd52c71b63.zip
Ok...sweet, the OptParser now supports everything the old one did, but in much
less code, and it does everything with more style and panache, also fewer bugs.
Diffstat (limited to 'src/optparser.h')
-rw-r--r--src/optparser.h29
1 files changed, 25 insertions, 4 deletions
diff --git a/src/optparser.h b/src/optparser.h
index 425bc90..89ef14d 100644
--- a/src/optparser.h
+++ b/src/optparser.h
@@ -12,6 +12,18 @@
12namespace Bu 12namespace Bu
13{ 13{
14 typedef Bu::Array<Bu::FString> StrArray; 14 typedef Bu::Array<Bu::FString> StrArray;
15
16 /**
17 * POSIX/Gnu style command line parser. Handles long and short options in
18 * a variety of fun and useful ways, along with singal based callbacks and
19 * automatic variable setting. It's pretty easy to use, and very flexible.
20 *
21 * OptParser supports it's own builtin help mechanism which automatically
22 * enumerates the available options and their help in a well formatted and
23 * easy to read way, automatically formatting your help text per option and
24 * allows for addition "help banners" which can be placed wherever you
25 * would like.
26 */
15 class OptParser 27 class OptParser
16 { 28 {
17 private: 29 private:
@@ -67,7 +79,6 @@ namespace Bu
67 Bu::FString sOpt; 79 Bu::FString sOpt;
68 Bu::FString sHelp; 80 Bu::FString sHelp;
69 OptionSignal sUsed; 81 OptionSignal sUsed;
70 bool bShortHasParams;
71 _ValueProxy *pProxy; 82 _ValueProxy *pProxy;
72 Bu::FString sOverride; 83 Bu::FString sOverride;
73 }; 84 };
@@ -103,7 +114,6 @@ namespace Bu
103 o.cOpt = cOpt; 114 o.cOpt = cOpt;
104 o.sOpt = sOpt; 115 o.sOpt = sOpt;
105 o.pProxy = new ValueProxy<vtype>( var ); 116 o.pProxy = new ValueProxy<vtype>( var );
106 o.bShortHasParams = true;
107 o.sHelp = sHelp; 117 o.sHelp = sHelp;
108 addOption( o ); 118 addOption( o );
109 } 119 }
@@ -148,14 +158,24 @@ namespace Bu
148 void setOverride( const Bu::FString &sOpt, 158 void setOverride( const Bu::FString &sOpt,
149 const Bu::FString &sOverride ); 159 const Bu::FString &sOverride );
150 160
151// void addOption( char cOpt, const Bu::FString &sOpt,
152
153 void addHelpOption( char c='h', const Bu::FString &s="help", 161 void addHelpOption( char c='h', const Bu::FString &s="help",
154 const Bu::FString &sHelp="This help." ); 162 const Bu::FString &sHelp="This help." );
155 void addHelpBanner( const Bu::FString &sText, bool bFormatted=true ); 163 void addHelpBanner( const Bu::FString &sText, bool bFormatted=true );
156 164
157 int optHelp( StrArray aParams ); 165 int optHelp( StrArray aParams );
158 166
167 /**
168 * This function is called when an unrecognized option is found, the
169 * default behaviour is to print an error to stdout and exit( 1 ), if
170 * you want to do something different, just override this function.
171 * This is also called by default when something is found that hasn't
172 * been handled by an option, and isn't an option (starts with - or --).
173 * To change this behaviour call
174 */
175 virtual void optionError( const Bu::FString sOption );
176
177 void setNonOption( OptionSignal sSignal );
178
159 private: 179 private:
160 Bu::FString format( const Bu::FString &sIn, int iWidth, int iIndent ); 180 Bu::FString format( const Bu::FString &sIn, int iWidth, int iIndent );
161 181
@@ -163,6 +183,7 @@ namespace Bu
163 ShortOptionHash hsOption; 183 ShortOptionHash hsOption;
164 LongOptionHash hlOption; 184 LongOptionHash hlOption;
165 BannerList lBanner; 185 BannerList lBanner;
186 OptionSignal sNonOption;
166 }; 187 };
167}; 188};
168 189