00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef TCLAP_STDCMDLINEOUTPUT_H
00023 #define TCLAP_STDCMDLINEOUTPUT_H
00024
00025 #include <string>
00026 #include <vector>
00027 #include <list>
00028 #include <iostream>
00029 #include <algorithm>
00030
00031 #include <mrpt/otherlibs/tclap/CmdLineInterface.h>
00032 #include <mrpt/otherlibs/tclap/CmdLineOutput.h>
00033 #include <mrpt/otherlibs/tclap/XorHandler.h>
00034 #include <mrpt/otherlibs/tclap/Arg.h>
00035
00036 namespace TCLAP {
00037
00038
00039
00040
00041
00042 class StdOutput : public CmdLineOutput
00043 {
00044 protected:
00045 std::ostream &m_my_output;
00046
00047 public:
00048
00049
00050
00051
00052
00053 StdOutput( std::ostream &desired_out = std::cout ) :
00054 m_my_output(desired_out)
00055 {
00056 }
00057
00058
00059
00060
00061
00062
00063 virtual void usage(CmdLineInterface& c);
00064
00065
00066
00067
00068
00069
00070 virtual void version(CmdLineInterface& c);
00071
00072
00073
00074
00075
00076
00077
00078 virtual void failure(CmdLineInterface& c,
00079 ArgException& e );
00080
00081 protected:
00082
00083
00084
00085
00086
00087
00088 void _shortUsage( CmdLineInterface& c, std::ostream& os ) const;
00089
00090
00091
00092
00093
00094
00095
00096 void _longUsage( CmdLineInterface& c, std::ostream& os ) const;
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 void spacePrint( std::ostream& os,
00110 const std::string& s,
00111 int maxWidth,
00112 int indentSpaces,
00113 int secondLineOffset ) const;
00114
00115 };
00116
00117
00118 inline void StdOutput::version(CmdLineInterface& _cmd)
00119 {
00120 std::string progName = _cmd.getProgramName();
00121 std::string version = _cmd.getVersion();
00122
00123 m_my_output << std::endl << progName << " version: "
00124 << version << std::endl << std::endl;
00125 }
00126
00127 inline void StdOutput::usage(CmdLineInterface& _cmd )
00128 {
00129 m_my_output << std::endl << "USAGE: " << std::endl << std::endl;
00130
00131 _shortUsage( _cmd, m_my_output );
00132
00133 m_my_output << std::endl << std::endl << "Where: " << std::endl << std::endl;
00134
00135 _longUsage( _cmd, m_my_output );
00136
00137 m_my_output << std::endl;
00138
00139 }
00140
00141 inline void StdOutput::failure( CmdLineInterface& _cmd,
00142 ArgException& e )
00143 {
00144 std::string progName = _cmd.getProgramName();
00145
00146 std::cerr << "PARSE ERROR: " << e.argId() << std::endl
00147 << " " << e.error() << std::endl << std::endl;
00148
00149 if ( _cmd.hasHelpAndVersion() )
00150 {
00151 std::cerr << "Brief USAGE: " << std::endl;
00152
00153 _shortUsage( _cmd, std::cerr );
00154
00155 std::cerr << std::endl << "For complete USAGE and HELP type: "
00156 << std::endl << " " << progName << " --help"
00157 << std::endl << std::endl;
00158 }
00159 else
00160 usage(_cmd);
00161
00162 }
00163
00164 inline void StdOutput::_shortUsage( CmdLineInterface& _cmd,
00165 std::ostream& os ) const
00166 {
00167 std::list<Arg*> argList = _cmd.getArgList();
00168 std::string progName = _cmd.getProgramName();
00169 XorHandler xorHandler = _cmd.getXorHandler();
00170 std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
00171
00172 std::string s = progName + " ";
00173
00174
00175 for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
00176 {
00177 s += " {";
00178 for ( ArgVectorIterator it = xorList[i].begin();
00179 it != xorList[i].end(); it++ )
00180 s += (*it)->shortID() + "|";
00181
00182 s[s.length()-1] = '}';
00183 }
00184
00185
00186 for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
00187 if ( !xorHandler.contains( (*it) ) )
00188 s += " " + (*it)->shortID();
00189
00190
00191 int secondLineOffset = static_cast<int>(progName.length()) + 2;
00192 if ( secondLineOffset > 75/2 )
00193 secondLineOffset = static_cast<int>(75/2);
00194
00195 spacePrint( m_my_output, s, 75, 3, secondLineOffset );
00196 }
00197
00198 inline void StdOutput::_longUsage( CmdLineInterface& _cmd,
00199 std::ostream& os ) const
00200 {
00201 std::list<Arg*> argList = _cmd.getArgList();
00202 std::string message = _cmd.getMessage();
00203 XorHandler xorHandler = _cmd.getXorHandler();
00204 std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
00205
00206
00207 for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
00208 {
00209 for ( ArgVectorIterator it = xorList[i].begin();
00210 it != xorList[i].end();
00211 it++ )
00212 {
00213 spacePrint( os, (*it)->longID(), 75, 3, 3 );
00214 spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
00215
00216 if ( it+1 != xorList[i].end() )
00217 spacePrint(os, "-- OR --", 75, 9, 0);
00218 }
00219 os << std::endl << std::endl;
00220 }
00221
00222
00223 for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
00224 if ( !xorHandler.contains( (*it) ) )
00225 {
00226 spacePrint( os, (*it)->longID(), 75, 3, 3 );
00227 spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
00228 os << std::endl;
00229 }
00230
00231 os << std::endl;
00232
00233 spacePrint( os, message, 75, 3, 0 );
00234 }
00235
00236 inline void StdOutput::spacePrint( std::ostream& os,
00237 const std::string& s,
00238 int maxWidth,
00239 int indentSpaces,
00240 int secondLineOffset ) const
00241 {
00242 int len = static_cast<int>(s.length());
00243
00244 if ( (len + indentSpaces > maxWidth) && maxWidth > 0 )
00245 {
00246 int allowedLen = maxWidth - indentSpaces;
00247 int start = 0;
00248 while ( start < len )
00249 {
00250
00251 int stringLen = std::min( len - start, allowedLen );
00252
00253
00254 if ( stringLen == allowedLen )
00255 while ( s[stringLen+start] != ' ' &&
00256 s[stringLen+start] != ',' &&
00257 s[stringLen+start] != '|' &&
00258 stringLen >= 0 )
00259 stringLen--;
00260
00261
00262
00263 if ( stringLen <= 0 )
00264 stringLen = allowedLen;
00265
00266
00267 for ( int i = 0; i < stringLen; i++ )
00268 if ( s[start+i] == '\n' )
00269 stringLen = i+1;
00270
00271
00272 for ( int i = 0; i < indentSpaces; i++ )
00273 os << " ";
00274
00275 if ( start == 0 )
00276 {
00277
00278 indentSpaces += secondLineOffset;
00279
00280
00281 allowedLen -= secondLineOffset;
00282 }
00283
00284 os << s.substr(start,stringLen) << std::endl;
00285
00286
00287 while ( s[stringLen+start] == ' ' && start < len )
00288 start++;
00289
00290 start += stringLen;
00291 }
00292 }
00293 else
00294 {
00295 for ( int i = 0; i < indentSpaces; i++ )
00296 os << " ";
00297 os << s << std::endl;
00298 }
00299 }
00300
00301 }
00302 #endif