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