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 
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 
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
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
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
virtual bool hasHelpAndVersion()=0
std::vector< std::vector< Arg * > > & getXorList()
Definition: XorHandler.h:153
GLenum GLuint GLenum GLsizei const GLchar * message
GLuint GLuint end
GLdouble s
virtual std::string & getProgramName()=0
virtual void usage(CmdLineInterface &c)
Definition: StdOutput.h:117
GLsizei const GLchar *const * string
virtual std::list< Arg * > & getArgList()=0
virtual void version(CmdLineInterface &c)
Definition: StdOutput.h:108
void _longUsage(CmdLineInterface &c, std::ostream &os) const
Definition: StdOutput.h:191
e
Definition: rmse.py:177
GLenum GLsizei len
Definition: glext.h:3285
void _shortUsage(CmdLineInterface &c, std::ostream &os) const
Definition: StdOutput.h:156
not_this_one begin(...)
std::ostream & cout()
const GLubyte * c
Definition: glext.h:12690
virtual std::string & getVersion()=0
virtual XorHandler & getXorHandler()=0
std::string error() const
Definition: ArgException.h:64
GLuint start
std::list< Arg * >::iterator ArgListIterator
Definition: Arg.h:396
bool contains(const Arg *a)
Definition: XorHandler.h:141
std::vector< Arg * >::iterator ArgVectorIterator
Definition: Arg.h:401
static auto it
virtual std::string & getMessage()=0
std::string argId() const
Definition: ArgException.h:69
std::ostream & cerr()
int i
Definition: Arg.h:57
virtual void failure(CmdLineInterface &c, ArgException &e)
Definition: StdOutput.h:131
void spacePrint(std::ostream &os, const std::string &s, int maxWidth, int indentSpaces, int secondLineOffset) const
Definition: StdOutput.h:229


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:10