00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef TCLAP_DOCBOOKOUTPUT_H
00023 #define TCLAP_DOCBOOKOUTPUT_H
00024
00025 #include <string>
00026 #include <vector>
00027 #include <list>
00028 #include <iostream>
00029 #include <algorithm>
00030
00031 #include <tclap/CmdLineInterface.h>
00032 #include <tclap/CmdLineOutput.h>
00033 #include <tclap/XorHandler.h>
00034 #include <tclap/Arg.h>
00035
00036 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00037
00038 namespace TCLAP {
00039
00045 class DocBookOutput : public CmdLineOutput
00046 {
00047
00048 public:
00049
00055 virtual void usage(CmdLineInterface& c);
00056
00062 virtual void version(CmdLineInterface& c);
00063
00070 virtual void failure(CmdLineInterface& c,
00071 ArgException& e );
00072
00073 protected:
00074
00081 void substituteSpecialChars( std::string& s, char r, std::string& x );
00082 void removeChar( std::string& s, char r);
00083
00084 void printShortArg(Arg* it);
00085 void printLongArg(Arg* it);
00086 };
00087
00088
00089 inline void DocBookOutput::version(CmdLineInterface& _cmd)
00090 {
00091 std::cout << _cmd.getVersion() << std::endl;
00092 }
00093
00094 inline void DocBookOutput::usage(CmdLineInterface& _cmd )
00095 {
00096 std::list<Arg*> argList = _cmd.getArgList();
00097 std::string progName = _cmd.getProgramName();
00098 std::string version = _cmd.getVersion();
00099 XorHandler xorHandler = _cmd.getXorHandler();
00100 std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
00101
00102
00103 std::cout << "<?xml version='1.0'?>" << std::endl;
00104 std::cout << "<!DOCTYPE book PUBLIC \"-//Norman Walsh//DTD DocBk XML V4.2//EN\"" << std::endl;
00105 std::cout << "\t\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\">" << std::endl << std::endl;
00106
00107 std::cout << "<book>" << std::endl;
00108 std::cout << "<refentry>" << std::endl;
00109
00110 std::cout << "<refmeta>" << std::endl;
00111 std::cout << "<refentrytitle>" << std::endl;
00112 std::cout << progName << std::endl;
00113 std::cout << "</refentrytitle>" << std::endl;
00114 std::cout << "<manvolnum>1</manvolnum>" << std::endl;
00115 std::cout << "</refmeta>" << std::endl;
00116
00117 std::cout << "<refnamediv>" << std::endl;
00118 std::cout << "<refname>" << std::endl;
00119 std::cout << progName << std::endl;
00120 std::cout << "</refname>" << std::endl;
00121 std::cout << "</refnamediv>" << std::endl;
00122
00123 std::cout << "<cmdsynopsis>" << std::endl;
00124
00125 std::cout << "<command>" << progName << "</command>" << std::endl;
00126
00127
00128 for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
00129 {
00130 std::cout << "<group choice='req'>" << std::endl;
00131 for ( ArgVectorIterator it = xorList[i].begin();
00132 it != xorList[i].end(); it++ )
00133 printShortArg((*it));
00134
00135 std::cout << "</group>" << std::endl;
00136 }
00137
00138
00139 for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
00140 if ( !xorHandler.contains( (*it) ) )
00141 printShortArg((*it));
00142
00143 std::cout << "</cmdsynopsis>" << std::endl;
00144
00145 std::cout << "<refsect1>" << std::endl;
00146 std::cout << "<title>Description</title>" << std::endl;
00147 std::cout << "<para>" << std::endl;
00148 std::cout << _cmd.getMessage() << std::endl;
00149 std::cout << "</para>" << std::endl;
00150 std::cout << "</refsect1>" << std::endl;
00151
00152 std::cout << "<refsect1>" << std::endl;
00153 std::cout << "<title>Options</title>" << std::endl;
00154 std::cout << "<para>" << std::endl;
00155 std::cout << "<itemizedlist>" << std::endl;
00156
00157 for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
00158 {
00159 std::cout << "<itemizedlist>" << std::endl;
00160 size_t xlen = xorList.size() - 1;
00161 size_t xcount = 0;
00162 for ( ArgVectorIterator it = xorList[i].begin();
00163 it != xorList[i].end(); it++, xcount++ )
00164 {
00165 printLongArg((*it));
00166 if ( xcount < xlen )
00167 std::cout << "<listitem>OR</listitem>" << std::endl;
00168 }
00169
00170 std::cout << "</itemizedlist>" << std::endl;
00171 }
00172
00173
00174 for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
00175 if ( !xorHandler.contains( (*it) ) )
00176 printLongArg((*it));
00177
00178 std::cout << "</itemizedlist>" << std::endl;
00179 std::cout << "</para>" << std::endl;
00180 std::cout << "</refsect1>" << std::endl;
00181
00182 std::cout << "<refsect1>" << std::endl;
00183 std::cout << "<title>Version</title>" << std::endl;
00184 std::cout << "<para>" << std::endl;
00185 std::cout << version << std::endl;
00186 std::cout << "</para>" << std::endl;
00187 std::cout << "</refsect1>" << std::endl;
00188
00189 std::cout << "</refentry>" << std::endl;
00190 std::cout << "</book>" << std::endl;
00191
00192 }
00193
00194 inline void DocBookOutput::failure( CmdLineInterface& _cmd,
00195 ArgException& e )
00196 {
00197 std::cout << e.what() << std::endl;
00198 }
00199
00200 inline void DocBookOutput::substituteSpecialChars( std::string& s,
00201 char r,
00202 std::string& x )
00203 {
00204 size_t p;
00205 while ( (p = s.find_first_of(r)) != std::string::npos )
00206 {
00207 s.erase(p,1);
00208 s.insert(p,x);
00209 }
00210 }
00211
00212 inline void DocBookOutput::removeChar( std::string& s, char r)
00213 {
00214 size_t p;
00215 while ( (p = s.find_first_of(r)) != std::string::npos )
00216 {
00217 s.erase(p,1);
00218 }
00219 }
00220
00221 inline void DocBookOutput::printShortArg(Arg* a)
00222 {
00223 std::string lt = "<";
00224 std::string gt = ">";
00225
00226 std::string id = a->shortID();
00227 substituteSpecialChars(id,'<',lt);
00228 substituteSpecialChars(id,'>',gt);
00229 removeChar(id,'[');
00230 removeChar(id,']');
00231
00232 std::string choice = "opt";
00233 if ( a->isRequired() )
00234 choice = "req";
00235
00236 std::string repeat = "norepeat";
00237 if ( a->acceptsMultipleValues() )
00238 repeat = "repeat";
00239
00240
00241
00242 std::cout << "<arg choice='" << choice
00243 << "' repeat='" << repeat << "'>"
00244 << id << "</arg>" << std::endl;
00245
00246 }
00247
00248 inline void DocBookOutput::printLongArg(Arg* a)
00249 {
00250 std::string lt = "<";
00251 std::string gt = ">";
00252
00253 std::string id = a->longID();
00254 substituteSpecialChars(id,'<',lt);
00255 substituteSpecialChars(id,'>',gt);
00256 removeChar(id,'[');
00257 removeChar(id,']');
00258
00259 std::string desc = a->getDescription();
00260 substituteSpecialChars(desc,'<',lt);
00261 substituteSpecialChars(desc,'>',gt);
00262
00263 std::cout << "<simplelist>" << std::endl;
00264
00265 std::cout << "<member>" << std::endl;
00266 std::cout << id << std::endl;
00267 std::cout << "</member>" << std::endl;
00268
00269 std::cout << "<member>" << std::endl;
00270 std::cout << desc << std::endl;
00271 std::cout << "</member>" << std::endl;
00272
00273 std::cout << "</simplelist>" << std::endl;
00274 }
00275
00276 }
00277 #endif
00278
00279
00280 #endif