Main MRPT website > C++ reference
MRPT logo
DocBookOutput.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * file: DocBookOutput.h
5  *
6  * Copyright (c) 2004, Michael E. Smoot
7  * All rights reverved.
8  *
9  * See the file COPYING in the top directory of this distribution for
10  * more information.
11  *
12  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
13  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18  * DEALINGS IN THE SOFTWARE.
19  *
20  *****************************************************************************/
21 
22 #ifndef TCLAP_DOCBOOKOUTPUT_H
23 #define TCLAP_DOCBOOKOUTPUT_H
24 
25 #include <string>
26 #include <vector>
27 #include <list>
28 #include <iostream>
29 #include <algorithm>
30 
35 
36 namespace TCLAP {
37 
38 /**
39  * A class that generates DocBook output for usage() method for the
40  * given CmdLine and its Args.
41  */
43 {
44 
45  public:
46 
47  /**
48  * Prints the usage to stdout. Can be overridden to
49  * produce alternative behavior.
50  * \param c - The CmdLine object the output is generated for.
51  */
52  virtual void usage(CmdLineInterface& c);
53 
54  /**
55  * Prints the version to stdout. Can be overridden
56  * to produce alternative behavior.
57  * \param c - The CmdLine object the output is generated for.
58  */
59  virtual void version(CmdLineInterface& c);
60 
61  /**
62  * Prints (to stderr) an error message, short usage
63  * Can be overridden to produce alternative behavior.
64  * \param c - The CmdLine object the output is generated for.
65  * \param e - The ArgException that caused the failure.
66  */
67  virtual void failure(CmdLineInterface& c,
68  ArgException& e );
69 
70  protected:
71 
72  /**
73  * Substitutes the char r for string x in string s.
74  * \param s - The string to operate on.
75  * \param r - The char to replace.
76  * \param x - What to replace r with.
77  */
78  void substituteSpecialChars( std::string& s, char r, std::string& x );
79  void removeChar( std::string& s, char r);
80 
81  void printShortArg(Arg* it);
82  void printLongArg(Arg* it);
83 };
84 
85 
87 {
88  std::cout << _cmd.getVersion() << std::endl;
89 }
90 
92 {
93  std::list<Arg*> argList = _cmd.getArgList();
94  std::string progName = _cmd.getProgramName();
95  std::string version = _cmd.getVersion();
96  XorHandler xorHandler = _cmd.getXorHandler();
97  std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
98 
99 
100  std::cout << "<?xml version='1.0'?>" << std::endl;
101  std::cout << "<!DOCTYPE book PUBLIC \"-//Norman Walsh//DTD DocBk XML V4.2//EN\"" << std::endl;
102  std::cout << "\t\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\">" << std::endl << std::endl;
103 
104  std::cout << "<book>" << std::endl;
105  std::cout << "<refentry>" << std::endl;
106 
107  std::cout << "<refmeta>" << std::endl;
108  std::cout << "<refentrytitle>" << std::endl;
109  std::cout << progName << std::endl;
110  std::cout << "</refentrytitle>" << std::endl;
111  std::cout << "<manvolnum>1</manvolnum>" << std::endl;
112  std::cout << "</refmeta>" << std::endl;
113 
114  std::cout << "<refnamediv>" << std::endl;
115  std::cout << "<refname>" << std::endl;
116  std::cout << progName << std::endl;
117  std::cout << "</refname>" << std::endl;
118  std::cout << "</refnamediv>" << std::endl;
119 
120  std::cout << "<cmdsynopsis>" << std::endl;
121 
122  std::cout << "<command>" << progName << "</command>" << std::endl;
123 
124  // xor
125  for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
126  {
127  std::cout << "<group choice='req'>" << std::endl;
128  for ( ArgVectorIterator it = xorList[i].begin();
129  it != xorList[i].end(); it++ )
130  printShortArg((*it));
131 
132  std::cout << "</group>" << std::endl;
133  }
134 
135  // rest of args
136  for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
137  if ( !xorHandler.contains( (*it) ) )
138  printShortArg((*it));
139 
140  std::cout << "</cmdsynopsis>" << std::endl;
141 
142  std::cout << "<refsect1>" << std::endl;
143  std::cout << "<title>Description</title>" << std::endl;
144  std::cout << "<para>" << std::endl;
145  std::cout << _cmd.getMessage() << std::endl;
146  std::cout << "</para>" << std::endl;
147  std::cout << "</refsect1>" << std::endl;
148 
149  std::cout << "<refsect1>" << std::endl;
150  std::cout << "<title>Options</title>" << std::endl;
151  std::cout << "<para>" << std::endl;
152  std::cout << "<itemizedlist>" << std::endl;
153  // xor
154  for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
155  {
156  std::cout << "<itemizedlist>" << std::endl;
157  size_t xlen = xorList.size() - 1;
158  size_t xcount = 0;
159  for ( ArgVectorIterator it = xorList[i].begin();
160  it != xorList[i].end(); it++, xcount++ )
161  {
162  printLongArg((*it));
163  if ( xcount < xlen )
164  std::cout << "<listitem>OR</listitem>" << std::endl;
165  }
166 
167  std::cout << "</itemizedlist>" << std::endl;
168  }
169 
170  // rest of args
171  for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
172  if ( !xorHandler.contains( (*it) ) )
173  printLongArg((*it));
174 
175  std::cout << "</itemizedlist>" << std::endl;
176  std::cout << "</para>" << std::endl;
177  std::cout << "</refsect1>" << std::endl;
178 
179  std::cout << "<refsect1>" << std::endl;
180  std::cout << "<title>Version</title>" << std::endl;
181  std::cout << "<para>" << std::endl;
182  std::cout << version << std::endl;
183  std::cout << "</para>" << std::endl;
184  std::cout << "</refsect1>" << std::endl;
185 
186  std::cout << "</refentry>" << std::endl;
187  std::cout << "</book>" << std::endl;
188 
189 }
190 
192  ArgException& e )
193 {
194  std::cout << e.what() << std::endl;
195 }
196 
197 inline void DocBookOutput::substituteSpecialChars( std::string& s,
198  char r,
199  std::string& x )
200 {
201  size_t p;
202  while ( (p = s.find_first_of(r)) != std::string::npos )
203  {
204  s.erase(p,1);
205  s.insert(p,x);
206  }
207 }
208 
209 inline void DocBookOutput::removeChar( std::string& s, char r)
210 {
211  size_t p;
212  while ( (p = s.find_first_of(r)) != std::string::npos )
213  {
214  s.erase(p,1);
215  }
216 }
217 
219 {
220  std::string lt = "&lt;";
221  std::string gt = "&gt;";
222 
223  std::string id = a->shortID();
224  substituteSpecialChars(id,'<',lt);
225  substituteSpecialChars(id,'>',gt);
226  removeChar(id,'[');
227  removeChar(id,']');
228 
229  std::string choice = "opt";
230  if ( a->isRequired() )
231  choice = "req";
232 
233  std::string repeat = "norepeat";
234  if ( a->acceptsMultipleValues() )
235  repeat = "repeat";
236 
237 
238 
239  std::cout << "<arg choice='" << choice
240  << "' repeat='" << repeat << "'>"
241  << id << "</arg>" << std::endl;
242 
243 }
244 
246 {
247  std::string lt = "&lt;";
248  std::string gt = "&gt;";
249 
250  std::string id = a->longID();
251  substituteSpecialChars(id,'<',lt);
252  substituteSpecialChars(id,'>',gt);
253  removeChar(id,'[');
254  removeChar(id,']');
255 
256  std::string desc = a->getDescription();
257  substituteSpecialChars(desc,'<',lt);
258  substituteSpecialChars(desc,'>',gt);
259 
260  std::cout << "<simplelist>" << std::endl;
261 
262  std::cout << "<member>" << std::endl;
263  std::cout << id << std::endl;
264  std::cout << "</member>" << std::endl;
265 
266  std::cout << "<member>" << std::endl;
267  std::cout << desc << std::endl;
268  std::cout << "</member>" << std::endl;
269 
270  std::cout << "</simplelist>" << std::endl;
271 }
272 
273 } //namespace TCLAP
274 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013