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


ecl_command_line
Author(s): Daniel Stonier (d.stonier@gmail.com)
autogenerated on Thu Jan 2 2014 11:12:37