summaryrefslogtreecommitdiff
path: root/src/optparser.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/optparser.h')
-rw-r--r--src/optparser.h74
1 files changed, 65 insertions, 9 deletions
diff --git a/src/optparser.h b/src/optparser.h
index acfb35d..425bc90 100644
--- a/src/optparser.h
+++ b/src/optparser.h
@@ -14,7 +14,7 @@ namespace Bu
14 typedef Bu::Array<Bu::FString> StrArray; 14 typedef Bu::Array<Bu::FString> StrArray;
15 class OptParser 15 class OptParser
16 { 16 {
17 public: 17 private:
18 class _ValueProxy 18 class _ValueProxy
19 { 19 {
20 public: 20 public:
@@ -54,6 +54,7 @@ namespace Bu
54 ptype &v; 54 ptype &v;
55 }; 55 };
56 56
57 public:
57 typedef Signal1<int, StrArray> OptionSignal; 58 typedef Signal1<int, StrArray> OptionSignal;
58 class Option 59 class Option
59 { 60 {
@@ -70,6 +71,21 @@ namespace Bu
70 _ValueProxy *pProxy; 71 _ValueProxy *pProxy;
71 Bu::FString sOverride; 72 Bu::FString sOverride;
72 }; 73 };
74
75 private:
76 typedef Bu::List<Option> OptionList;
77 typedef Bu::Hash<char, Option *> ShortOptionHash;
78 typedef Bu::Hash<Bu::FString, Option *> LongOptionHash;
79
80 class Banner
81 {
82 public:
83 Bu::FString sText;
84 bool bFormatted;
85 OptionList::const_iterator iAfter;
86 };
87
88 typedef Bu::List<Banner> BannerList;
73 89
74 public: 90 public:
75 OptParser(); 91 OptParser();
@@ -80,8 +96,8 @@ namespace Bu
80 void addOption( const Option &opt ); 96 void addOption( const Option &opt );
81 97
82 template<typename vtype> 98 template<typename vtype>
83 void addOption( char cOpt, const Bu::FString &sOpt, vtype &var, 99 void addOption( vtype &var, char cOpt, const Bu::FString &sOpt,
84 const Bu::FString &sHelp="", const Bu::FString &sOverride="" ) 100 const Bu::FString &sHelp )
85 { 101 {
86 Option o; 102 Option o;
87 o.cOpt = cOpt; 103 o.cOpt = cOpt;
@@ -89,24 +105,64 @@ namespace Bu
89 o.pProxy = new ValueProxy<vtype>( var ); 105 o.pProxy = new ValueProxy<vtype>( var );
90 o.bShortHasParams = true; 106 o.bShortHasParams = true;
91 o.sHelp = sHelp; 107 o.sHelp = sHelp;
92 o.sOverride = sOverride;
93 addOption( o ); 108 addOption( o );
94 } 109 }
95 110
96 void addHelpOption( char c, const Bu::FString &s, const Bu::FString &sHelp ); 111 template<typename vtype>
112 void addOption( vtype &var, const Bu::FString &sOpt,
113 const Bu::FString &sHelp )
114 {
115 addOption( var, '\0', sOpt, sHelp );
116 }
117
118 template<typename vtype>
119 void addOption( vtype &var, char cOpt, const Bu::FString &sHelp )
120 {
121 addOption( var, cOpt, "", sHelp );
122 }
123
124 void addOption( OptionSignal sUsed, char cOpt, const Bu::FString &sOpt,
125 const Bu::FString &sHelp )
126 {
127 Option o;
128 o.cOpt = cOpt;
129 o.sOpt = sOpt;
130 o.sUsed = sUsed;
131 o.sHelp = sHelp;
132 addOption( o );
133 }
134
135 void addOption( OptionSignal sUsed, const Bu::FString &sOpt,
136 const Bu::FString &sHelp )
137 {
138 addOption( sUsed, '\0', sOpt, sHelp );
139 }
140
141 void addOption( OptionSignal sUsed, char cOpt,
142 const Bu::FString &sHelp )
143 {
144 addOption( sUsed, cOpt, "", sHelp );
145 }
146
147 void setOverride( char cOpt, const Bu::FString &sOverride );
148 void setOverride( const Bu::FString &sOpt,
149 const Bu::FString &sOverride );
150
151// void addOption( char cOpt, const Bu::FString &sOpt,
152
153 void addHelpOption( char c='h', const Bu::FString &s="help",
154 const Bu::FString &sHelp="This help." );
155 void addHelpBanner( const Bu::FString &sText, bool bFormatted=true );
97 156
98 int optHelp( StrArray aParams ); 157 int optHelp( StrArray aParams );
99 158
100 private: 159 private:
101 Bu::FString format( const Bu::FString &sIn, int iWidth, int iIndent ); 160 Bu::FString format( const Bu::FString &sIn, int iWidth, int iIndent );
102 161
103 typedef Bu::List<Option> OptionList;
104 typedef Bu::Hash<char, Option *> ShortOptionHash;
105 typedef Bu::Hash<Bu::FString, Option *> LongOptionHash;
106
107 OptionList lOption; 162 OptionList lOption;
108 ShortOptionHash hsOption; 163 ShortOptionHash hsOption;
109 LongOptionHash hlOption; 164 LongOptionHash hlOption;
165 BannerList lBanner;
110 }; 166 };
111}; 167};
112 168