tclap 1.2.1

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

StdOutput.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3 /******************************************************************************
4 *
5 * file: StdOutput.h
6 *
7 * Copyright (c) 2004, Michael E. Smoot
8 * All rights reverved.
9 *
10 * See the file COPYING in the top directory of this distribution for
11 * more information.
12 *
13 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *****************************************************************************/
22
23 #ifndef TCLAP_STDCMDLINEOUTPUT_H
24 #define TCLAP_STDCMDLINEOUTPUT_H
25
26 #include <string>
27 #include <vector>
28 #include <list>
29 #include <iostream>
30 #include <algorithm>
31
32 #include <tclap/CmdLineInterface.h>
33 #include <tclap/CmdLineOutput.h>
34 #include <tclap/XorHandler.h>
35 #include <tclap/Arg.h>
36
37 namespace TCLAP {
38
43 class StdOutput : public CmdLineOutput
44 {
45
46 public:
47
53 virtual void usage(CmdLineInterface& c);
54
60 virtual void version(CmdLineInterface& c);
61
68 virtual void failure(CmdLineInterface& c,
69 ArgException& e );
70
71 protected:
72
78 void _shortUsage( CmdLineInterface& c, std::ostream& os ) const;
79
86 void _longUsage( CmdLineInterface& c, std::ostream& os ) const;
87
99 void spacePrint( std::ostream& os,
100 const std::string& s,
101 int maxWidth,
102 int indentSpaces,
103 int secondLineOffset ) const;
104
105 };
106
107
108 inline void StdOutput::version(CmdLineInterface& _cmd)
109 {
110 std::string progName = _cmd.getProgramName();
111 std::string xversion = _cmd.getVersion();
112
113 std::cout << std::endl << progName << " version: "
114 << xversion << std::endl << std::endl;
115 }
116
117 inline void StdOutput::usage(CmdLineInterface& _cmd )
118 {
119 std::cout << std::endl << "USAGE: " << std::endl << std::endl;
120
121 _shortUsage( _cmd, std::cout );
122
123 std::cout << std::endl << std::endl << "Where: " << std::endl << std::endl;
124
125 _longUsage( _cmd, std::cout );
126
127 std::cout << std::endl;
128
129 }
130
131 inline void StdOutput::failure( CmdLineInterface& _cmd,
132 ArgException& e )
133 {
134 std::string progName = _cmd.getProgramName();
135
136 std::cerr << "PARSE ERROR: " << e.argId() << std::endl
137 << " " << e.error() << std::endl << std::endl;
138
139 if ( _cmd.hasHelpAndVersion() )
140 {
141 std::cerr << "Brief USAGE: " << std::endl;
142
143 _shortUsage( _cmd, std::cerr );
144
145 std::cerr << std::endl << "For complete USAGE and HELP type: "
146 << std::endl << " " << progName << " --help"
147 << std::endl << std::endl;
148 }
149 else
150 usage(_cmd);
151
152 throw ExitException(1);
153 }
154
155 inline void
156 StdOutput::_shortUsage( CmdLineInterface& _cmd,
157 std::ostream& os ) const
158 {
159 std::list<Arg*> argList = _cmd.getArgList();
160 std::string progName = _cmd.getProgramName();
161 XorHandler xorHandler = _cmd.getXorHandler();
162 std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
163
164 std::string s = progName + " ";
165
166 // first the xor
167 for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
168 {
169 s += " {";
170 for ( ArgVectorIterator it = xorList[i].begin();
171 it != xorList[i].end(); it++ )
172 s += (*it)->shortID() + "|";
173
174 s[s.length()-1] = '}';
175 }
176
177 // then the rest
178 for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
179 if ( !xorHandler.contains( (*it) ) )
180 s += " " + (*it)->shortID();
181
182 // if the program name is too long, then adjust the second line offset
183 int secondLineOffset = static_cast<int>(progName.length()) + 2;
184 if ( secondLineOffset > 75/2 )
185 secondLineOffset = static_cast<int>(75/2);
186
187 spacePrint( os, s, 75, 3, secondLineOffset );
188 }
189
190 inline void
191 StdOutput::_longUsage( CmdLineInterface& _cmd,
192 std::ostream& os ) const
193 {
194 std::list<Arg*> argList = _cmd.getArgList();
195 std::string message = _cmd.getMessage();
196 XorHandler xorHandler = _cmd.getXorHandler();
197 std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
198
199 // first the xor
200 for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
201 {
202 for ( ArgVectorIterator it = xorList[i].begin();
203 it != xorList[i].end();
204 it++ )
205 {
206 spacePrint( os, (*it)->longID(), 75, 3, 3 );
207 spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
208
209 if ( it+1 != xorList[i].end() )
210 spacePrint(os, "-- OR --", 75, 9, 0);
211 }
212 os << std::endl << std::endl;
213 }
214
215 // then the rest
216 for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
217 if ( !xorHandler.contains( (*it) ) )
218 {
219 spacePrint( os, (*it)->longID(), 75, 3, 3 );
220 spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
221 os << std::endl;
222 }
223
224 os << std::endl;
225
226 spacePrint( os, message, 75, 3, 0 );
227 }
228
229 inline void StdOutput::spacePrint( std::ostream& os,
230 const std::string& s,
231 int maxWidth,
232 int indentSpaces,
233 int secondLineOffset ) const
234 {
235 int len = static_cast<int>(s.length());
236
237 if ( (len + indentSpaces > maxWidth) && maxWidth > 0 )
238 {
239 int allowedLen = maxWidth - indentSpaces;
240 int start = 0;
241 while ( start < len )
242 {
243 // find the substring length
244 // int stringLen = std::min<int>( len - start, allowedLen );
245 // doing it this way to support a VisualC++ 2005 bug
246 using namespace std;
247 int stringLen = min<int>( len - start, allowedLen );
248
249 // trim the length so it doesn't end in middle of a word
250 if ( stringLen == allowedLen )
251 while ( stringLen >= 0 &&
252 s[stringLen+start] != ' ' &&
253 s[stringLen+start] != ',' &&
254 s[stringLen+start] != '|' )
255 stringLen--;
256
257 // ok, the word is longer than the line, so just split
258 // wherever the line ends
259 if ( stringLen <= 0 )
260 stringLen = allowedLen;
261
262 // check for newlines
263 for ( int i = 0; i < stringLen; i++ )
264 if ( s[start+i] == '\n' )
265 stringLen = i+1;
266
267 // print the indent
268 for ( int i = 0; i < indentSpaces; i++ )
269 os << " ";
270
271 if ( start == 0 )
272 {
273 // handle second line offsets
274 indentSpaces += secondLineOffset;
275
276 // adjust allowed len
277 allowedLen -= secondLineOffset;
278 }
279
280 os << s.substr(start,stringLen) << std::endl;
281
282 // so we don't start a line with a space
283 while ( s[stringLen+start] == ' ' && start < len )
284 start++;
285
286 start += stringLen;
287 }
288 }
289 else
290 {
291 for ( int i = 0; i < indentSpaces; i++ )
292 os << " ";
293 os << s << std::endl;
294 }
295 }
296
297 } //namespace TCLAP
298 #endif
TCLAP::CmdLineInterface::hasHelpAndVersion
virtual bool hasHelpAndVersion()=0
Indicates whether or not the help and version switches were created automatically.
TCLAP::XorHandler::getXorList
std::vector< std::vector< Arg * > > & getXorList()
Definition: XorHandler.h:153
TCLAP::ArgException
A simple class that defines and argument exception.
Definition: ArgException.h:36
TCLAP::CmdLineInterface::getProgramName
virtual std::string & getProgramName()=0
Returns the program name string.
CmdLineOutput.h
TCLAP::StdOutput::usage
virtual void usage(CmdLineInterface &c)
Prints the usage to stdout.
Definition: StdOutput.h:117
TCLAP::ExitException
Definition: ArgException.h:187
TCLAP::CmdLineInterface::getArgList
virtual std::list< Arg * > & getArgList()=0
Returns the argList.
TCLAP::StdOutput::version
virtual void version(CmdLineInterface &c)
Prints the version to stdout.
Definition: StdOutput.h:108
XorHandler.h
TCLAP::StdOutput::_longUsage
void _longUsage(CmdLineInterface &c, std::ostream &os) const
Writes a longer usage message with long and short args, provides descriptions and prints message...
Definition: StdOutput.h:191
TCLAP::StdOutput::_shortUsage
void _shortUsage(CmdLineInterface &c, std::ostream &os) const
Writes a brief usage message with short args.
Definition: StdOutput.h:156
CmdLineInterface.h
TCLAP::CmdLineInterface::getVersion
virtual std::string & getVersion()=0
Returns the version string.
TCLAP::CmdLineInterface::getXorHandler
virtual XorHandler & getXorHandler()=0
Returns the XorHandler.
TCLAP::ArgException::error
std::string error() const
Returns the error text.
Definition: ArgException.h:64
TCLAP::ArgListIterator
std::list< Arg * >::iterator ArgListIterator
Typedef of an Arg list iterator.
Definition: Arg.h:396
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:43
TCLAP::XorHandler::contains
bool contains(const Arg *a)
Simply checks whether the Arg is contained in one of the arg lists.
Definition: XorHandler.h:141
TCLAP::ArgVectorIterator
std::vector< Arg * >::iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition: Arg.h:401
TCLAP::StdOutput
A class that isolates any output from the CmdLine object so that it may be easily modified...
Definition: StdOutput.h:43
Arg.h
TCLAP::CmdLineInterface::getMessage
virtual std::string & getMessage()=0
Returns the message string.
TCLAP::ArgException::argId
std::string argId() const
Returns the argument id.
Definition: ArgException.h:69
TCLAP
Definition: Arg.h:57
TCLAP::StdOutput::failure
virtual void failure(CmdLineInterface &c, ArgException &e)
Prints (to stderr) an error message, short usage Can be overridden to produce alternative behavior...
Definition: StdOutput.h:131
TCLAP::StdOutput::spacePrint
void spacePrint(std::ostream &os, const std::string &s, int maxWidth, int indentSpaces, int secondLineOffset) const
This function inserts line breaks and indents long strings according the params input.
Definition: StdOutput.h:229
TCLAP::XorHandler
This class handles lists of Arg's that are to be XOR'd on the command line.
Definition: XorHandler.h:38
TCLAP::CmdLineOutput
The interface that any output object must implement.
Definition: CmdLineOutput.h:41