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



Page generated by Doxygen 1.9.8 for MRPT 1.4.0 SVN: at Thu Dec 14 16:41:50 UTC 2023