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



Page generated by Doxygen 1.9.1 for MRPT 1.4.0 SVN: at Sun Mar 7 18:43:46 UTC 2021